IP command from IPROUTE2

ip command in Linux is used to show / manipulate routing, devices, policy routing and tunnels. It is a provided by iproute2 package. ifconfig command which is provided by net-tools package has been around since long and will stay around. ip is more powerful and modern and will eventually replace it. This blog shows some example and powerful use of ip command

IP QUERIES

Display IP Addresses and property information

Show information for all addresses

root@bcf62d10644f:~# ip addr  
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1  
    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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default  
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link 
       valid_lft forever preferred_lft forever

Display information only for device eth0

root@bcf62d10644f:~# ip addr show dev eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default  
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link 
       valid_lft forever preferred_lft forever

Manage and display the state of all network interfaces

Show information for all interfaces

root@bcf62d10644f:~# ip link  
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1  
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff

Display information only for device eth0

root@bcf62d10644f:~# ip link show dev eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff

Display interface statistics

root@bcf62d10644f:~# ip -s link  
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1  
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    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      
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    648        8        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    648        8        0       0       0       0

Display and alter the routing table

List all of the route entries in the kernel

root@bcf62d10644f:~# ip route  
default via 172.17.0.1 dev eth0  
172.17.0.0/16 dev eth0  proto kernel  scope link  src 172.17.0.3  

Manage and display multicast IP addresses

Display multicast information for all devices

root@bcf62d10644f:~# ip maddr  
1:    lo  
    inet  224.0.0.1
    inet6 ff02::1
    inet6 ff01::1
2:    eth0  
    link  33:33:00:00:00:01
    link  01:00:5e:00:00:01
    link  33:33:ff:11:00:03
    inet  224.0.0.1
    inet6 ff02::1:ff11:3
    inet6 ff02::1
    inet6 ff01::1

Display multicast information for device eth0

ip maddr show dev eth0  

Show neighbour objects; also known as the ARP table for IPv4

Display neighbour objects

ip neigh  

Show the ARP cache for device em1

ip neigh show dev em1  

MODIFYING ADDRESS AND LINK PROPERTIES

Add an address

Add address 192.168.1.1 with netmask 24 to device em1

ip addr add 192.168.1.1/24 dev em1  

Delete an address

Remove address 192.168.1.1/24 from device em1

ip addr del 192.168.1.1/24 dev em1  

Alter the status of the interface

Bring em1 online

ip link set em1 up  

Bring em1 of offline

ip link set em1 down  

Set the MTU on em1 to 9000

ip link set em1 mtu 9000  

Enable promiscuous mode for em1

ip link set em1 promisc on  

ADJUSTING AND VIEWING ROUTES

Add an entry to the routing table

Add a default route (for all addresses) via the local gateway 192.168.1.1 that can be reached on device em1

ip route add default via 192.168.1.1 dev em1  

Add a route to 192.168.1.0/24 via the gateway at 192.168.1.1

ip route add 192.168.1.0/24 via 192.168.1.1  

Add a route to 192.168.1.0/24 that can be reached on device em1

ip route add 192.168.1.0/24 dev em1  

Delete a routing table entry

Delete the route for 192.168.1.0/24 via the gateway at 192.168.1.1

ip route delete 192.168.1.0/24 via 192.168.1.1  

Replace, or add if not defined, a route

Replace the defined route for 192.168.1.0/24 to use device em1

ip route replace 192.168.1.0/24 dev em1  

Display the route an address will take

Display the route taken for IP 192.168.1.5

ip route get 192.168.1.5  

MANAGING THE ARP TABLE

Add an entry to the ARP Table

Add address 192.168.1.1 with MAC 1:2:3:4:5:6 to em1

ip neigh add 192.168.1.1 lladdr 1:2:3:4:5:6 dev em1  

Invalidate an entry

Invalidate the entry for 192.168.1.1 on em1

ip neigh del 192.168.1.1 dev em1  

Replace, or adds if not defined, an entry to the ARP table

Replace the entry for address 192.168.1.1 to use MAC 1:2:3:4:5:6 on em1

ip neigh replace 192.168.1.1 lladdr 1:2:3:4:5:6 dev em1  

Like it? Click here to Tweet your feedback

Swapnil Jain

RHCA Level IX, RHCI ♦ Solution Architect ♦ DevOps Trainer & Consultant