16 Linux ip command examples to configure network interfaces (cheatsheet)

The ip is a very powerful utility that is used to perform many tasks. Multiple older utilities such as ifconfig and route command were earlier used to perform these functions. In this article I will share different examples to configure network interfaces and routes using ip command.

IMPORTANT NOTE:

All the changes performed using ip command are applied only for the current session and are not reboot persistent. We mostly use this tool to perform temporary network assignments in linux resuce mode or emergency mode to activate the network temporarily for transferring logs or accessing the server.

 

Install iproute rpm

By default iproute rpm is installed on all variants of RHEL/CentOS Linux, you can also find ip binary command in the rescue and emergency target.

NOTE:

On RHEL system you must have an active subscription to RHN or you can configure a local offline repository using which "yum" package manager can install the provided rpm and it's dependencies.
# yum -y install iproute

The absolute path is /usr/sbin/ip

# which ip
/usr/sbin/ip

 

1. Show IP Address of all the available interfaces using ip command

Use ip addr show to show the IP Address of all the available network interfaces on your Linux system

# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:26:91:86 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.20/8 brd 10.255.255.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe26:9186/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:2c:8a:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.115/24 brd 192.168.0.255 scope global noprefixroute enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::d3ff:3740:bf75:74ad/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s9
       valid_lft 1060sec preferred_lft 1060sec
    inet6 fe80::4025:8441:1c30:ebf5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Here I have three interfaces (including the loopback interface), wherein we can check the IP Address of individual interface as highlighted above.

 

2. Show IP Address of individual interface

You can use "dev" argument along with "ip addr show" to display information of provided interface only. Here we are displaying IP Address detail for enp0s9 interface only.

# ip addr show dev enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s9
       valid_lft 949sec preferred_lft 949sec
    inet6 fe80::4025:8441:1c30:ebf5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

 

3. List interfaces based on interface type

There are various types of NIC which can be available on your Linux system such as vlan, bond, bridge, vxlan etc.

Now to list only bridge interfaces on your system:

# ip addr show type bridge
11: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:4e:e8:2c brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

To list bond interface from your system

# ip addr show type bond
10: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 9c:dc:71:76:7c:81 brd ff:ff:ff:ff:ff:ff
    inet 192.151.12.10/26 brd 192.151.12.63 scope global noprefixroute bond1
       valid_lft forever preferred_lft forever

 

4. Check if NIC's link state is enabled or disabled

"ip link" will manage and display the state of all network interfaces. To display the current state of all the interfaces

# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:26:91:86 brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2c:8a:ba brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff

To check the link state of individual interface

# ip link show dev enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff

 

5. Change NIC's Link state

Since our NIC interface is already in enabled state, let us turn down link state for enp0s9 interface

# ip link set enp0s9 down

Check the link state again for enp0s9

# ip  link show dev enp0s9
4: enp0s9: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff

Now since it is in DOWN state, let us change the link status for enp0s9 to UP

# ip link set enp0s9 up

Check the link state again for enp0s9

# ip  link show dev enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff
NOTE:

The "ip link set ifname command" sets a network interface in IFF_UP state and enables it from the kernel's scope. This is different from the ifup ifname command for initscripts or NetworkManager's activation state of a device. In fact, NetworkManager always sets an interface up even if it is currently disconnected.

 

6. Assign/Add IPv4 and IPv6 address

To assign ipv4 address using you can use "ip addr add". To demo this step I have already temporarily unassigned/removed IP address from enp0s9 interface.

# ip addr add 192.168.56.101/24 dev enp0s9

To assign IPv4 address using ip command you would need netmask prefix value which here for me is "24". You can either calculate this prefix value or you can use other online network calculators which can help you get the prefix value depending upon your netmask.

To check the assigned IPv4 Address for enp0s9 interface

# ip addr show enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 scope global enp0s9
       valid_lft forever preferred_lft forever
    inet6 fe80::4025:8441:1c30:ebf5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

To assign IPv6 address using ip command you can use the same command as used for IPv4 address but with the IPv6 address and netmask

# ip addr add 2001:0001:0001:1443::0411/122 dev eno55

Verify the IPv6 address assigned to the provided interface

# ip addr show eno55
8: eno55: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq portid 0100000000000000000000535634393543 state UP qlen 1000
    link/ether 9c:dc:71:77:ef:53 brd ff:ff:ff:ff:ff:ff
    inet6 2001:1:1:1443::411/122 scope global
       valid_lft forever preferred_lft forever
    inet6 fe80::b587:e726:85d2:b57a/64 scope link tentative
       valid_lft forever preferred_lft forever

 

7. Un-Assign/Delete IPv4 and IPv6 address

You can un-assign or delete an assigned ip address from an interface using "ip addr del" argument. Here you must give the exact IP address along with the prefix value to un-assign the ip address.

# ip add del 192.168.56.101/24 dev enp0s9

Check and verify if the ip address was un-assigned sucessfully from the provided interface

# ip addr show enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff
    inet6 fe80::4025:8441:1c30:ebf5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

You can un-assign or delete the IPv6 address for individual interface using the same command as used for IPv4 address above.

# ip addr del 2001:0001:0001:1443::0411/122 dev eno55

Next check the IPv6 Address of the interface to make sure it is un-assigned from the provided interface

# ip addr show eno55
8: eno55: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq portid 0100000000000000000000535634393543 state UP qlen 1000
    link/ether 9c:dc:71:77:ef:53 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::b587:e726:85d2:b57a/64 scope link
       valid_lft forever preferred_lft forever

 

8. Un-assign/delete all protocol addresses from individual interface

By using "ip addr del" we only remove the provided address assigned to the respective interface. But if you wish to remove all the protocol address assigned to individual interface then you must use flush

# ip addr flush dev enp0s9

Next check the interface detail

# ip addr show dev enp0s9
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:5f:88:6f brd ff:ff:ff:ff:ff:ff

As you see both IPv4 and IPv6 addresses are removed.

 

9. Display detailed statistics of interface

To get more detailed information on packet statistics for individual interface using "-s" argument

# ip -s link show dev nm-bridge1
17: nm-bridge1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT qlen 1000
    link/ether 00:17:a4:77:00:44 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    291975556  1468680  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    4360125    30415    0       0       0       0

If the "-s" option appears twice or more, the amount of information increases.

# ip -s -s link show dev nm-bridge1
17: nm-bridge1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT qlen 1000
    link/ether 00:17:a4:77:00:44 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    291982160  1468785  0       0       0       0
    RX errors: length   crc     frame   fifo    missed
               0        0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    4364175    30434    0       0       0       0
    TX errors: aborted  fifo   window heartbeat transns
               0        0       0       0       2

 

10. List available routes

To list all the available routes on your Linux system

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102

You can also use "ip route" or "ip route show" to get the list of available routes in the routing table

 

11. Add default route to routing table

To add default route (adding an entry in the routing table) use "ip route add default"

# ip route add default via  192.168.0.1 dev enp0s8

Verify the available default routes in the routing table

# ip route list
default via 192.168.0.1 dev enp0s8
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102
NOTE:

Here before re-running the command to add default route I have manually deleted the existing route. If you attempt to re-add the same route again then the command will fail with error "RTNETLINK answers: File exists"

To add more details for the default route as we have for other routes

# ip route add default via 192.168.0.1 dev enp0s8 proto static metric 101

Verify the default route again using ip command

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102

 

12. Delete default route from routing table

To delete the default route, instead of using "add", use "delete". Now here I have two default routes, where I wish to delete the route with 10.0.2.2 for enp0s3 interface

# ip route list
default via 10.0.2.2 dev enp0s3 proto static metric 100
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102

Delete the default route

# ip route delete default via 10.0.2.2

Verify the available routes in the routing table

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102

You can also remove the default route using interface name to delete all the default routes mapped to provided interface

# ip route delete default dev enp0s8

 

13. Add additional route to the routing table

Now after adding default route, you can also add additional routes to the routing table. Here we will add a route to 192.168.168.0/24 via the route 192.168.0.1

# ip route add 192.168.168.0/24 via 192.168.0.1 dev enp0s8

Verify the available routes in the routing table

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102
192.168.168.0/24 via 192.168.0.1 dev enp0s8

 

14. Delete additional route from routing table

Here we had added a new route for 192.168.168.0/24 via 192.168.0.1. so we will attempt to delete the same here

# ip route delete 192.168.168.0/24 via 192.168.0.1

Verify the route list again

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102

 

15. Replace existing routes from routing table

We can also replace certain directives from existing routes in the routing table. In our last example we added route 192.168.168.0/24 via 192.168.0.1 so we will change this to 192.168.168.0/24 via 10.0.2.2 gateway via a different interface (enp0s3)

My existing list of routes

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102
192.168.168.0/24 via 192.168.0.1 dev enp0s8

Replace the route values

# ip route replace 192.168.168.0/24 via 10.0.2.2 dev enp0s3

Verify the new route list

# ip route list
default via 192.168.0.1 dev enp0s8 proto static metric 101
10.0.0.0/8 dev enp0s3 proto kernel scope link src 10.0.2.20 metric 100
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.115 metric 101
192.168.56.0/24 dev enp0s9 proto kernel scope link src 192.168.56.101 metric 102
192.168.168.0/24 via 10.0.2.2 dev enp0s3

 

16. Display the route an address will take

Now we have added all the routes, but we would like to know the route which any address on the linux system will take

To know the route which will be used by 192.168.168.10 address

# ip route get 192.168.168.10
192.168.168.10 via 10.0.2.2 dev enp0s3 src 10.0.2.20 uid 0
    cache

To know the route which will be used by 192.168.0.10 address

# ip route get 192.168.0.10
192.168.0.10 dev enp0s8 src 192.168.0.115 uid 0
    cache

 

Conclusion

Linux ip command is a very handy tool for Linux Administrators. The only drawback with this tool is that the changes are non-persistent which means this will not survive a reboot. But in many cases such as in rescue mode or to verify temporary network changes we can use this tool to create and assign network to interfaces.

Lastly I hope this article with different ip command examples to perform network related activities (cheatsheet) on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
man page for ip command

 

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.