How to Use the ip Command on Linux

A Bash prompt on an Ubuntu-style Linux laptop in a terminal window.Fatmawati Achmad Zaenuri / Shutterstock

You can configure IP addresses, network interfaces, and routing rules on the fly with the Linux ip command. We’ll show you how you can use this modern replacement for the classic (and now obsolete) ifconfig.

How the ip command works

With the ip command, you can adjust the way a Linux computer manages IP addresses, network interface controllers (NIC), and routing rules. The changes also take effect immediately – you don’t need to restart. The ip command can do much more than that, but we will focus on the most common uses in this article.

The ip command has many subcommands, each operating on a type of object, such as IP addresses and routes. There are in turn many options for each of these items. It is this wealth of functionality that gives the ip command the granularity you need to perform delicate tasks. It is not an ax job, it requires a set of scalpels.

We will examine the following objects:

Address: IP addresses and ranges.
Link: Network interfaces, such as wired connections and Wi-Fi adapters.
Road: The rules that manage the routing of traffic sent to addresses via interfaces (links).

Use ip with addresses

Obviously, you must first know the parameters with which you are dealing. To find out the IP addresses of your computer, use the ip command with the object’s address. The default action is show, which lists the IP addresses. You can also omit display and abbreviate the address with “addr” or even “a”.

The following commands are all equivalent:

IP address show
ip addr show
ip addr
ip a

We see two IP addresses, as well as a lot of other information. IP addresses are associated with network interface controllers (NICs). The ip command tries to be useful and also provides a lot of information about the interface.

The first IP address is the loopback (internal) address used to communicate within the computer. The second is the real (external) IP address that the computer has on the local area network (LAN).

Let’s break down all the information we have received:

lo: The name of the network interface as a string.
: It is a loopback interface. It’s UP, which means it’s operational. the physical network layer (layer one) is also increasing.
mtu 65536: The maximum transfer unit. This is the size of the largest block of data that this interface can transmit.
qdisc noqueue: A qdisc is a queuing mechanism. It plans the transmission of packets. There are different queuing techniques called disciplines. Discipline without a queue means “send instantly, don’t queue”. This is the default qdisc discipline for virtual devices, such as the loopback address.
UNKNOWN state: This can be LOW (the network interface is not operational), UNKNOWN (the network interface is operational but nothing is connected), or HIGH (the network is operational and there is a connection).
default group: The interfaces can be grouped logically. The default is to put them all in a group called “default”.
qlen 1000: The maximum length of the transmission queue.
link / loopback: the support access control (MAC) address of the interface.
inet 127.0.0.1/8: The version 4 IP address. The part of the address after the slash (/) is Class-free inter-domain routing notation (CIDR) representing the subnet mask. It indicates how many contiguous header bits are set to one in the subnet mask. The value of eight means eight bits. Eight bits defined on a represent 255 in binary, the subnet mask is therefore 255.0.0.0.
scope host: The scope of the IP address. This IP address is only valid inside the computer (the “host”).
lo: The interface with which this IP address is associated.
valid_lft: Valid lifetime. For an IP version 4 IP address allocated by Dynamic host configuration protocol (DHCP) is the time for which the IP address is considered valid and capable of making and accepting connection requests.
Preferred_lft: Preferred life. For an IP version 4 IP address allocated by DHCP, this is the time for which the IP address can be used without restriction. This should never be greater than the valid_lft value.
inet6: Version 6 IP address, scope, valid_lft and Preferred_lft.

The physical interface is more interesting, as we will show below:

enp0s3: The name of the network interface as a string. The “en” means Ethernet, “p0” is the bus number of the Ethernet card and “s3” is the slot number.
: This interface supports vast- and multicastand the interface is UP (operational and connected). The hardware layer of the network (layer 1) is also UP.
mtu 1500: The maximum transfer unit supported by this interface.
qdisc fq_codel: The scheduler uses a discipline called “Fair Queuing, Controlled Delay”. It is designed to provide a fair share of bandwidth to all traffic flows that use the queue.
indicate UP: The interface is operational and connected.
default group: This interface is part of the “default” interface group.
qlen 1000: The maximum length of the transmission queue.
bond / ether: The MAC address of the interface.
inet 192.168.4.26/24: The IP address version 4. The “/ 24” tells us that there are 24 contiguous header bits set to one in the subnet mask. It’s three groups of eight bits. An eight-bit binary number equals 255; therefore, the subnet mask is 255.255.255.0.
brd 192.168.4.255: the Mailing address for this subnet.
global scope: The IP address is valid everywhere on this network.
dynamic: The IP address is lost when the interface breaks down.
noprefixroute: Do not create a route in the route table when this IP address is added. Someone has to add a route manually if they want to use one with this IP address. Likewise, if this IP address is deleted, do not look for a route to delete.
enp0s3: The interface with which this IP address is associated.
valid_lft: Valid lifetime. The time at which the IP address will be considered valid; 86 240 seconds is 23 hours and 57 minutes.
Preferred_lft: Preferred life. The time at which the IP address will operate without any restrictions.
inet6: Version 6 IP address, scope, valid_lft and Preferred_lft.

Show only IPv4 or IPv6 addresses

If you want to limit output to IP version 4 addresses, you can use the -4 option, as follows:

ip -4 addr

If you want to limit output to version 6 IP addresses, you can use the -6 option, as follows:

ip -6 addr

Display information for a single interface

If you want to see IP address information for a single interface, you can use the show and dev options and name the interface, as shown below:

ip addr show dev lo
ip addr show dev enp0s3

You can also use the -4 or -6 flag to further refine the output so that you only see what interests you.

If you want to see IP version 4 address information on the enp0s3 interface, type the following command:

ip -4 addr show dev enp0s3

Adding an IP address

You can use the add and dev options to add an IP address to an interface. You just have to tell the ip command which IP address to add and to which interface to add it.

We will add the IP address 192.168.4.44 to the enp0s3 interface. We must also provide the CIDR notation for the subnet mask.

We type the following:

sudo ip addr add 192.168.4.44/24 dev enp0s3

We type the following to take another look at IP version 4 IP addresses on this interface:

ip -4 addr show dev enp0s3

The new IP address is present on this network interface. We jump to another computer and use the following command to see if we can ping new IP address:

ping 192.168.4.44

The IP address responds and returns acknowledgments of receipt to pings. Our new IP address is operational after a simple ip command.

Deleting an IP address

To delete an IP address, the command is almost the same as that to add one, except that you replace add by del, as shown below:

sudo ip addr del 192.168.4.44/24 dev enp0s3

If we type the following to verify, we see that the new IP address has been deleted:

ip -4 addr show dev enp0s3

Use ip with network interfaces

You use the link object to inspect and work with network interfaces. Type the following command to see the interfaces installed on your computer:

ip show link

To see a single network interface, simply add its name to the command, as shown below:

ip show link enp0s3

Starting and stopping links

You can use the option set with high or low to stop or start a network interface option. You should also use sudo, as shown below:

sudo ip link set enp0s3 down

We type the following to take a look at the network interface:

ip show link enp0s3

The state of the network interface is LOW. We can use the up option to restart a network interface, as shown below:

sudo ip link set enp0s3 up

We type the following to perform another quick check of the network interface status:

ip show link enp0s3

The network interface has been restarted and the status is displayed as UP.

Use ip with routes

With the route object, you can inspect and manipulate routes. Routes define where network traffic to different IP addresses is forwarded and through which network interface.

If the destination computer or device shares a network with the sending computer, the sending computer can directly forward the packet to it.

However, if the destination device is not directly connected, the sending computer forwards the packet to the default router. The router then decides where to send the packet.

To see the routes defined on your computer, type the following command:

route ip

Let’s take a look at the information we have received:

default: The default rule. This route is used if none of the other rules match what is sent.
via 192.168.4.1: Routes packets through the device to 192.168.4.1. This is the IP address of the default router on this network.
dev enp0s3: Use this network interface to send packets to the router.
proto dhcp: The routing protocol identifier. DHCP means that routes will be determined dynamically.
metric 100: An indication of the preference of the route over the others. Itineraries with lower metrics are preferred over those with higher metrics. You can use it to give preference to a wired network interface over a Wi-Fi interface.

The second route governs traffic to the IP range of 169.254.0.0/16. It’s a network without configuration, which means that it is trying to configure itself for intranet communication. However, you cannot use it to send packets outside the immediate network.

The principle of zero configuration networks is that they do not depend on the presence and activation of DHCP and other services. Only them need to see TCP / IP in order to self-identify on each of the other devices on the network.

We are going to take a look:

169.254.0.0/16: The range of IP addresses governed by this routing rule. If the computer communicates on this IP range, this rule intervenes.
dev enp0s3: The network interface that traffic governed by this route will use.
scope link: The range is a link, which means that the range is limited to the network to which this computer is directly connected.
metric 1000: This is a high measure and it is not a preferred route.

The third route governs traffic to the IP address range of 192.168.4.0/24. This is the range of IP addresses for the local network to which this computer is connected. It is for communication through, but within this network.

Let’s break it down:

192.168.4.1/24: The range of IP addresses governed by this routing rule. If the computer communicates in this range of IP addresses, this rule triggers and controls the routing of packets.
dev enp0s3: The interface through which this route will send packets.
proto nucleus: Route created by the kernel during automatic configuration.
scope link: Range is a link, which means that the range is limited to the immediate network to which this computer is connected.
src 192.168.4.26: IP address from which the packets sent by this route come.
metric 100: This weak metric indicates a preferred route.

Display information for a single route

If you want to focus on the details of a particular route, you can add the list option and the IP address range of the route to the command as follows:

ip routing list 192.168.4.0/24

Adding a route

We have just added a new network interface card to this computer. We type the following and see it appear as enp0s8:

ip show link

We will add a new route to the computer to use this new interface. First, we type the following to associate an IP address with the interface:

sudo ip addr add 192.168.121.1/24 dev enp0s8

A default route using the existing IP address is added to the new interface. We use the delete option, as shown below, to delete the route and provide its details:

sudo ip route delete default via 192.168.4.1 dev enp0s8

We will now use the add option to add our new route. The new interface will handle network traffic in the IP address range 192.168.121.0/24. We will give it a metric of 100; because it will be the only route that handles this traffic, the metric is roughly academic.

We type the following:

sudo ip route add 192.168.121.0/24 dev enp0s8 metric 100

Now we type the following to see what it gives us:

route ip

Our new route is now in place. However, we still have route 192.168.4.0/24 which points to the enp0s8 interface – we type the following to remove it:

sudo ip route delete 192.168.4.0/24 dev enp0s8

We should now have a new route that points to all traffic destined for the IP range 192.168.121.0/24 via the enp0s8 interface. It should also be the only route that uses our new interface.

We type the following to confirm:

route ip

Route taken, root not taken

The great thing about these orders is that they are not permanent. If you want to erase them, simply restart your system. This means that you can experiment with them until they work the way you want. And it’s a very good thing if you make a terrible mess of your system – a simple reboot will restore order.

On the other hand, if you want the changes to be permanent, you have to do a little more work. Exactly what varies by distribution family, but they all involve changing configuration files.

In this way, however, you can test the commands before making something permanent.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.