Linux IP command with examples

IP command is similar to ifconfig, which is very familiar for assigning Static IP Address, Route, Default Gateway, and  configure network interface parameters on Linux system.

IP command replaces the ifconfig command on modern Linux distributions because it does not support InfiniBand addresses. However it is still available on most Linux distributions.

IP command utility bundled with ‘iproute2’ package. By default iproute2 utility pre-installed in most of the major Linux distributions. If no, you can install it by issuing iproute2 on your terminal with help of your system package manager.

Be careful when using the IP command on a remote Linux system, because if you make a mistake you will lose the connection with the server.

By default, the changes you make through the IP command will not be permanent until you update the parameters in the configuration file. If you do not make them permanent the system will not retain changes when the system restarts.

In this guide, we’ll show you how to use the IP command when configuring the network interface in Linux.

IP command general syntax as follows:

Syntax:
$ ip OBJECT COMMAND
$ ip [options] OBJECT COMMAND
$ ip OBJECT help
$ ip [options] OBJECT COMMAND [parameters]

Before seeing the IP command usage, you should understand the IP command ‘OBJECTS’ and their purpose so, that the right option can be used in the right place.

OBJECTAbbreviated FormDescription
linklDisplay and modify network interfaces
addressa, addrDisplay and modify IP Addresses
addrlabeladdrlLabel configuration for protocol address selection
neighbourn, neighARP or NDISC cache entry
routerDisplay and alter the routing table
rule ruRule in routing policy database
maddressm, maddrMulticast address
mroutemrMulticast routing cache entry
tunneltTunnel over IP

Make a note: When configuring network interfaces, you must execute the ip command as root or user with sudo privileges, otherwise you will get an error.

Display information about all network interfaces

To display a list of available all network interfaces (active or inactive) and the associated ip address, run:

It shows many information for each interface not only IP address.

$ ip a
or
$ ip addr
or
$ ip address
or
$ ip addr show

1: lo:  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: eth0:  mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
     link/ether c8:5b:76:4d:d4:5c brd ff:ff:ff:ff:ff:ff
 3: wlan0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
     link/ether e4:a7:a0:32:fc:e9 brd ff:ff:ff:ff:ff:ff
     inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlan0
        valid_lft 2786sec preferred_lft 2786sec
     inet6 2402:3a80:569:5fbd:d9d1:87d8:dac7:75c5/64 scope global temporary dynamic 
        valid_lft 3587sec preferred_lft 3587sec
     inet6 2402:3a80:569:5fbd:6be2:c0c4:6e5e:d29b/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 3587sec preferred_lft 3587sec
    inet6 fe80::e5e8:a342:f161:a8d4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

You can find the following information from the above output.

  • It shows list of available interfaces (including active and inactive) on the system.
  • IP address (IPv4 and IPv6), MAC address, subnet mask, and mtu value of the interface.
  • The interface has either a static IP or a dynamic IP. If it’s configured via DHCP (Dynamic IP), the interface output displays the word "dynamic".
  • You can determine if the interface is Ethernet or WiFi using the naming convention standard. Ethernet interfaces always start with the letter 'e' whereas Wi-Fi interfaces always start with the letter 'w'.

Show information about a specific network interface

To display information about a specific network interface, enter the device name with the IP command. For instance, to view 'wlan0', you would run. Similarly, you can view any interface information.

$ ip addr show eth0
or
$ ip addr list eth0
or
$ ip addr show dev eth0

3: wlan0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether e4:a7:a0:32:fc:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlan0
       valid_lft 2782sec preferred_lft 2782sec
    inet6 2402:3a80:569:5fbd:d9d1:87d8:dac7:75c5/64 scope global temporary dynamic 
       valid_lft 3583sec preferred_lft 3583sec
    inet6 2402:3a80:569:5fbd:6be2:c0c4:6e5e:d29b/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 3583sec preferred_lft 3583sec
    inet6 fe80::e5e8:a342:f161:a8d4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Show only information about IPv4 or IPv6

If you only want to view 'IPv4' or 'IPv6' information, you can do this by using the following options with the ip command:

To list the IPv4 addresses, run:

$ ip -4 addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
3: wlan0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlan0
       valid_lft 2403sec preferred_lft 2403sec

To list the IPv6 addresses, run:

$ ip -6 addr
1: lo:  mtu 65536 state UNKNOWN qlen 1000
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
3: wlan0:  mtu 1500 state UP qlen 1000
    inet6 2402:3a80:569:5fbd:d9d1:87d8:dac7:75c5/64 scope global temporary dynamic 
       valid_lft 3569sec preferred_lft 3569sec
    inet6 2402:3a80:569:5fbd:6be2:c0c4:6e5e:d29b/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 3569sec preferred_lft 3569sec
    inet6 fe80::e5e8:a342:f161:a8d4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Assigning a Static IP Address to an Interface

The ip command can be used to assign IP addresses to an interface. Use the following syntax to assign a IP address to a specific interface.

Syntax:
ip addr add address dev ifname

address: The IP address you want to assign to the interface, which must be included with netmask.
ifname : Specifies the interface from which you want to add the IP.

For instance, to add address 10.20.0.15 with netmask 24 to device ‘eth1’, you would run:

$ sudo ip addr add 10.20.0.15/24 dev eth1

Configuring multiple IP addresses

You can assign multiple IP addresses to the same interface without using the alias interface as shown below. The same IP command can be used multiple times to assign multiple addresses to a single interface.

$ sudo ip address add 10.20.0.15/24 dev eth1
$ sudo ip address add 10.20.0.16/24 dev eth1

You can see the output similar to the below:

$ ip -4 addr show eth0

2: eth0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 10.20.0.15/24 brd 10.20.0.255 scope global dynamic eth1
       valid_lft 3569sec preferred_lft 3569sec
    inet 10.20.0.16/24 scope global secondary eth1
       valid_lft forever preferred_lft forever

Remove an IP address from the interface

The following IP command syntax can be used to remove an IP address from a specific interface.

Syntax:
ip addr del ADDRESS dev IFNAME

For example: To remove address ‘10.20.0.15/24’ from device ‘eth1’, run:

$ sudo ip addr del 10.20.0.15/24 dev eth1

Enable the Network Interface

To bring interfaces up or down, use the following IP command syntax:

Syntax:
ip link set dev [DEVICE] [up]

For instance, to bring up the interface ‘eth1’ online, run:

$ sudo ip link set dev eth1 up

Bring down the Network Interface

Use the following IP command syntax to bring down the interfaces.

Syntax:
ip link set dev [DEVICE] [down]

For instance, to bring down the interface ‘eth1’ offline, run:

$ sudo ip link set dev eth1 down

Changing the txqueuelen of the device

IP command can be used to set the length of the transmit queue of the device as shown below:

Syntax: ip link set txqueuelen [NUMBER] dev [DEVICE]

Changing the MTU of the device

You can set maximum transmission units (MTU) for better network performance as shown below:

Syntax: ip link set mtu [NUMBER] dev [DEVICE]

Display and alter the routing table

IP command can be used to alter and view routing table information on the Linux system using the ‘route’ object.

Display routing table

Run one of the following command to check the routing table information of the system.

$ ip r
or
$ ip route
or
$ ip route show
or
$ ip route list

default via 192.168.43.1 dev wlan0 proto dhcp metric 600 
 192.168.43.0/24 dev wlan0 proto kernel scope link src 192.168.43.4 metric 600 

To make the gateway permanent, you must add the gateway value to the interface configuration file or update the interface routing configuration file, as mentioned below:

All network interface related configuration files can be found at ‘/etc/sysconfig/network-scripts/’.

  • ifcfg-enp0s3 – Network interface configuration file of enp0s3 interface.
  • route-enp0s3 – Create a new file with ‘route-IFNAME’ then add a route information.

Adding a new route

To add a route via gateway, use the following syntax:

Syntax:
ip route add [network/mask] via [gatewayIP]

Add a new route to 10.20.0.0/24 via the gateway at 10.20.0.1, run:

$ sudo ip route add 10.20.0.0/24 via 10.20.0.1

To add a route through a device (network interface), use the following syntax:

Syntax:
ip route add [network/mask] dev [IFNAME]

Add a new route to 10.20.0.0/24 that can be reached on device eth1.

$ sudo ip route add 10.20.0.0/24 dev eth1

You must use the keyword ‘default’ in the IP route command to make the route as default. For example, to add a default route via the local gateway 10.20.0.1 that can be reached on device eth1, run:

$ sudo ip route add default via 10.20.0.1 dev eth1

Delete a route

Use the following command to delete default gateway.

$ sudo ip route del default

Use the following syntax to delete the route added via gateway IP.

Syntax:
ip route del [network/mask] via [gatewayIP]

$ sudo ip route del 10.20.0.0/24 via 10.20.0.1

Use the following syntax to remove the route added through a device (network interface).

Syntax:
ip route del [network/mask] dev [IFNAME]

$ sudo ip route del 10.20.0.0/24 dev eth1

Show Network Statistics

IP command is not limited to do IP manipulation, also shows network statistics, Use the following command to check network statistics for all interfaces.

$ ip -s link

1: lo:  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 overrun mcast   
    46440      448      0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    46440      448      0       0       0       0       
2: eth0:  mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether c8:5b:76:4d:d4:5c 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       
4: wlan0:  mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether e4:a7:a0:32:fc:e9 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    43779183   60701    0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    12447326   54365    0       0       0       0       

Show specific Network interface Statistics

Add the desired network interface name to Show specific Network interface Statistics.

$ ip -s link ls wlan0

4: wlan0:  mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether e4:a7:a0:32:fc:e9 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    43782833   60729    0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    12452566   54396    0       0       0       0       

Closing Notes

In this guide, we’ve shown you several IP command examples to add, delete and modify IP address, routing table & other information in Linux.

If you have any questions or feedback, feel free to comment below.

Leave a Reply

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