How to Set Static IP Address and Modifying Routing Table on Linux

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

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 ip and ifconfig commands.

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)

Nowadays 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.

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 a temporary IP address, use the following command syntax:

ifconfig <interface> <ipv4address> netmask <subnetmask>Code language: HTML, XML (xml)

Assuming you want to make the following changes:

  • The 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.0Code language: CSS (css)

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.

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 upCode language: JavaScript (javascript)

And to bring it offline:

ip link set dev eth0 downCode language: JavaScript (javascript)

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 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 eth0Code language: CSS (css)

To delete the default route:

ip route del defaultCode language: JavaScript (javascript)

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
Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%