Linux Commands

How to use the IP command in Ubuntu

IP (Internet Protocol) acts as a unique identifier address over the network. This unique identifier helps to send information among different devices on the network. Internet services also work based on IP and differentiate between several devices such as routers, computers, etc. IP addresses consist of four separated integer strings, the range of each string may vary from 0 to 255 (0.0.0.0 to 255.255.255.255).

IP commands in Ubuntu can be used for several purposes within a network, such as to assign, delete, set up an address, or configure parameters for network interfaces. One can say that it is an alternative to the ifconfig command of Ubuntu, but it is quite useful and commonly used nowadays because the ifconfig command has not been maintained for so long and, therefore, has been deprecated. This guide will be focused on the usage of the IP command in Ubuntu:

let’s start with the syntax of the IP command:

Syntax:

ip [ OPTIONS ] OBJECT { COMMAND | help }

Before digging into this tutorial; execute the following command in your terminal to get the commands and arguments related to IP:

$ ip help

Text Description automatically generated

How to check IP addresses of all network interfaces

You can check the IP addresses of the network interfaces with the help of the command given below. For this, we will use “address” OBJECT of the IP command:

$ ip address

Text Description automatically generated

You will get the same output if you use “addr” instead of “address” in the above command.

It is to notice that the IP commands can be used with various options to get the output of the specific command in the required manner.

For instance, if you want to get the IP addresses of all network interfaces in a colorful manner, you have to use the “-c” option as shown below:

$ ip -c address

Text Description automatically generated

How to get only IPv4 or IPv6 addresses of network interfaces

You can get only IPv4 or IPv6 addresses by using the command stated below; you will have to use the “-4” option for IPv4 and the “-6” option for IPv6.

$ ip -4 address

Text Description automatically generated

Or:

$ ip -6 address

Text Description automatically generated

How to get the link layer information of network interfaces

The data link layer acts at the lowest position in the TCP/IP, and it defines the methods inside a local network link on which hosts communicate. IP command can be used to fetch the link layer information of all network interfaces or one as per your requirement. You have to use the “link” keyword as shown below:

$ ip link

Text Description automatically generated

The above command has displayed the data link layer information of all network interfaces; moreover, if you want to get for a specific interface, you have to specify the name of that interface as shown below:

$ ip link show enp0s3

Text Description automatically generated

Furthermore, you can get the statistics of the link layer by using the “-s” option of the IP command as shown below:

$ ip -s link

Text Description automatically generated

How to assign an IP address to an interface using the IP command

You can use the IP command to assign a specific ip address to the network interface; the command given below will assign IP address (192.168.10.1/10) to interface “lo” (“lo” refers to the address that the computer uses to communicate with itself ): it is noticed that the following command won’t execute until sudo permissions are not granted:

$ sudo ip address add 192.168.10.1/10 dev lo

To notice the changes, you must run the command:

$ ip address show

Or:

$ ip address

Text Description automatically generated

How to remove an IP address of a network interface using the IP command

The IP command can be utilized to remove the IP address of a network interface. Let’s say we want to remove IP address “192.168.10.1/10” of interface “lo”, you have to use the “del” keyword with IP command as shown below:

$ sudo ip addr del 192.168.10.1/10 dev lo

You must get the IP addresses after the above command to check that IP address has been removed or not:

Text Description automatically generated

How to enable or disable network interface using IP command

With the help of the IP command, one can enable or disable any of the network interfaces available:

For instance, to disable “enp0s3”, use the following command:

$ sudo ip link set enp0s3 down

Text Description automatically generated

Or to enable the “enp0s3” network interface, execute the command stated below, and you will notice that the state of “enp0s3” has been revived again:

$ sudo ip link set enp0s3 up

Text Description automatically generated

How to monitor Network Interfaces using IP command

One of the major benefits of the IP command is to monitor the state, routes, and addresses of the devices. Execute the following command to monitor your network; it will keep on monitoring until you break the session by getting out of the command (Ctrl+c):

$ ip monitor

A picture containing text, screenshot Description automatically generated

The “STALE” and “REACHABLE” represent the availability of Network Interfaces; both are valid, but the “STALE” state is unreachable. However, “PROBE” lies in between reachable and unreachable states; the “PROBE” indicates that the interface would soon be reachable.

Conclusion

The IP command in Ubuntu is a well-known command for network handling. Starting from the introduction of IP to the detailed use of this command in Ubuntu, it can be used to perform several network-related tasks such as monitoring the network devices, adding/removing the IP address of interfaces. Earlier, the ifconfig command was being used, but due to lack of maintenance, it has been deprecated, and therefore it is replaced by an IP command. I hope this article would be beneficial for you, and if you want a more detailed guide about commands related to networking, you must visit linuxhint.

About the author

Adnan Shabbir