How to Use IP Command in Linux with Examples?


The IP command is a powerful tool for network configuration in Linux. It is used to show, manipulate routing, devices, policy routing, and tunnels. The IP command is part of the iproute2 package, which is installed by default in most Linux distributions. This article will guide you through the basics of the IP command and provide examples to help you understand its usage.

Displaying IP Addresses

To display the IP address of all network interfaces, use the following command−

Example

ip addr show

Output

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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
   link/ether 08:00:27:15:64:b3 brd ff:ff:ff:ff:ff:ff
   inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
      valid_lft 86378sec preferred_lft 86378sec

Adding an IP Address

To add an IP address to a network interface, use the following command:

sudo ip addr add 192.168.1.100/24 dev eth0

This command adds the IP address 192.168.1.100 to the network interface eth0. The /24 at the end of the IP address is the subnet mask.

Deleting an IP Address

To delete an IP address from a network interface, use the following command −

sudo ip addr del 192.168.1.100/24 dev eth0

This command removes the IP address 192.168.1.100 from the network interface eth0.

Displaying the Routing Table

To display the routing table, use the following command −

Example

ip route show

Output

default via 192.168.1.1 dev eth0 proto static 
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10

Adding a Route

To add a route, use the following command −

sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

This command adds a route to the network 192.168.2.0/24 via the gateway 192.168.1.1 on the network interface eth0.

Deleting a Route

To delete a route, use the following command −

sudo ip route del 192.168.2.0/24

This command deletes the route to the network 192.168.2.0/24.

Displaying Statistics for Network Interfaces

The ip -s link command displays statistics for the network interfaces −

Example

ip −s link

Output

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
   RX: bytes  packets  errors  dropped overrun mcast   
   166114     1982     0       0       0       0      
   TX: bytes  packets  errors  dropped carrier collsns 
   166114     1982     0       0       0       0 

Displaying Neighbour Objects

The ip neigh command displays neighbour objects; essentially, it shows the ARP table −

Example

ip neigh

Output

192.168.1.1 dev eth0 lladdr 00:14:bf:b1:cb:31 REACHABLE

Adding a Neighbour

To add a neighbour, use the following command −

sudo ip neigh add 192.168.1.101 lladdr 1:2:3:4:5:6 dev eth0

This command adds a neighbour with the IP address 192.168.1.101 and the MAC address 1:2:3:4:5:6 to the network interface eth0.

Deleting a Neighbour

To delete a neighbour, use the following command −

sudo ip neigh del 192.168.1.101 dev eth0

This command deletes the neighbour with the IP address 192.168.1.101 from the network interface eth0.

Displaying Multicast Information

The ip maddr command displays multicast addresses −

Example

ip maddr

Output

1:    lo
      inet  224.0.0.1
      inet6 ff02::1
2:    eth0
      inet  224.0.0.251
      inet  224.0.0.1
      inet6 ff02::1:ff00:1
      inet6 ff02::1

Displaying Network Interfaces

The ip link show command displays information about the network interfaces −

Example

ip link show

Output

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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:15:64:b3 brd ff:ff:ff:ff:ff:ff

Changing the State of a Network Interface

To change the state of a network interface, use the following commands −

sudo ip link set eth0 down
sudo ip link set eth0 up

The first command brings the network interface eth0 down, and the second command brings it back up.

Changing the MTU of a Network Interface

To change the Maximum Transmission Unit (MTU) of a network interface, use the following command −

sudo ip link set eth0 mtu 1400

This command sets the MTU of the network interface eth0 to 1400.

Displaying TCP and UDP Sockets

The ip −s −s −t tcp show and ip −s −s −t udp show commands display TCP and UDP sockets, respectively −

Example

ip −s −s −t tcp show
ip −s −s −t udp show

Output

The output will look something like this −

TCP: inuse 23 orphan 0 tw 10 alloc 25 mem 6
TCP: inuse 23 orphan 0 tw 10 alloc 25 mem 6

Displaying Network Namespace

The ip netns command is used to manage network namespaces. To display the list of network namespaces, use the following command −

ip netns

If you haven't created any network namespaces, this command will not return any output.

Adding a Network Namespace

To add a network namespace, use the following command −

sudo ip netns add mynamespace

This command creates a new network namespace named "mynamespace".

Deleting a Network Namespace

To delete a network namespace, use the following command −

sudo ip netns del mynamespace

This command deletes the network namespace named "mynamespace".

Displaying the IP Address of a Network Namespace

To display the IP address of a network namespace, use the following command −

sudo ip netns exec mynamespace ip addr

This command displays the IP address of the network namespace named "mynamespace".

Displaying the Routing Table of a Network Namespace

To display the routing table of a network namespace, use the following command −

sudo ip netns exec mynamespace ip route

Save to grepper

This command displays the routing table of the network namespace named "mynamespace".

These are just a few examples of the many commands and options available with the ip command in Linux. As always, you can refer to the man pages (man ip) for more detailed information.

Conclusion

The IP command in Linux is a versatile tool for managing network interfaces and routes. The examples provided in this article are just the basics. The IP command has many more options and capabilities. For more detailed information, you can always refer to the man pages by typing man ip in the terminal.

Remember, changes made with the IP command are not persistentacross reboots. To make persistent changes, you need to edit the network configuration files, which vary based on your Linux distribution and network manager. Always be cautious when making changes to your network configuration, as incorrect settings can disrupt network connectivity.

Updated on: 13-Jul-2023

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements