Essential IP commands

We have started deploying RHEL 7 at work. I am using CentOS 7 at home. Yes, I can still install net-tools but iproute2 is the future. I want to default to using ip. These are my 15 essential ip commands.

1. Show all IP addresses
ip address show, similar to ifconfig, will display all IP addresses from all interfaces. It can be shortened in many ways. Similar to Cisco IOS and NX-OS, as long as you type the unique part of the command, it will work.

ip address show
ip addr show
ip addr
ip a s
ip a

[root@centos7 ~]# ip 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
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:e8:43 brd ff:ff:ff:ff:ff:ff
    inet 192.168.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1573sec preferred_lft 1573sec
    inet6 fe80::20c:29ff:fefc:e843/64 scope link
       valid_lft forever preferred_lft forever

Show IPv4 addresses
ip -4 a

[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.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1359sec preferred_lft 1359sec

Show IPv6 addresses
ip -6 a

[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:fefc:e843/64 scope link
       valid_lft forever preferred_lft forever

2. Show IP address for a single interface
This is similar to ifconfig
ip a s dev $device

[root@centos7 ~]# ip a s dev eno16777736
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:e8:43 brd ff:ff:ff:ff:ff:ff
    inet 192.168.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1151sec preferred_lft 1151sec

3. Show all links (interfaces)
ip link show

[root@centos7 ~]# ip link show
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:fc:e8:43 brd ff:ff:ff:ff:ff:ff

4. Show a specific link
ip link show dev $device

[root@centos7 ~]# ip link show dev lo
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

5. Show routing table
Similar to route -n or netstat -r

ip route show
ip route
ip r

[root@centos7 ~]# ip r
default via 192.168.76.2 dev eno16777736  proto static  metric 100
192.168.76.0/24 dev eno16777736  proto kernel  scope link  src 192.168.76.135  metric 100

6. Show neighbors
Similar to arp -a

ip neighbor show
ip neighbor
ip n

[root@centos7 ~]# ip n
192.168.76.254 dev eno16777736 lladdr 00:50:56:e7:bc:68 STALE
192.168.76.1 dev eno16777736 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.76.2 dev eno16777736 lladdr 00:50:56:f9:cb:1e STALE

7. Flush neighbors
ip n flush dev $device

[root@rtfmp ~]# ip n
192.168.76.254 dev eno16777736 lladdr 00:50:56:e3:e0:9e STALE
192.168.76.1 dev eno16777736 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.76.2 dev eno16777736 lladdr 00:50:56:f9:cb:1e STALE
[root@rtfmp ~]# ip n flush dev  eno16777736
[root@rtfmp ~]# ip n
192.168.76.254 dev eno16777736  FAILED
192.168.76.1 dev eno16777736 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.76.2 dev eno16777736  FAILED

8. Add IP Address
You assign an IP address to an interface by adding an IP to it. You can add many addresses to an interface. The first address will be used by outgoing traffic.
ip addr add $ip/$prefix dev $device

[root@centos7 ~]# ip addr add 192.168.76.136/24 dev eno16777736
[root@centos7 ~]# ip -4 a s dev  eno16777736
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1656sec preferred_lft 1656sec
    inet 192.168.76.136/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever

I am adding three addresses (or alias) to the interface. Both CIRD and dotted decimal notation will work.

[root@centos7 ~]# ip addr add 192.168.76.136/24 dev eno16777736
[root@centos7 ~]# ip addr add 192.168.76.137/24 dev eno16777736
[root@centos7 ~]# ip addr add 192.168.76.138/255.255.255.0 dev eno16777736
[root@centos7 ~]# ip -4 a s dev  eno16777736
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1505sec preferred_lft 1505sec
    inet 192.168.76.136/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.76.137/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.76.138/24 scope global secondary eno16777736
      valid_lft forever preferred_lft forever

9. Delete IP address from an interface
ip addr delete $ip/$prefix dev $device

[root@centos7 ~]# ip addr delete 192.168.76.138/24 dev eno16777736
[root@centos7 ~]# ip -4 a s dev  eno16777736
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.76.135/24 brd 192.168.76.255 scope global dynamic eno16777736
       valid_lft 1065sec preferred_lft 1065sec
    inet 192.168.76.136/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.76.137/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever

10. Delete all IP addresses
You don’t want to do this unless you have direct console access. I am going to add a second interface to demonstrate this.
ip address flush dev $device

[root@centos7 ~]# ip a s dev  eno33554984
3: eno33554984:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:e8:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.76.137/24 brd 192.168.76.255 scope global dynamic eno33554984
       valid_lft 1782sec preferred_lft 1782sec
    inet6 fe80::20c:29ff:fefc:e84d/64 scope link
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ip address flush  dev  eno33554984
[root@centos7 ~]# ip a s dev  eno33554984
3: eno33554984:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:e8:4d brd ff:ff:ff:ff:ff:ff

To delete just IPv4 or IPv6 address, use
ip -4 address flush dev
ip -6 address flush dev

11. Bring down an interface
ip link set dev $device down

[root@centos7 ~]# ip link set  dev  eno33554984 down
[root@centos7 ~]# ip link show  dev  eno33554984
3: eno33554984:  mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
    link/ether 00:0c:29:fc:e8:4d brd ff:ff:ff:ff:ff:ff

Note the state is DOWN.

12. Bring up an Interface
ip link set dev $device up

[root@centos7 ~]# ip link set  dev  eno33554984 up
[root@centos7 ~]# ip link show  dev  eno33554984
3: eno33554984:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:fc:e8:4d brd ff:ff:ff:ff:ff:ff

Note the state is UP.

13. Add default route
ip route add default via $gateway_ip
ip route add default dev $device

[root@rtfmp ~]# ip route add default via 192.168.76.1
[root@rtfmp ~]# ip ro
default via 192.168.76.1 dev eno16777736
default via 192.168.76.2 dev eno16777736  proto static  metric 1024
192.168.76.0/24 dev eno16777736  proto kernel  scope link  src 192.168.76.135

14. Add a static route
ip route add $network_id/$prefix dev $device

[root@rtfmp ~]# ip route add 10.0.0.0/24 dev  eno16777736
[root@rtfmp ~]# ip route
default via 192.168.76.1 dev eno16777736
default via 192.168.76.2 dev eno16777736  proto static  metric 1024
10.0.0.0/24 dev eno16777736  scope link
192.168.76.0/24 dev eno16777736  proto kernel  scope link  src 192.168.76.135

15. Delete Route
ip route delete $network_id/$prefix dev $device

[root@rtfmp ~]# ip route delete 10.0.0.0/24 dev  eno16777736
[root@rtfmp ~]# ip route
default via 192.168.76.1 dev eno16777736
default via 192.168.76.2 dev eno16777736  proto static  metric 1024
192.168.76.0/24 dev eno16777736  proto kernel  scope link  src 192.168.76.135

If there is anything else you need to do with ip command, this is the most complete guide I have found.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s