How to Administer the Network Using the ip Command in Linux

2023.02.02

How to Administer the Network Using the ip Command in Linux

The IP command is an essential tool for system and network administrators. Here's how to use it to manage networks and IP addresses on Linux.

The most fundamental part of Linux administration and troubleshooting is to check the system's IP configuration to make sure the system has a valid IP and is reachable on the local network.

The ip command in Linux is a powerful tool that not only displays the current IP address of the system, but also allows you to view and manage the current configuration of network interfaces, IP addresses, routing and ARP tables.

Let's look at some common use cases of the ip command in Linux.

Find IP Address on Linux

To find the IP address of a Linux system, use the ip command followed by address, addr or options:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip address
  • 1.
  • 2.

This will display the configuration of all network interfaces, including their IP addresses.

In the example below, you can see that the system has three network interfaces: lo (the loopback address), ens33. The output includes the following information:

  • Layer 1 information such as interface capabilities and physical layer connection status, MTU, operational status of the interface (for example, UP or DOWN), and transmit queue length (qlen).
  • Layer 2 information, such as the MAC address of the interface.
  • Layer 3 information includes the IP address and its type (dynamic IP addressing or static IP addressing).

To display brief information about a network interface, use the ip command with the –brief option as follows:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip --brief address show
  • 1.
  • 2.

To display only IPv4 address information, use the following command:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip -4 addr
  • 1.
  • 2.

To find IP address information for a specific network interface, use the following syntax:

ip address show dev [interface]
  • 1.

For example, to view the IP address of network interface ens33, the command would be:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip address show dev ens33
  • 1.
  • 2.

View and change MAC address

Using the ip command, you can also view and change the MAC address of the system.

To view the MAC address of your Linux system, use the following command:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip --brief link show
  • 1.
  • 2.

To view the MAC address of a specific interface, use:

ip --brief link show dev [interface]
  • 1.

To change the MAC address, first shut down the interface:

sudo ip link set dev [interface] down
  • 1.

Then change the MAC address of the interface with the following command:

sudo ip link set dev [interface] address [new-mac-adddress]
  • 1.

After that, call out the interface:

sudo ip link set dev ens33 up
  • 1.

View network interface statistics

Statistics for network interfaces can also be viewed using the ip command. Use the following ip command to view statistics for all network interfaces on the system:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip -s link
  • 1.
  • 2.

Output includes statistics about transmitted and received bytes/packets, errors, dropped packets, multicast, etc. To display statistics for a specific interface, use the following syntax:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip -s link show dev ens33
  • 1.
  • 2.

Use ip to modify link properties

To open an interface, use the following ip command:

sudo ip link set [interface] up
  • 1.

To shut down an interface, run:

sudo ip link set [interface] down
  • 1.

You can also change the MTU (Maximum Transmission Unit) of an interface with the following command:

sudo ip link set mtu [number] dev [interface]
  • 1.

For example, to set the MTU of network interface ens33 to 8000, the command would be:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ sudo ip link set mtu 8000 dev ens33
  • 1.
  • 2.

Add/Remove IP Addresses on Linux

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

ip addr add [ip-address] dev [interface]
  • 1.

To add the IP address 192.168.42.140/24 to the network interface ens33, the command would be:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ sudo ip addr add 192.168.152.130/24 dev ens33
  • 1.
  • 2.

Use the following command syntax to remove an IP address from an interface:

ip addr del [ip-address] dev [interface]
  • 1.

For example, to delete the IP address 192.168.152.130/24 from the interface ens33, the command is:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ sudo ip addr del 192.168.152.130/24 dev ens33
  • 1.
  • 2.

View routing table on Linux

The ip route command is used to view and modify routes in the Linux system. To display the system's routing table, use the ip route command without any options:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip route
  • 1.
  • 2.

Each line in the output represents a configured route. A route consists of a destination network address, a next hop (i.e., the router's IP address), the interface the packet is sent from, and a metric (the value used to determine the preferred route when there are multiple routes to reach the destination). In this case, the route with the lower metric is preferred.

In the example above, the first two entries represent the default route, which is used when no other route is available for the destination address. 192.168.174.2 is the router's IP address. dev ens33 indicates the interface that will be used to send packets to the router. The proto dhcp field indicates that the default route is learned from DHCP.

The second entry represents the APIPA address (Automatic Private IP Addressing) 169.254.0.0/16. When a host cannot obtain an IP address from a DHCP server, it assigns itself a random IP address from that network. It allows them to communicate with other hosts on the subnet that also failed to obtain IP addresses.

The src field indicates the IP address of the interface used as the source address when sending packets through this route.

Use ip to modify the routing table

To add a route manually, use the ip route command followed by the destination network address and gateway IP:

sudo ip route add [network-id] via [gateway-ip]
  • 1.

For example, to add a route that sends all traffic destined for the 192.168.30.0 network to the gateway at 192.168.174.2, you would use the following command:

sudo ip route add 192.168.30.0/24 via 192.168.174.2
  • 1.

Add a default route with the following command:

sudo ip route add default via [ip-address] dev [interface]
  • 1.

For example, to add a default route directing traffic to router 192.168.30.1 through ens33, the command would be:

sudo ip route add default via 192.168.30.1 dev ens33
ip route get [ip-address]
  • 1.
  • 2.

To delete a routing table entry, use the following syntax:

sudo ip route delete [network-address] via [gateway-ip]
  • 1.

You can also see which route an address will take using the following syntax:

ip route get [ip-address]
  • 1.

Manage Neighbor Tables on Linux

In Linux, you can use the ip neigh command to view and modify the neighbor table, that is, the ARP table. To view current neighbor table entries, use the following command:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip neigh show
  • 1.
  • 2.

To add a new entry to the neighbor table, use the following syntax:

sudo ip neigh add [ip-address] lladdr [mac-address] dev [interface]
  • 1.

To delete an entry from the neighbor table, use the following syntax:

sudo ip neigh del [ip-address] dev [interface]
  • 1.

Add color to the output of the ip command

To make the output easier and faster to understand, you can use the -c option to add color to the ip command output:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ ip -c a
  • 1.
  • 2.

Manage Network and IP Addresses on Linux

The ip command in Linux is a useful tool for managing and troubleshooting network connections. From viewing network interfaces and modifying link properties to finding IP addresses and managing routes, the ip command lets you perform many system administration tasks from the command line.