Introduction

If you’ve been working with Linux/UNIX operating systems for a while then you would’ve probably used the ifconfig command very frequently. In case you are new to Linux operating systems it’s still likely that you’ve used or at least heard of the ifconfig command at some point in your Linux user experience. The ifconfig command has been around for years and was primarily used to view the network interface cards on your system and their corresponding network information. The ifconfig command was also used to modify properties of network interfaces i.e. change the IP addresses assigned to them, assign multiple IP addresses etc. In this article we will not be covering the ifconfig command but will instead focus on it’s enhanced and more feature rich replacement found on almost all modern Linux distributions. The command we are referring to here is the ip command. The ip command has deprecated the ifconfig command to the extent that ifconfig is not even installed by default on RHEL 7/Centos 7 distributions.

Let’s now take a look at some examples to familiarize ourselves with the ip command

Example 1: Display network information for all network interfaces
To view IP address information for all network interfaces we use the ‘ip address show’ command.

[root@linuxnix ~]# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:81 brd ff:ff:ff:ff:ff:ff
inet 192.168.188.133/24 brd 192.168.188.255 scope global dynamic ens33
valid_lft 1772sec preferred_lft 1772sec
inet6 fe80::acd8:bd6b:965b:ad38/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.188.146/24 brd 192.168.188.255 scope global dynamic ens37
valid_lft 1731sec preferred_lft 1731sec
inet6 fe80::28d6:c38:a0e0:66be/64 scope link
valid_lft forever preferred_lft forever
[root@linuxnix ~]#

You may also use the abbreviated shorthand version ‘ip a s’ which will display the same information.

Example 2: Display network information for a particular interface
To do this we need to provide the name of the desired network interface with the ip address show command.
In the below example, we retrieve information for the ens33 network interface.

[root@linuxnix ~]# ip address show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:81 brd ff:ff:ff:ff:ff:ff
inet 192.168.188.133/24 brd 192.168.188.255 scope global dynamic ens33
valid_lft 1671sec preferred_lft 1671sec
inet6 fe80::acd8:bd6b:965b:ad38/64 scope link
valid_lft forever preferred_lft forever

Example 3: Enable/Disable a network interface
To bring a network interface up or down, we use the ip command with the link set subcommands. In the below example, we enable and disable the interface ens37 on our system.

To bring down the interface we type ip link set <interface name> down

[root@linuxnix ~]# ip link set ens37 down

Let’s verify that the interface is down with the ip address show command.

[root@linuxnix ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
[root@linuxnix ~]#

To bring the interface up we type ip link set <interface name> up.

[root@linuxnix ~]# sudo ip link set ens37 up

Now if we check the status of the interface again we’ll observe that it’s now up.

[root@linuxnix ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.188.146/24 brd 192.168.188.255 scope global dynamic ens37
valid_lft 1798sec preferred_lft 1798sec
inet6 fe80::28d6:c38:a0e0:66be/64 scope link
valid_lft forever preferred_lft forever

Example 4: Assign IP address to an interface
To assign an IP address assigned to an interface, we use the ip address add subcommand followed by the IP address along with the network prefix and the interface to which the address needs to be assigned to. Given below is an example.

[root@linuxnix ~]# ip addr add 192.168.188.146/24 dev ens37
[root@linuxnix ~]# ip add show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.188.146/24 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::28d6:c38:a0e0:66be/64 scope link
valid_lft forever preferred_lft forever

Example 5: Delete IP address from an interface
To delete an IP address assigned to an interface, we use the ip address delete subcommand followed by the IP address along with the network prefix and the interface from which the address needs to be deleted from. Given below is an example.

[root@linuxnix ~]# ip addr del 192.168.188.146/24 dev ens37

Example 6: Add a logical IP address
To add a logical IP address to an interface we use almost the same command as we did while assigning an IP address to an interface but here we also add the label keyword followed by the logical interface name. Here’s an example:

[root@linuxnix ~]# ip addr add 192.168.188.150/24 dev ens37 label ens37:1

To verify the address assignation let’s check the ip addr show output for the interface.

[root@linuxnix ~]# ip addr show ens37:1
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.188.146/24 scope global ens37
valid_lft forever preferred_lft forever
inet 192.168.188.150/24 scope global secondary ens37:1
valid_lft forever preferred_lft forever
inet6 fe80::28d6:c38:a0e0:66be/64 scope link
valid_lft forever preferred_lft forever

As you may observe the IP address has been successfully assigned to the logical interface.

Example 7: Check network interface statistics
With ip command we can also view the network statistics like bytes and packets transferred, errors or dropped packets etc for all the network interfaces. To view network statistics, use ip -s link command.

[root@linuxnix ~]# ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
383908 1535 0 0 0 0
TX: bytes packets errors dropped carrier collsns
383908 1535 0 0 0 0
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:f6:1d:81 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
160034 2128 0 0 0 0
TX: bytes packets errors dropped carrier collsns
70690 644 0 0 0 0
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:f6:1d:8b brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
89100 1254 0 0 0 0
TX: bytes packets errors dropped carrier collsns
16468 175 0 0 0 0

Example 8: Display kernel routing table
To display the kernel routing table type the ip route show command.

[root@linuxnix ~]# ip route show
default via 192.168.188.2 dev ens33 proto static metric 100
192.168.188.0/24 dev ens37 proto kernel scope link src 192.168.188.146
192.168.188.0/24 dev ens33 proto kernel scope link src 192.168.188.133 metric 100
[root@linuxnix ~]#

Example 9: Add a static route
To add a static route to the kernel routing table we use the ip route add command. Given below is an example of adding a static route on the ens37 network interface.

[root@linuxnix ~]# ip route add 192.168.188.134 via 192.168.188.1 dev ens37

Let’s run the ip route show command to verify that the route has been added.

[root@linuxnix ~]# ip route show
default via 192.168.188.2 dev ens33 proto static metric 100
default via 192.168.188.2 dev ens37 proto static metric 101
192.168.188.0/24 dev ens37 proto kernel scope link src 192.168.188.146
192.168.188.0/24 dev ens33 proto kernel scope link src 192.168.188.133 metric 100
192.168.188.0/24 dev ens37 proto kernel scope link src 192.168.188.146 metric 101
192.168.188.134 via 192.168.188.1 dev ens37

The route added by the ip route add command will not persist across reboots. To make the entry permanent we need to add it in the /etc/sysconfig/network-scripts/route-<interface name> file. So in order to make the routing entry we just did permanent, we need to add the following line in the file /etc/sysconfig/network-scripts/route-ens37.

192.168.188.134 via 192.168.188.1 dev ens37

We could also add a default route using the ip route add command. The syntax would be as follows:

ip route add default via <IP address>

Example 10: Delete a static route
To delete a static route we use the ip route del command followed by the address for which the route needs to be removed. To demonstrate, lets delete the route that we added in the previous example.

[root@linuxnix ~]# ip route del 192.168.188.134

Example 11: View arp cache
To view entries in ARP cache i.e. MAC addresses of the devices connected in LAN, we would type the following command.

[root@linuxnix ~]# ip neigh
192.168.188.1 dev ens33 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.188.254 dev ens33 lladdr 00:50:56:fa:4f:07 STALE
192.168.188.2 dev ens33 lladdr 00:50:56:fd:b2:1a STALE

Example 12: View help
To view the available options for use with the ip command type ip –help.

[root@linuxnix ~]# ip --help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
ip [ -force ] -batch filename
where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
netns | l2tp | macsec | tcp_metrics | token }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
-h[uman-readable] | -iec |
-f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
-4 | -6 | -I | -D | -B | -0 |
-l[oops] { maximum-addr-flush-attempts } |
-o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
-rc[vbuf] [size] | -n[etns] name | -a[ll] }

To view the more detailed information you could access the man page for the ip command by typing ‘man ip’

 

Conclusion

This concludes our exploration of the Linux ip command. We hope that you’ve found the examples illustrated in this article useful and we look forward towards your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.