The ip command used to display, configure or manipulate routing, network interfaces, and tunnels in Linux operating systems. This comes with iproute2 collections of tools that have replaced net-tools utilities.
We have traditionally used ifconfig command which is now deprecated and now it is encouraged to use iproute2 tools.
In this tutorial, I will go through examples with explanations of how to use the ip command for administrating your Linux TCP/IP network.
1) Show IP Address assigned on interface
You can use ip command to retrieve the IP address, MAc address and more information of a specified network interface on your Linux system.
Assuming that you want to get the IP of ens160 interface, run the following command:
$ ip addr show dev ens160
$ ip addr show ens160
Output 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 8000 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:9a:f8:ad brd ff:ff:ff:ff:ff:ff inet 10.9.8.40/24 brd 10.9.8.255 scope global ens160 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe9a:f8ad/64 scope link valid_lft forever preferred_lft forever
To show all the interface details, run:
$ ip addr show
2) Assign IP address to interface
In order to set an IP address to a specified interface, you can use the command as follows:
$ sudo ip addr add 192.168.0.193/24 dev wlan0
After you have set the IP address you can confirm by
ip addr show, whether the changes have taken effect, run the following command:
$ ip addr show wlan0
3) Set multiple ip addresses to same interface
In some cases, you want to set multiple IP addresses to the same network interface. You can easily do it with ip command by running:
$ sudo ip addr add 10.9.8.40/24 dev ens160 $ sudo ip addr add 10.9.8.46/24 dev ens160
To verify that the IP addresses are assigned to network interface ens160, run the following command:
$ ip -4 addr show dev ens160
The output indicates that ens160 has two ip addresses:
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 inet 10.9.8.40/24 brd 10.9.8.255 scope global ens160 valid_lft forever preferred_lft forever inet 10.9.8.46/24 scope global secondary ens160 valid_lft forever preferred_lft forever
4) Delete IP address on interface
You can also use the same procedure to delete an IP address by just replacing 'add' with 'del':
$ sudo ip addr del 192.168.0.193/24 dev wlan0
5) Show Routing Table
Some computers have multiple default gateways and static gateways, to display default gateway and static gateway information ip route. Basically, it will show the current routing table:
$ ip route show
To show the route taken for an IP address, run the following command:
$ ip route get 10.42.0.47
To get the default gateway IP address, run the command:
$ ip route | grep default
6) How to add static route
You can use ip command to add static route on Linux system by running the following command:
$ sudo ip route add 10.9.7.0/24 via 10.9.8.100 dev ens160
To verify the command, run:
$ ip r default via 10.9.8.100 dev ens160 proto static 10.9.7.0/24 via 10.9.8.100 dev ens160 10.9.8.0/24 dev ens160 proto kernel scope link src 10.9.8.40 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
In order to add the default gateway, run the command:
$ sudo ip route add default via 192.168.0.196
7) Delete a route
If you want to delete a route from the routing table, you can run the following command:
$ sudo ip route delete 10.9.7.0/24 via 10.9.8.100
Moreover, in order to delete the default gateway, run the below command:
$ sudo ip route delete default
8) Show network statistics
The ip command can also be used to show the statistics of the various network interfaces. To do this you can use the ip command with the option
-s and then specify the network device.
$ ip -s link
When you need to get information about a particular network interface, add the option
ls followed by the name of the network interface. The option
-s when used more than once gives you more information about that particular interface. This can be very useful especially when troubleshooting errors in network connectivity.
$ ip -s -s link ls p2p1
9) ARP entries
With ip command you can view the MAC address of the devices connected in your LAN by using the option neigh or neighbour.
$ ip neighbour
6) Monitor netlink messages
It is also possible to view netlink messages with ip command. The monitor option allows you to see the state of your network devices.
For instance, a computer on your LAN could be categorized as REACHABLE or STALE depending on its status. The command can be used as follows:
$ ip monitor all
7) Enable and Disable Network Interface
To activate a particular interface you can use the ip command with options up and down, almost similar to how ifconfig is used.
In this example you can see the routing table entry when the ppp0 interface is activated and after it is deactivated and activated once again. The interface can be wlan0 or eth0. Change ppp0 to whatever interface is available in your case.
$ sudo ip link set ppp0 down
$ sudo ip link set ppp0 up
8) Enable TCP delayed ACK
From RHEL 7.1 iproute package added support for TCP Delayed ACK. Use below command to enable it
$ ip route quickack
9) Change the MTU on interface
MTU stands for Maximum Transmission Unit - the largest size of the data can be transmitted through a network interface. Assuming that you want to change the MTU of network interface ens160 to 8000, run:
$ sudo ip link set mtu 8000 dev ens160
Verify that the ip command has successfully changed the MTU of ens160:
$ ip -4 addr list ens160 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 8000 qdisc mq state UP group default qlen 1000 inet 10.9.8.40/24 brd 10.9.8.255 scope global ens160 valid_lft forever preferred_lft forever
10) IP command Help
In case you get stuck and do not know how a particular option works you can use the help option. The man page does not give a lot of information on how to use the ip options and this is where help comes to the rescue.
For example, to know more about the route option:
$ ip route help
In this tutorial, we learned how to use Linux IP command to display and configure networking in Linux systems. It's really a powerful tool for any Linux administrator/user.
The ip command cheat sheet shows net-tools vs iproute2 commands.
Thanks for reading and please leave your suggestion in the below comment section.