Sep 232018
 

Getting a good handle on IP commands will prove useful if you’re managing a Linux box.

Although ifconfig was deprecated and replaced with ip several years ago, some folks sill use ifconfig.

But it’s good to be aware of the basic ip commands when you’re managing a Linux server.

Here are a bunch of IP commands that were tested on my CentOS 7 box. They should work on a RedHat 7 system too.

1. How to Display IP Addresses

To see the IP addresses on your system and various interfaces, use the ip addr command.

[thomaspc@localhost ~]$ ip addr
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: enp0s25:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:25:64:d4:d7:ca brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.50/24 brd 10.0.1.255 scope global noprefixroute dynamic enp0s25
       valid_lft 57035sec preferred_lft 57035sec
    inet6 fe80::e64d:5dda:2def:dfbe/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:13:72:5b brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:13:72:5b brd ff:ff:ff:ff:ff:ff
7: vnet0:  mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:ed:b0:ce brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:feed:b0ce/64 scope link 
       valid_lft forever preferred_lft forever

Let’s next examine how to check for information only on a particular interface.

2. Display Information for a Specific Interface

The below command will display information only on a particular interface.

[thomaspc@localhost ~]$ ip addr show dev enp0s25
2: enp0s25:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:25:64:d4:d7:ca brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.50/24 brd 10.0.1.255 scope global noprefixroute dynamic enp0s25
       valid_lft 56947sec preferred_lft 56947sec
    inet6 fe80::e64d:5dda:2def:dfbe/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3. Show Various Network Interfaces

The ip link command provides information on the state of different network interfaces.

[thomaspc@localhost ~]$ ip 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
2: enp0s25:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:25:64:d4:d7:ca brd ff:ff:ff:ff:ff:ff
3: virbr0:  mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:13:72:5b brd ff:ff:ff:ff:ff:ff
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:13:72:5b brd ff:ff:ff:ff:ff:ff
7: vnet0:  mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN mode DEFAULT group default qlen 1000
    link/ether fe:54:00:ed:b0:ce brd ff:ff:ff:ff:ff:ff

The ip link command can also be used to get an interface online or offline.

4. How to Add an IP Address

The below command will show you how to add an IP address using the ip addr command.

[thomaspc@localhost ~]$ ip addr add 192.168.0.77/24 dev enp0s25

The above command adds address 192.168.0.77 with netmask 24 to device enp0s25

5. How to Delete an IP Address

With the ip addr del command, deleting an IP address is simple.

[thomaspc@localhost ~]$ ip addr del 192.168.0.77/24 dev enp0s25
6. How to bring a Network Interface Up?

Learning how to bring a network interface up is essential for a Linux system administrator.

[thomaspc@localhost ~]$ sudo ip link set em1 up

The above command will bring enp0s25 online.

7. How to bring a Network Interface Down?

There might be occasions when you need to bring a network interface down on our CentOS 7 system.

For such situations, go with the following command.

[thomaspc@localhost ~]$ sudo ip link set enp0s25 down

The above command will take enp0s25offline.

You can check if the interface enp0s25 has been taken offline via the following command:

[thomaspc@localhost ~]$ ip addr show enp0s25
enp0s25:  mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:25:64:d4:d7:ca brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.50/24 brd 10.0.1.255 scope global noprefixroute dynamic enp0s25
       valid_lft 79017sec preferred_lft 79017sec

As you see from the above output, the network interface enp0s25 is indeed down.

8. How to Check the Routing Table?

You can use ip route show to check the routing table (previously, you were likely using the route command.

[thomaspc@localhost ~]$ ip route show
default via 10.0.1.1 dev enp0s25 proto dhcp metric 100 
10.0.1.0/24 dev enp0s25 proto kernel scope link src 10.0.1.50 metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
9. How to Add a Route?

Now, let’s look at how to add an entry to the routing table.

[thomaspc@localhost ~]$ ip route add 192.168.3.0/24 dev enp0s25
10. How to Delete a Route?

To delete an entry from the routing table, run the below command.

[thomaspc@localhost ~]$ ip route del 192.168.3.0/24 dev enp0s25

Sorry, the comment form is closed at this time.