The most fundamental part of Linux administration and troubleshooting is to check the IP configuration of a system to make sure the system has a valid IP and is accessible on the local network.
The ip command in Linux is a powerful tool that not only displays the current IP address of a system but also allows you to view and manage the current configuration of network interfaces, IP addresses, routes, and ARP tables.
Let’s look at some of the common use cases of the ip command in Linux.
Find the IP Address on Linux
To find the IP address of your Linux system, use the ip command followed by address, addr, or a option:
This will display the configuration of all network interfaces including their IP addresses.
In the following example, you can see that the system has three network interfaces: lo (loopback address), ens33, and ens37. The output includes the following information:
- Layer 1 information such as interface capability and physical layer connectivity state, MTU, the operational state of the interface (e.g., UP or DOWN), and transmission queue length (qlen).
- Layer 2 information like 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 network interfaces, use the ip command with the –brief option as follows:
ip --brief address show
To display only IPv4 address information, use the following command:
ip -4 addr
To find out the IP address information of a particular network interface, use the following syntax:
ip address show dev [interface]
For instance, to view the IP address of a network interface ens33, the command would be:
ip address show dev ens33
View and Change the MAC Address
Using the ip command, you can also view and change the MAC address of your system.
To view the MAC address of your Linux system, use the following command:
ip --brief link show
To view the MAC address of a particular interface, use:
ip --brief link show dev [interface]
To change the MAC address, first bring down the interface:
sudo ip link set dev [interface] down
Then use the following command to change the MAC address of your interface:
sudo ip link set dev [interface] address [new-mac-adddress]
After that, bring up the interface:
sudo ip link set dev [interface] up
View Network Interface Statistics
Using the ip command, you can also view the statistics of a network interface. Use the following ip command to view the stats for all network interfaces on your system:
ip -s link
The output includes stats such as transmitted and received bytes/packets, errors, dropped packets, multicast, etc. To display stats for a particular interface, use the following syntax:
ip -s link show dev [interface]
Modify Link Properties Using ip
To bring up an interface, use the following ip command:
sudo ip link set [interface] up
To bring down the interface, run:
sudo ip link set [interface] down
You can also change the MTU (maximum transmission unit) of an interface using the following command:
sudo ip link set mtu [number] dev [interface]
For instance, to set the MTU of a network interface ens33 to 8000, the command would be:
sudo ip link set mtu 8000 dev ens33
Add/Remove IP Address on Linux
To add an IP address to a network interface, use the following syntax:
ip addr add [ip-address] dev [interface]
To add an IP address 192.168.42.140/24 to network interface ens33, the command would be:
ip addr add 192.168.42.140/24 dev ens33
Use the following command syntax to remove the IP address from an interface:
ip addr del [ip-address] dev [interface]
To remove the IP address 192.168.42.140/24 from interface ens33, the command would be:
ip addr del 192.168.42.140/24 dev ens33
View the Routing Table on Linux
The ip route command is used to view and modify the routes in a Linux system. To display the routing table of your system, use the ip route command without any option:
Each line in the output represents a configured route. A route consists of a destination network address, a next hop (i.e., the IP address of the router), an interface through which the packet will be sent, and the metric (a value used to determine the preferred route when there are multiple routes available to the destination). The route with the lower metric is preferred in this case.
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.42.2 and 192.168.10.1 are the IP addresses of the router. The dev ens33 and ens37 indicate the interface which will be used to send the packets to the router. The proto dhcp fields indicate 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 fails to obtain an IP address from a DHCP server, it assigns itself a random IP address from this network. It lets them communicate with other hosts in the subnet that have also failed to obtain the IP address.
The third and fourth entries show the network addresses of the local networks to which the system is connected. The 192.168.10.0 is the network that interface ens37 is attached to while the 192.168.42.0 is the network ens33 is attached to.
The src field indicates the IP address of the interface used as the source address when sending packets through this route.
Modify the Routing Table Using ip
To manually add a route, use the ip route command followed by the destination network address and gateway IP:
sudo ip route add [network-id] via [gateway-ip]
For instance, to add a route that sends all traffic destined for the 192.168.20.0 network to the gateway at 192.168.10.16, you would use the following command:
sudo ip route add 192.168.20.0/24 via 192.168.10.16
Use the following command to add a default route:
sudo ip route add default via [ip-address] dev [interface]
For instance, to add the default route that directs the traffic to the router at 192.168.10.1 through ens33, the command would be:
sudo ip route add default via 192.168.10.1 dev ens33
ip route get [ip-address]
To delete a routing table entry, use the following syntax:
sudo ip route delete [network-address] via [gateway-ip]
You can also view the route an address will take using the following syntax:
ip route get [ip-address]
Manage Neighbor Table on Linux
You can use the ip neigh command in Linux to view and modify the neighbor table, also known as the ARP table. To view current neighbor table entries, use the following command:
ip neigh show
To add a new entry in the neighbor table, use the following syntax:
sudo ip neigh add [ip-address] lladdr [mac-address] dev [interface]
To remove an entry from the neighbor table, use the following syntax:
sudo ip neigh del [ip-address] dev [interface]
Add Color to the Output of ip Command
To make the output easier and quicker to understand, you can add color to the ip command output using the -c option:
ip -c a
Managing Networks and IP Addresses on Linux
The ip command in Linux is a useful tool for administering and troubleshooting network connections. From viewing the network interfaces and modifying link properties to finding the IP addresses and managing routes, the ip command lets you do several system administration tasks via the command line.