OSETC TECH

How to Use IP Command in Linux

This post will guide you how to show or manipulate routing, network devices, interfaces and tunnels using IP command on your Linux system. How do I Dispaly and modify network interfaces using ip command under CentOS or RHEL Linux system.

IP Command


IP Command can be used to display or modify IP Address for network interface, or used to show or alter the routing table on Linux operating systems.

The syntax of the IP command are as follows:

ip [ OPTIONS ] OBJECT { COMMAND | help }

ip [ -force ] -batch filename
OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | tcp_met‐
rics | token | macsec }
OPTIONS := { -V[ersion] | -h[uman-readable] | -s[tatistics] | -d[etails] | -r[esolve] | -iec | -f[amily] { inet | inet6 | ipx | dnet | link } | -4 | -6 | -I |
-D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] | -c[olor]
-br[ief] }

Show Information about All IP Addresses


If you want to show IP address information for all network interfaces on your Linux system, and you can type the following command:

$ ip a

or

$ ip addr

or

$ ip addr show

Outputs:

devops@devops-ubuntu:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever
3: lxdbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether fe:2d:55:33:7e:a9 brd ff:ff:ff:ff:ff:ff
inet 10.128.138.1/24 scope global lxdbr0
valid_lft forever preferred_lft forever
inet6 fd42:841b:d3e7:7c96::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::3cef:96ff:fe8e:b7cb/64 scope link
valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:44:00:db:06 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
6: vethH3DUHJ@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP group default qlen 1000
link/ether fe:c1:f0:e8:87:e7 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::fcc1:f0ff:fee8:87e7/64 scope link
valid_lft forever preferred_lft forever
8: veth2VYAR3@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP group default qlen 1000
link/ether fe:2d:55:33:7e:a9 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::fc2d:55ff:fe33:7ea9/64 scope link
valid_lft forever preferred_lft forever
10: veth8LHE8V@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP group default qlen 1000
link/ether fe:f9:55:e3:ab:38 brd ff:ff:ff:ff:ff:ff link-netnsid 2
inet6 fe80::fcf9:55ff:fee3:ab38/64 scope link
valid_lft forever preferred_lft forever

Show Link Information about All Network Interfaces


If you want to show link information about all network interfaces, and you can type the following command at the shell prompt:

$ ip link

or

$ ip l

Outputs:

devops@devops-ubuntu:~$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
3: lxdbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether fe:2d:55:33:7e:a9 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:44:00:db:06 brd ff:ff:ff:ff:ff:ff
6: vethH3DUHJ@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP mode DEFAULT group default qlen 1000
link/ether fe:c1:f0:e8:87:e7 brd ff:ff:ff:ff:ff:ff link-netnsid 0
8: veth2VYAR3@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP mode DEFAULT group default qlen 1000
link/ether fe:2d:55:33:7e:a9 brd ff:ff:ff:ff:ff:ff link-netnsid 1
10: veth8LHE8V@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxdbr0 state UP mode DEFAULT group default qlen 1000
link/ether fe:f9:55:e3:ab:38 brd ff:ff:ff:ff:ff:ff link-netnsid 2

Show Network Statistics


You can also use the ip command to show the statistics of the various network interfaces. and you can use one of the following command:

$ ip -s a

or

$ ip -s link

outputs:

devops@devops-ubuntu:~$ ip -s a
1: lo: <LOOPBACK,UP,LOWER_UP> 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
RX: bytes packets errors dropped overrun mcast
47086 478 0 0 0 0
TX: bytes packets errors dropped carrier collsns
47086 478 0 0 0 0
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
17728735 20594 0 0 0 1758
TX: bytes packets errors dropped carrier collsns
1115529 7763 0 0 0 0

If you want to get network statistics for a particular network interface, and you need to add the option ls followed by the name of the network interface. type:

$ ip -s a ls enp0s3

Outputs:

devops@devops-ubuntu:~$ ip -s a ls enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
17741003 20742 0 0 0 1808
TX: bytes packets errors dropped carrier collsns
1128809 7840 0 0 0 0

If you want to get more detailed information about that particular network interface, and you can add more than once “-s” option, type:

$ ip -s -s a ls enp0s3

Outputs:

devops@devops-ubuntu:~$ ip -s -s a ls enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
17743897 20773 0 0 0 1810
RX errors: length crc frame fifo missed
0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1131649 7859 0 0 0 0
TX errors: aborted fifo window heartbeat transns
0 0 0 0 2

show IPv4 Information only


If you only want to print out IPv4 information about all network interfaces, just type:

$ ip -4 a

Outputs:

devops@devops-ubuntu:~$ ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> 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
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
3: lxdbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 10.128.138.1/24 scope global lxdbr0
valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever

Show IPv6 Information Only


To print out information relating to IPv6 in your Linux, and just type the following command:

$ ip -6 a

Outputs:

devops@devops-ubuntu:~$ ip -6 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever
3: lxdbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fd42:841b:d3e7:7c96::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::3cef:96ff:fe8e:b7cb/64 scope link
valid_lft forever preferred_lft forever
6: vethH3DUHJ@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fe80::fcc1:f0ff:fee8:87e7/64 scope link
valid_lft forever preferred_lft forever

Print Netowrk Interfaces Details in a Single Line


You may be want to print out all network interface details into a single line, and you can type the following command:

$ ip -o a

outputs:

devops@devops-ubuntu:~$ 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: enp0s3 inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3\ valid_lft forever preferred_lft forever
2: enp0s3 inet6 fe80::a00:27ff:fe1d:33d0/64 scope link \ valid_lft forever preferred_lft forever
3: lxdbr0 inet 10.128.138.1/24 scope global lxdbr0\ valid_lft forever preferred_lft forever
3: lxdbr0 inet6 fd42:841b:d3e7:7c96::1/64 scope global \ valid_lft forever preferred_lft forever
3: lxdbr0 inet6 fe80::3cef:96ff:fe8e:b7cb/64 scope link \ valid_lft forever preferred_lft forever
4: docker0 inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0\ valid_lft forever preferred_lft forever
6: vethH3DUHJ inet6 fe80::fcc1:f0ff:fee8:87e7/64 scope link \ valid_lft forever preferred_lft forever
8: veth2VYAR3 inet6 fe80::fc2d:55ff:fe33:7ea9/64 scope link \ valid_lft forever preferred_lft forever
10: veth8LHE8V inet6 fe80::fcf9:55ff:fee3:ab38/64 scope link \ valid_lft forever preferred_lft forever

Assign IP Address to an Network Interface


If you want to assign an IP address to an network interface, and you can use the following syntax:

ip address add IP_ADDRESS/Mask dev Interface_Name

For example, you want to assign 192.168.3.59/25 to enp0s3 network interface, just enter:

$ sudo ip address add 192.168.3.59/24 dev enp0s3

You can type the following command to verify if the IP is assigned, type:

$ ip addr show enp0s3

Outputs:

devops@devops-ubuntu:~$ ip addr show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1d:33:d0 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.50/24 brd 192.168.3.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.3.59/24 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe1d:33d0/64 scope link
valid_lft forever preferred_lft forever

If you want to remove an IP address from a particular network interface, and you can use the following syntax:

ip address del IP_ADDRESS/Mask dev Interface_Name

To remove Ip address 192.168.3.59/24 from interface device enp0s3, enter:

$ sudo ip address del 192.168.3.59/24 dev enp0s3

Bring Network Interface UP or Down


If you want to bring network interfaces up or down using IP command in your Linux system, and you can use the following syntax:

ip link set Interface_Name up| down

For example, you want to bring the interface enp0s3 down, enter:

$ sudo ip link set enp0s3 down

To bring it up, enter:

$ sudo ip link set enp0s3 up

Show IP Routing Table


If you want to dispaly a list of the routing tables in your system, and type:

$ ip route list

Outputs:

devops@devops-ubuntu:~$ ip route list
default via 192.168.3.1 dev enp0s3
10.128.138.0/24 dev lxdbr0 proto kernel scope link src 10.128.138.1
169.254.0.0/16 dev enp0s3 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.3.0/24 dev enp0s3 proto kernel scope link src 192.168.3.50

Add a New Route or Default Route


If you need to add a new route to the routing table, and you can use the route add command followed by network or Gateway or Device. you can use the following syntax:

ip route add Network/mask via Gateway
ip route add Network/mask dev Device

For example, add a route to 192.168.3.0/24 via gateway 192.168.3.1, enter:

$ sudo ip route add 192.168.3.0/24 via 192.168.3.1

To add a route to 192.168.3.0/24 via device enp0s3, enter:

$ sudo ip route add 192.168.3.0/24 dev enp0s3

if you want to add a default route into the routing table, and you can use “ip route add default” command.

For example, you wan to add a default route via the gateway 192.168.3.1 connected via enp0s3 device. enter:

$ sudo ip route add default via 192.168.3.1 dev enp0s3

Delete a Route


If you need to delete a route from routing table in your system, and you can use “ip route del” command.

For exmaple, you wish to delete a route for network 192.168.3.0/24 via the gateway 192.168.3.1, type:

$ sudo ip route del 192.168.3.0/24 via 192.168.3.1

If you want to delete a default route,just running the following command:

$ ip route del default

Conclusion


You should know that how to use IP command to display or configure networking, routing, and tunnels in your CentOS/RHEL/Ubuntu Linux.