Assign IP address via command-line

By Sachin
March 24, 2012

Sometimes, assigning IP address using command line is much more easier than using Network manager applet, this post covers examples with syntax

(Updated on June 17, 2017)

Note: You must be root or sudo user to run following commands. ifconfig has been deprecated in favor of ip

Assign IP address


ifconfig <INTERFACE> <IP-ADDRESS> netmask <NETMASK>

# or

ip addr add <IP-ADDRESS/CIDR> dev <INTERFACE>


ifconfig eth0 netmask

# or

ip addr add dev eth0

Add gateway/route


route add default gw <GATEWAY-IP> <INTERFACE>

# or

ip route add default via <GATEWAY-IP> dev <INTERFACE>


route add default gw eth0

# or

ip route add default via dev eth0

Set DNS address

Optionally DNS can be entered in the file /etc/resolv.conf in following format

# /etc/resolv.conf

All the above changes will be temporary(unless you reboot the system)

Additional scenario

  • You want 10.10.10.x address space to bypass default gateway of the network. You can reach network range of via on device eth0

      ip route add via dev eth0

Make routes persistent on Fedora

Add following entry into the file /etc/sysconfig/network-scripts/route-DEVICE_NAME via dev DEVICE_NAME