How To Set Static IP Address And Modifying Routing Table On Linux

How To Manually Set Static IP Address And Route On Linux

This article will show you how to configure a static IP address from the command line on Linux and how to modifying routing table.

Configuring IP address and routes from the command line is one of the mandatory skill that every Linux administrator should learn. In this article we are going to review how we can assign a static IP address using ip and ifconfig commands. In addition to, we will discuss how we can use ip route command to create a static route and to change the default gateway for the Linux system.

Set static IP address using ifconfig command (deprecated)

Nowadays ifconfig command is deprecated and replaced by ip command in Linux. However, ifconfig command is still works and available for most of the Linux distributions. It is used to configure the network interfaces.

If no arguments are given, ifconfig displays the status of the currently active interfaces.

ifconfig

If a single interface argument is given, it displays the status of the given interface only.

ifconfig eth0

With -a argument added, the command displays the status of all interfaces, even those that are down.

ifconfig -a

To configure temporary IP address, use the following command syntax:

ifconfig <interface> <ipv4address> netmask <subnetmask>

Assuming you want to make the following changes:

  • Network device name is eth0
  • Change the IP address to the static value 192.168.1.10
  • Set the subnet mask to 255.255.255.0
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 IP address you need to edit 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.

ip a

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 eth0:

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 basically use the ip route command to create static routes in Linux to specific hosts or to change the default gateway for the Linux system. 

To display 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 192.168.1.1

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. 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 add 192.168.1.0/255.255.255.0 via 192.168.1.1

Leave a Reply

Your email address will not be published.