try.directtry.direct
Article preview image

IP command In Linux With Usage Examples | Guide 2022

Many Linux users have used the 'ifconfig' command as a primary tool for obtaining network information. However, this command is now obsolete, and we now have the 'IP' command in its place. The 'IP' command has many features compared to the old 'ifconfig' command. Today, you will learn how to use it through practical examples


IP command in Linux: usage examples

The IP command is part of the iproute2 package. This package is present in all modern Linux distributions and is easy to use.


The general syntax of the IP command is as follows:


ip [OPTIONS] OBJECT {COMMAND | help}

In this syntax, we must know that `OBJECT` refers to what we want to configure with the IP command. Some of these are:

link (l): For the configuration of network interfaces.

address (a): edit and display IP addresses.

route (r): For editing routing tables.

neigh (n): View and configure (ARP).


You can get help with each of these objects, for example:


ip link help

Or


ip address help

So, let's start working with the IP command.


1. Show the IP address of the network interfaces

One of the most common uses of the IP command is to display the IP address of network interfaces. To achieve this, run the following command:


ip addr show

Output:


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: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
link/ether 54:e1:ad:6d:9f:49 brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether cc:2f:71:e9:45:4e brd ff:ff:ff:ff:ff:ff
inet 192.168.51.232/24 brd 192.168.51.255 scope global dynamic noprefixroute wlp2s0
valid_lft 2359sec preferred_lft 2359sec
inet6 2401:4900:1954:ff53:57cd:d9d1:ed2a:282b/64 scope global dynamic noprefixroute
valid_lft 3507sec preferred_lft 3507sec
inet6 fe80::9efe:aa76:2250:d18b/64 scope link noprefixroute
valid_lft forever preferred_lft forever

There you will have information about each of the network interfaces. The output shows the IP address, broadcast, status, and many more things.

You can also choose the network interface to display.


ip addr show dev [interface]

For example:


ip addr show dev eth0

You will get the same information as retrieved by the previous command but limited to the interface.


2. Assign an IP address to a network interface

Execute the following syntax to assign an IP address to a network interface of the computer:


ip addr add [address] dev [interface]

For example,


ip addr add 192.168.1.51/24 dev eth0

3. Remove an IP address assigned to a network interface

To revert to the previous step, you have to run:


ip addr del 192.168.1.51/24 dev eth0

It is easy to revert the changes this way.


4. Enable and disable a network interface

Thanks to the IP command, you can easily enable a new network interface. To complete this, you have to run:


ip link set [interface] up

For example:


ip link set enp1s0 up

And to disable it you can run:


ip link set [interface] down

An example of this is:


ip link set enp1s0 down

5. Display the system routing table

To display the routing table that exists on the system using the IP command, you have to run the following command:


ip route show

Output:
default via 192.168.1.1 dev enp0s3 proto dhcp metric 100
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.21 metric 100

Moreover, it is possible to show the route of a single IP address. Execute the following command to do this:


ip route get [ip address]

For example:


ip route get 192.168.1.1

6. Add and remove a static route with the IP command


We can also add a static route manually, the same as with the IP address. To achieve this, you have to run the following command:


ip route add [route] via [ip-address] dev [interface]

For example:


ip route add 192.168.1.0/24 via 192.168.1.1

To remove a previously made route, just run the following command:


ip route delete [route] via [ip-address] dev [interface]

For example:


ip route delete 192.168.1.0/24 via 192.168.1.1

It's that quick and easy.


7. Show network statistics

The IP command is also frequently used to obtain statistics on network performance. To display them, you have to run.


ip -s link

Output:
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
RX: bytes packets errors dropped missed mcast
1184 16 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1184 16 0 0 0 0
2: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether 54:e1:ad:6d:9f:49 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped missed mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
link/ether cc:2f:71:e9:45:4e brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped missed mcast
37996487 50128 0 0 0 0
TX: bytes packets errors dropped carrier collsns
15040574 39681 0 0 0 0

The information you will see is about sent and received packets, sorted by the network interface.

As you can see, the IP command is very useful when applied in practice. Some of its featuresmake it a worthy successor of ifconfig.