Configuring IP addresses and routes from the command line is a mandatory skill that every Linux administrator should learn. In this article, we will review how we can assign a static IP address using
In addition, we will discuss how we can use the
ip route command to create a static route to change the default gateway for the Linux system.
Set Static IP Address Using ifconfig Command (Deprecated)
ifconfig command is deprecated and replaced by the
ip command in Linux. However, the
ifconfig command is still works and available for most Linux distributions. It is used to configure the network interfaces.
If no arguments are given,
ifconfig displays the status of the currently active interfaces.
If a single interface argument is given, it displays the status of the given interface only.
-a argument added, the command displays the status of all interfaces, even those that are down.
To configure a temporary IP address, use the following command syntax:
ifconfig <interface> <ipv4address> netmask <subnetmask>
Assuming you want to make the following changes:
- The network device name is
- Change the IP address to the static value
- Set the subnet mask to
ifconfig eth0 192.168.1.10 netmask 255.255.255.0
On success, the command will not show any output. However, the IP address configured this way is not permanent and will be lost after a reboot.
To make permanent changes to the IP address, you need to edit the configuration file according to your Linux distribution.
Set Static IP Address Using ip Command
To find your current IP address, you can use the
ip command with the
a option for address.
To add address
192.168.1.10 with netmask
255.255.255.0 to device
eth0, you would type:
ip address add 192.168.1.10/255.255.255.0 dev eth0
To delete all addresses from an interface
ip address flush dev eth0
To bring interfaces up or down, use the
ip link set dev followed by the device name and the desired state. For example, to bring the interface
eth0 online, you would type:
ip link set dev eth0 up
And to bring it offline:
ip link set dev eth0 down
Displaying and Altering the Linux Routing Table
We use the
ip route command to create static routes in Linux to specific hosts or change the Linux system’s default gateway.
To display the Linux routing table, type the following command:
ip route show
To add a new entry to the routing table, use the
ip route add command followed by network or device name.
Add a route to
192.168.1.0/255.255.255.0 via the gateway at
ip route add 192.168.1.0/255.255.255.0 via 192.168.1.1
To add a default route, use the keyword
default. All network packets that cannot be sent according to the previous entries of the routing table are sent through the following default gateway:
ip route add default via 192.168.1.1 dev eth0
To delete the default route:
ip route del default
The syntax for deleting a route is the same as when adding. For example, the following command will delete a route for 192.168.1.0/255.255.255.0 via the gateway at 192.168.1.1.
ip route del 192.168.1.0/255.255.255.0 via 192.168.1.1