Administration toolsCommands

How to use Linux IP Command

The Linux IP command is similar to ifconfig, but more powerful and is intended to be a replacement for it. You can perform several network administration tasks using the IP command. Also, ifconfig is one of the deprecated commands that has not been maintained for many years even though it is still available on most Linux distributions. The IP command line utility comes with the iproute2 suite utility, and most Linux distributions will come with the iproute2 tools pre-installed.
In this article we will show you how to assign Static IP Address, Static Route, Default Gateway and Assigning IP Address on demand using IP command.

Syntax

$ ip OBJECT COMMAND
$ ip [options] OBJECT COMMAND
$ ip OBJECT help
Use IP command
Use IP command

To get information about each object use help command as follows:

$ ip OBJECT help
$ ip OBJECT h
$ ip a help
$ ip r help

Enable and Disable Network Interface

To enable network interface (in my case wlan0) use following command:

$ sudo ip link set wlan0 up

If you want to disable the network interface wlan0, run the following command:

$ sudo ip link set wlan0 down

Setting and Deleting an IP Address

To set an IP address for your computer, the IP command can be used as follows:

$ sudo ip addr add 192.168.1.8/24 dev wlan0

or

$ sudo ip a add 192.168.1.8/255.255.255.0 dev wlan0

After you have set the IP address confirm with show, whether the changes have taken effect.

$ ip addr show wlan0

Example output:

3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:25:00:3d:e1:aa brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.8/24 brd 192.168.1.255 scope global wlan0
       valid_lft forever preferred_lft forever

If you want to delete an IP address by just replacing add with del flag:

$ sudo ip a del 192.168.1.8/24 dev wlan0

Show Routing Table

You can use the route object of the IP command to see the route packets will take in your network as set in your routing table. Run the following command to check the routing table information of the system.

$ sudo ip route show

example:

$ sudo ip route show
default via 192.168.1.1 dev wlan0  proto static
...
192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.8  metric 9 
...

To change the default route, the IP command can be used as follows:

$ sudo ip route add default via 192.168.1.1

You can also delete default gateway using the following command:

$ sudo ip route del default via 192.168.1.1

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
$  ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast   
    2610322    27323    0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    2610322    27323    0       0       0       0      
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:25:00:d6:e4:bc brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        0       0       0       0      
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DORMANT group default qlen 1000
    link/ether 00:25:00:3d:e1:aa brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    547244369  499056   0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    56533331   382167   6       0       0       0      
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 52:54:00:52:8f:04 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    1156       28       0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    527        2        0       0       0       0      
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 500
    link/ether 52:54:00:52:8f:04 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        0       0       0       0      
7: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 4e:23:40:bf:89:88 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        0       0       0       0  

If you need to get information about a particular network interface, add the option ls followed by the name of the network interface (wlan0). This can be very useful, especially when troubleshooting errors in network connectivity.
To do this, run the following command:

$ ip -s link ls wlan0
$ ip -s link ls wlan0
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DORMANT group default qlen 1000
    link/ether 00:25:00:3d:e1:aa brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    547837598  500388   0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    56939474   383500   6       0       0       0      

ARP Entries

Address Resolution Protocol (ARP) is used to translate an IP address to its corresponding physical address, commonly known as MAC address. 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

$ ip neighbour
192.168.1.1 dev wlan0 lladdr 2c:95:7f:4c:c2:d6 REACHABLE

Conclusion

The IP command is a must have tool for network administrators and all Linux users alike. It is time to move from ifconfig, especially when you are writing scripts.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close
Close