11 ip Command Examples For Linux

The ‘ip’ command is used to print out various network information in Linux. It replaces the deprecated ‘ifconfig’ command, which is not even installed by default in CentOS 7.

The ip command is part of the iproute package, which is installed by default in most modern Linux distributions.

In the examples here you’ll see how to use the ip command to show network configuration such as link information, IP addresses and routing.

How To Use ip – Command Examples

  • 1. Show IP Addresses

    We can show IP address information for all interfaces by running ‘ip address show’ as shown below.

    [root@centos7 ~]# ip address show
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
        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: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:dd:cd:6a brd ff:ff:ff:ff:ff:ff
        inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736
           valid_lft 1576sec preferred_lft 1576sec
        inet6 fe80::20c:29ff:fedd:cd6a/64 scope link
           valid_lft forever preferred_lft forever
    

    In this exammple we see information for the loopback interface as well as eno16777736. Also note that with ip commands we can shorten them as long as the components provided are still unique, so for example ‘ip address show’ can be shortened to ‘ip a’ which will display the same information and is faster to type.

  • 2. Show IP Link

    Link information can be shown with ‘ip link show’, which can be shortened to ‘ip l’.

    [root@centos7 ~]# ip l
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:dd:cd:6a brd ff:ff:ff:ff:ff:ff
    
  • 3. Show IP Routing Table

    We can print out routing information with the ‘ip routing show’ command, which can be shortened to ‘ip r’.

    [root@centos7 ~]# ip r
    default via 192.168.220.2 dev eno16777736  proto static  metric 100
    192.168.220.0/24 dev eno16777736  proto kernel  scope link  src 192.168.220.135  metric 100
    
  • 4. View Statistics

    We can run many ip commands with the -s option to display various statistics. In the below example we use -s with ‘ip address show’ which displays additional stats.

    [root@centos7 ~]# ip -s a
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
        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
        RX: bytes  packets  errors  dropped overrun mcast
        666        6        0       0       0       0
        TX: bytes  packets  errors  dropped carrier collsns
        666        6        0       0       0       0
    2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:dd:cd:6a brd ff:ff:ff:ff:ff:ff
        inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736
           valid_lft 1431sec preferred_lft 1431sec
        inet6 fe80::20c:29ff:fedd:cd6a/64 scope link
           valid_lft forever preferred_lft forever
        RX: bytes  packets  errors  dropped overrun mcast
        495701     5144     0       0       0       0
        TX: bytes  packets  errors  dropped carrier collsns
        2262117    4096     0       0       0       0
    
  • 5. Print IPv4 Only

    With the -4 option we can print out information relating to IPv4 only.

    [root@centos7 ~]# ip -4 a
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736
           valid_lft 1267sec preferred_lft 1267sec
    
  • 6. Print IPv6 Only

    Likewise we can use the -6 option to only print out information relating to IPv6.

    [root@centos7 ~]# ip -6 a
    1: lo:  mtu 65536
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: eno16777736:  mtu 1500 qlen 1000
        inet6 fe80::20c:29ff:fedd:cd6a/64 scope link
           valid_lft forever preferred_lft forever
    
  • 7. Single Line Output

    We can print output to a single line with the -o option, this way the printed output will not take up multiple lines.

    [root@centos7 ~]# ip -o a
    1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
    1: lo    inet6 ::1/128 scope host \       valid_lft forever preferred_lft forever
    2: eno16777736    inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736\       valid_lft 1128sec preferred_lft 1128sec
    2: eno16777736    inet6 fe80::20c:29ff:fedd:cd6a/64 scope link \       valid_lft forever preferred_lft forever
    

    This is just a different way of formatting the same information.

  • 8. Modify Interface Temporarily

    While the ip command can be used to edit the network configuration of an interface, it is generally not recommended as it’s not a persistent change, meaning that it will not survive a reboot. However despite this there may be times where you want to temporarily set an IP address for the current session, which can be done with the ip command.

    [root@centos7 ~]# ip address add 192.168.220.200 dev eno16777736
    

    This has added the second IP address 192.168.220.200 to the eno16777736 device.

    [root@centos7 ~]# ip -o -4 addr show dev eno16777736
    2: eno16777736    inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736\       valid_lft 1720sec preferred_lft 1720sec
    2: eno16777736    inet 192.168.220.200/32 scope global eno16777736\       valid_lft forever preferred_lft forever
    

    This configuration change will be lost after a system reboot.

    For further information on this, you can simply type ‘ip addr add help’ which will show you the syntax to use and what else you can modify. To modify the routing take a look at ‘ip route add help’.

  • 9. Bring Interface Up Or Down

    We can use the ip command to take a network link down or bring it back up, as shown below.

    ip link set eno16777736 down
    ip link set eno16777736 up
    

    This will take eno16777736 down, and then bring it back up. Note that this should be run from a console rather than through an SSH session, as you will lose network connectivity instantly once the link is taken down.

  • 10. Read Commands From File

    Rather than specifying options or objects as part of the ip command each time, we can read in from a batch file with the -b option followed by the path to the file.

    In the below example we can see the batch.txt file contains two ip commands, when run with -b we get the output of both ‘ip addr show’ followed directly by ‘ip route show’.

    [root@centos7 ~]# cat batch.txt
    addr show
    route show
    
    [root@centos7 ~]# ip -b batch.txt
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
        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: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:dd:cd:6a brd ff:ff:ff:ff:ff:ff
        inet 192.168.220.135/24 brd 192.168.220.255 scope global dynamic eno16777736
           valid_lft 1667sec preferred_lft 1667sec
        inet6 fe80::20c:29ff:fedd:cd6a/64 scope link
           valid_lft forever preferred_lft forever
    default via 192.168.220.2 dev eno16777736  proto static  metric 100
    192.168.220.0/24 dev eno16777736  proto kernel  scope link  src 192.168.220.135  metric 100
    
  • 11. Display Version

    We can display the version information for the ip tool with the -V option.

    [root@centos7 ~]# ip -V
    ip utility, iproute2-ss130716
    

Summary

We have seen how the ip command can be used to both display and modify network configuration in Linux.

Going forward we recommend using the ip command over ifconfig as it’s intended to replace it, so it is therefore worth learning and knowing how to use the ip command.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>