The Linux IP command is a versatile tool that helps users in configuring network interfaces of Linux systems. It is the preferred command over the ifconfig command which is now deprecated. The IP utility is used to perform a wide range of network administration tasks. You can use it to modify network interfaces and configure network parameters such as the IP address of a network interface, manage the routing table and ARP cache, and configure the MTU.
In this tutorial, we learn about Linux IP command and demonstrate, through examples, how you can use this powerful tool to perform several network administration tasks.
How to use the IP Command in Linux
The IP command is provided by default in modern Linux operating systems, and hence no installation is required.
The IP command takes the following basic format:
ip [OPTION] OBJECT {COMMAND | help}
The OPTION
refers to the various flags you can use with the command to modify the output. The OBJECT
refers to the various subcommands that you can use alongside the IP command.
Some of the widely used sub-commands include:
- address( addr / a ) - This is used to view/display or change an IP address.
- link ( l ) - Used to display the state of a network interface.
- route ( r ) - Used to view or modify the routing table.
- neigh ( n ) - Displays and modifies the ARP table ( neighbor objects ).
To display a list of arguments for each of these objects use the syntax:
ip OBJECT help
Manage IP Addresses
Another common use of the IP command is managing IP addresses associated with network interfaces. This includes displaying IP addresses, configuring static routing, and even deleting the ip addresses from the network interfaces. Let us have a look at how you can accomplish each of these.
Display information about IP Addresses
To display or view a list of all the network interfaces alongside their associated IP addresses, run the command:
ip address
OR
ip addr
OR
ip addr show
The output provides you with all the network interfaces (including the loopback interface) their associated IPv4 and IPv6 addresses alongside other information such as the maximum transmission unit (mtu) and transmit queue length (txqueuelen).
To narrow the output to just one network interface, use the syntax:
ip address show interface-name
For example, to view the IP address of interface ens3
, run:
ip address show ens3
In addition, you can use the -4 or -6 options to specify protocol address selection. For example, to display IPv4 addresses, run:
ip -4 a
To display IPv6 addresses, type:
ip -6 a
Add IP Address to an interface
To add an IP address to a network interface, use the following command where the ip_address should be accompanied by a subnet mask.
ip addr add [ip_address] dev [interface-name]
For example, to assign an ip address e.g 192.168.92.150
and netmask 255.255.255.0
to the ens33
network interface run the command:
sudo ip addr 192.168.92.150/255.255.255.0 dev ens33
To view the assigned ip address, once again, run:
ip addr show ens33
Alternatively, you can use the CIDR notation instead of specifying the netmask.
sudo ip addr 192.168.92.150/24 dev ens33
To add a broadcast address to an interface, use the syntax:
ip addr add brd [ip-address] dev [interface-name]
Delete IP address from an interface
If you wish to delete an IP address from a network interface use the syntax as follows:
ip addr del [ip_address] dev [interface]
For example, to delete the IP address we have just assigned to the interface, we will run the command:
sudo ip addr del 192.168.92.150/24 dev ens33
For more ip addr
command options, run the command:
ip addr help
How to manage network interfaces
We will begin by looking at how you can manage network interfaces. This includes displaying network interface information and modifying the status of the interfaces.
Display network interface information
To list all the network interfaces on your system, use the link
subcommand. It manages and displays the status of all the network interfaces. It allows you to display interface information such as the interface name, state (whether UP or DOWN) maximum transmission unit (MTU), and transmission queue (txqueuelen).
To view the information of all the network interfaces on your system, run the following commands:
ip link
OR
ip link show
From the output shown below, you can see information about all the network interfaces including the loopback address (lo) and ens33.
To narrow down and display information about a specific network interface name, use the syntax
ip link show dev interface-name
For example, to display link information of ens33
we will execute the command.
ip link show dev ens33
To monitor network traffic such as the total number of packets transmitted and received and even errors, of all the interfaces, use the -s
option.
ip -s link
Similarly, you can display the same information for a single network interface:
ip -s link ls ens33
To display all running network interfaces execute:
ip link ls up
Modify Network Interface Status
The ip link
command can also be used to change the status of a network interface from UP to DOWN or vice versa.
To bring down or deactivate an interface, use the syntax as follows. Be advised that you need sudo or root privileges to change the state of an interface.
sudo ip link set interface-name down
For example, to deactivate interface ens33
, run the command
sudo ip link set ens33 down
To confirm the interface is deactivated, you can try pinging a remote host. You should get the alert: ‘Network is unreachable’.
To activate or bring an offline ip link up, run the command:
sudo ip link set ens33 up
Change the txqueuelen
The IP command can also be used to modify the transmit queue length - txqueuelen for a network interface. . This is the network interface value that sets the number of packets allowed per kernel transmit queue of an interface.
By default, this value is set to 1000, and the txqueuelen is denoted by the qlen parameter. You can verify this using the following commands:
ip address
OR
ip addr
OR
ip a
To change the txqueuelen
value, use the syntax:
sudo ip link set txqueuelen [number] dev [interface]
For example, to set the txqueuelen value to 5000, run the command
sudo ip link set txqueuelen 5000 dev ens33
To confirm the change, run:
ip a show ens33 | grep -i qlen
Change the MTU
MTU, short for Maximum Transmission Unit, is the size of the largest protocol data unit ( PDU ) that can be transmitted in a network. By default, this is set to 1500 bytes on most Linux systems.
ip a show ens33
To change or set the MTU of a network interface, use the syntax:
sudo ip link set mtu [number] dev [interface]
For example, to set the MTU of interface ens33
to 2000 run the command:
sudo ip link set mtu 2000 dev ens33
For additional options with the command run the help command shown.
ip link help
Manage IP Routing Table
In managing the routing tables, the route
object is normally used along with these three sub-commands: list, add, and del. These sub-commands are used when displaying the kernel routing tables, adding and deleting routes respectively.
Display IP Routing Table
To view the routing table, run the following command
ip route
OR
ip route list
To view the route entries for a specific network, for instance, 192.168.92.0/24
run the command:
ip route list 192.168.92.0/24
Modify IP Routing Table
You can add a new route to the routing table using the syntax:
ip route add ip-address via default gateway-address
For instance, to add a routing table entry such as 192.168.92.0/24
via 192.168.92.1
, run the following command. Here, 192.168.92.1
is the default gateway. In most cases, the default gateway is the ip address of the default router on the local network.
ip route add 192.168.92.0/24 via 192.168.92.1
To confirm the routing entries, type:
ip route
Suppose you want to add a default route, via a local gateway such as 192.168.92.1
to be accessed on eth0
, type:
ip route add default via 192.168.92.1 dev eth0
Delete an IP Route
To remove or delete a route entry from the routing table, run the following command:
ip route del 192.168.92.0/24 via 192.168.92.1
To delete a default route, execute the following command:
ip route del default
For more ip route command options, run the command:
ip route help
Manage Neighbor Entries
Neighbor entries are synonymous with ARP ( Address Resolution Protocol) Tables. ARP is an internet protocol that connects a link-layer address such as a MAC address to a corresponding IPv4 address.
The ip neigh ( you can also use ip neigh, or ip neighbor) command allows you to display and manage ARP cache. In this section, we shall see how you can display and modify neighbor entries.
Display IP Neighbor Entries
To display existing entries in the ARP table, run the following command:
ip neigh show
A device can be in 4 states:
- REACHABLE - This indicates a valid entry.
- PERMANENT - This is a persistent entry that can only be removed by the administrator.
- STALE - This is a valid yet unreachable entry. The Linux kernel needs to look for it at the first transmission.
- DELAY - This is a state that indicates that the kernel is on standby waiting for validation from the stale entry.
Modify IP Neighbor Entries
To add a new entry to the ARP table, use the syntax:
ip neigh add [ip_address] lladdr mac-address dev [interface]
For example, to add an entry of network device with IP 192.168.0.2 and MAC address A4:C3:F0:9F:56:B9 run the following command.
ip neigh add 192.168.0.2 lladdr A4:C3:F0:9F:56:B9 dev ens33
To delete an entry, use the following syntax:
ip neigh del [ip_address] dev [interface]
Example:
ip neigh del 192.168.0.2 dev ens33
For additional options of the ip neigh
command, use the following syntax.
ip neigh help
Conclusion
That was a round-up of widely used IP commands. As you have seen, the IP command is a useful tool in configuring network interface parameters such as IP addresses, MTU as well as modifying the kernel routing table. It performs similar tasks to the ifconfig command and ships with modern Linux distributions hence no installation is required.
It’s our hope that by now you have a clear understanding of the Linux IP command. For more command options simply visit the man pages by running the man ip command.
Comments