How to use the IP command in Ubuntu?

The “ip” command refers to the built-in “Internet Protocol” command line tool that assigns addresses to the interfaces and configures their various parameters. It is also beneficial to bring the interface states up(online) and down(offline) and assign or remove the associated IP addresses. 

It is the replacement of the “ifconfig” command. The “ifconfig” command is used in the older version of the Linux system, having a very limited range of functionalities, the “ip” command. 

This guide will provide a deep insight into the usage of the “IP” command in Ubuntu.

How to Use the ip Command in Ubuntu?

The “IP” command configures the network interfaces. For this purpose, it depends on its basic syntax that is typed below:

Syntax:

$ ip [ OPTIONS ] OBJECT { COMMAND | help }

The syntax holds the following essential parameter:

  • Ip: The main keyword that denotes the “ip” command.
  • OPTIONS: Represent the associated options/flags of the “ip” command.
  • OBJECT: It includes the most frequent object’s type like “links(l)”, “address(a)”, “route(r)”, and “neigh(n)”.

To see “ip” tool commands and object types details, execute the following command in the terminal(Ctrl+Alt+T):

$ ip --help

Now move on to the practical implementation of the “ip” command with the help of various examples.

Example 1: Get IP Addresses of All Network Interfaces

To get all the network devices and their associated IP addresses execute the “ip” command followed by the object type “address(a)”. Its main purpose is to display and modify the IP addresses: 

$ ip address

The same output can be achieved by using the “addr or a” flag of the “ip” command. In the above output, it is difficult to identify the IP addresses associated with the network interfaces. 

If the user uses the “-c” flag with the “ip” command, then it will highlight the IP addresses in different colors:

$ ip -c address

The “IP” addresses have been highlighted in the above output and are easy to identify.

Example 2: View the Specified Network Interface Addresses

Instead of getting all network interfaces IP addresses, the user can also check the individual network IP address. 

In this example, the “IPv6” and the “IPv4” address is displayed by typing “-4” for “IPv4” and the “-6” for “IPv6” with the “ip” command:

IPv4 Address

$ ip -4 address

IPv6 Address

$ ip -6 address

Both of the “IPv4” and “IPv6” network addresses are shown in the output.

Example 3: Assign the IP Address

The “ip” command allows the user to assign the IP address to the specific network interface. 

Suppose assign an IP address “192.168.157.150/24” to the device “lo(an address that the system uses to communicate itself)” use the following command:

$ sudo ip address add 192.168.157.150/24 dev lo

The above command executed successfully and the output of the “ip address” command verifies that “192.168.157.150/24” has been assigned to an interface “lo”.

Note: It is to be noted that this command can’t execute without the sudo privileges or the super-user authentication.

Example 4: Remove the IP Address

Once the IP address is being set or added, it can be deleted/removed by typing the “del” keyword with the “ip” command in the following way:

$ sudo ip address del 192.168.157.150/24 dev lo

The above command has deleted the specified IP address for interface “lo”. For more verification of this action, execute the “ip” command with the interface “lo” and the “addr” as shown below:

$ sudo ip addr show dev lo

The output does not contain the “192.168.157.150” IP address for interface “lo”.

Example 5: Access All the Network Interfaces 

The “link(l)” object type refers to the network interfaces. Its main objective is to manage and look after the network interface’s status.

Suppose to check the information of all the available network interfaces uses the “ip” command with the keyword “link”:

$ ip link

Apart from that, the specified network interface information can also be achieved by typing a particular interface name in this way:

$ ip link show ens33

The output displays the link layer information of the specified interface “ens”

Example 6: Check the Network Interface Statistics

The “-s” flag displays the statistics of all/particular network interfaces. These statistics include errors, dropped packets, and transferred packets.

In this example, to see the statistical information of all the network interfaces, run the following “ip” command:

$ ip -s link

The above link layer statistics can also be achieved for the individual interface by typing the particular interface name like this:

$ ip -s link ls lo

The interface “lo” statistics have been displayed in the terminal.

Example 7:  Modify the State of Interface

The “ip” command line tool facilitates the user in changing the state of the available interfaces in Ubuntu. To do so, use the “set” command with the “ip” tool.

In this example, the interface “ens33” status is “UP” as shown below:

$ ip link show ens33

Now, to bring the “ens33” interface down (offline) execute the “ip” command in this way:

$ sudo ip link set ens33 down

The output confirms that the interface “ens33” state has been modified to “DOWN(offline)”.

Example 8: Get Route Table Entries

The “route” object is beneficial for viewing or modifying the routing table. For this purpose, use the “route” keyword with the “ip” command. 

In this example, all the route entries are displayed in the terminal in the following way:

$ ip route

The same output can be displayed by using the “list” option with the “ip route” command.

Example 9: View the Neighbor Entries

The “neigh” object is beneficial for displaying and altering the neighbor entries. To display the “neigh” list, run the following command:

$ ip neigh

In the above output, the “REACHABLE” shows that the “neighbor” is valid and can be easily reachable. However, it can be “STALE” or “DELAY”.

DELAY: Represent that the packet sending procedure is done, and wait for the confirmation at the kernel side.

STALE: Specifies that neighbor is valid but is not reachable

Conclusion

In Ubuntu, the “ip(Internet Protocol)” command configures the network interface objects, which include “link”, “address”, “route”, and “neigh”. All of them are accessed and altered by using their associated commands, such as “add”, “set”, “del”, and “show”. This guide enlists a large list of examples describing the “ip” command in Ubuntu.