Working With The ip Command On Linux
For decades now, the ifconfig command has been the go-to tool for managing network interfaces and checking network configuration on Linux. Unfortunately, ifconfig is now quite out-of-date, so much so that the members of the Debian team proposed deprecating the old ifconfig tool and its associated suite of software and replacing it with iproute in 2009. Even after all this time, people are still using the old ifconfig tool rather than its shiny replacement. So in this post, we’ll be covering some of the uses for this replacement: the ip command.
ip Command Uses
The first common use for the ifconfig command is to look at the current network configuration of the server. With the ip command this can be achieved using the following command:
ip addr show
The output layout is somewhat different from ifconfig. The first line shows the interface name and some details about the configuration. The second line (link/ether) shows the MAC address details of the interface. The inet line shows information about the IPv4 address assigned, such as the IP with it’s CIDR mask and broadcast address and how it was configured. The inet6 line shows the similar IPv6 details.
If you only wanted to see the IPv4 or IPv6 details, you can do that using the -4 or -6 flags respectively. An example would be:
ip -4 addr show
If you have multiple network interfaces, you can also limit them by providing the name of the interface at the end of the command. The following example would show just the ipv6 information for the network interface named wlp4s0:
ip -6 addr show wlp4s0
Missing elements that ifconfig would typically display are the interface statistics such as bytes sent and received, packets sent and received, packets dropped, errors counted, etc. This can be shown by the following command:
ip -s link
The ip command can also be used to enable or disable interfaces as well as the “link set” option:
sudo ip link set enp0s3 up
sudo ip link set enp0s3 down
The commands above would enable (up) or disable (down) the ethernet interface named enp0s3.
As with ifconfig, the ip command can be used to change the interfaces configuration too. One common task is to manage an additional IP address on a network interface, also referred to as an alias. Here’s an example of using aliases:
sudo ip addr add 192.168.1.50/24 dev enp0s3 label enp0s3:1
There’s a lot going on in this command, so let’s walk through it. The start of “ip addr add” is fairly straightforward in that we are adding an ip address. Next, we have “192.168.1.50/24” which is the IP address we are adding along with its netmask in CIDR notation. The section after that “dev enp0s3” tells the system which network device the IP address is to be assigned to. Finally, we give a label, which should be referred to when looking at the interface details.
Removing the IP alias is fairly straightforward. Repeating the command switching out the add for del, as follows:
sudo ip addr del 192.168.1.50/24 dev enp0s3 label enp0s3:1
As well as dealing with interface configuration, you can also use the ip command for managing the routing information for your system. You can view the current route information with the following command:
ip route show
Normally this would show a default setting, the gateway IP address of your main network interface, and there would be routes for local IP addresses to the network interface on the same subnet. In most cases, your VPS will have a single interface with a public IP address. You can also have additional interfaces, in which case there will be route information for each interface to route the relevant traffic to each.
That concludes a somewhat whistlestop tour of some commonly used functions of the ip command. As with many commands, it’s worth having a look at the man page for it and its related commands for advanced usage.