Linux ip Command with Examples

September 12, 2024

Introduction

The ip command is a Linux networking tool for configuring network interfaces. It was designed to extend the functionality and simplify the syntax of the older ifconfig command, which is still present on some systems as part of the net-tools package.

This tutorial introduces the ip command, lists examples of its usage, and provides a downloadable PDF cheat sheet for quick reference.

Linux ip command with examples.

Prerequisites

  • Command-line access.
  • Administrative privileges.
  • The iproute2 package installed.

Linux ip Command Syntax

The ip command accepts objects (subcommands), options, and additional arguments, allowing users to configure and manage network interfaces, routes, tunneling, etc. Below is the basic command syntax:

ip [option] [subcommand] [arguments]

The following list introduces the most frequently used ip command objects:

  • link (l) configures network interfaces.
  • address (addr/a) modifies protocol addresses (IP, IPv6).
  • route (r) alters routing tables.
  • neigh (n) manipulates neighbor objects.

The commands can be executed using either full or abbreviated object forms. For example, ip link and ip l will give the same results.

To see a complete list of ip objects, type in the following command:

ip help
The output of the main ip help command.

Note: The system does not permanently retain the modifications made with the ip command. Once the user restarts a Linux Server, the configuration reverts to the defaults.

To make adjustments permanent, add the commands to a startup script or edit the distro-specific configuration files.

ip vs. ifconfig

The ip and ifconfig commands are used for network interface configuration in Linux. However, ip provides additional functionalities and a more consistent syntax. The list below contains the main points of comparison between ip and ifconfig:

  • ifconfig is part of the older net-tools package that is not actively maintained. ip belongs to the newer iproute2 package.
  • ifconfig uses ioctl system calls to communicate with the kernel while ip uses more efficient Netlink sockets.
  • ifconfig syntax is simple but lacks consistency. ip features more consistent hierarchical syntax.
  • While ifconfig focuses on network interface configuration, ip adds the possibility of route, address, and tunnel management.
  • ifconfig has limited support for the IPv6 protocol, while ip supports it fully.
  • ifconfig displays enabled interfaces only. ip can show both enabled and disabled interfaces.

Linux ip Command Options

The ip command options change the command's behavior, control the information the command provides, and format the output to make it more readable. The table below lists all the available options:

OptionDescription
-a, -allExecutes the command on all objects (supported commands only).
-b, -batchAllows the user to provide a batch file with multiple commands and executes them all.
-br, -briefPrints essential information only.
-cShows color output.
-d, -detailsPrints detailed information.
-echoRequests the kernel to echo back the applied configuration.
-f, -familyAllows the user to specify the protocol family. Available options are inet, inet6, bridge, mpls, and link. Each family has a shortcut:
-4 is short for -family inet,
-6 is -family inet6,
-B stands for -family bridge,
-M for -family mpls, and
-0 for -family link.
-forceIn batch mode, continues executing ip even if there are errors in the command execution.
-h, -human, -human-readablePrints stats with suffixed human-readable values.
-iecShows human-readable rates using IEC units.
-j, -jsonPrints output using JSON format.
-l, -loopsAllows the user to set the maximum number of tries ip address flush should attempt before stopping.
-n, -netnsSwitches the ip command to the NETNS network namespace.
-N, -NumericPrints numerical values for protocol, dsfield, scope, etc.
-o, -onelineUses one line per record.
-p, -prettyAdds indentation in JSON documents.
-r, -resolveOutputs DNS names instead of host addresses.
-rc, -rcvbufSets the receive buffer size for the netlink socket.
-s, -stats, -statisticsShows more information in the command output. Can be used multiple times within a single command to increase the amount of the information provided.
-t, -timestampShows current time.
-ts, -tshortSets the receive buffer size for the Netlink socket.
-V, -VersionPrints the version of the ip utility.

ip Command Cheat Sheet

We compiled a list of the most frequently used commands and created a one-page downloadable PDF cheat sheet. Click the button below to download the cheat sheet and use it as a reference when working with the ip command.

ip Command Examples

The following sections provide an overview of the common ip command operations. The accompanying examples illustrate the use of the command and clarify the syntax.

Get Network Interface Information

Type the following command to see link-layer information for all devices that have a driver loaded:

ip link show

The output shows a numbered list of devices with their configuration parameters.

Link-layer information for all devices.

To see a list of running interfaces only, use the syntax below:

ip link ls up

To see information for one specific device:

ip link show dev [device]

For example, to view the device named enp0s3, enter:

ip link show dev enp0s3

The output shows enp0s3 information:

Link-layer information for a single device.

View Interface Statistics

To see statistics for all network interfaces, use the -s option:

ip -s link

The device list now displays details such as transferred packets, dropped packets, and errors.

Using the -s option to show more information for all devices.

To see this information for an individual network interface, enter the following:

ip -s link ls [interface]

For example, use the command below to view the statistics for the enp0s3 device:

ip -s link ls enp0s3
Using the -s option to show more information for a single device.

To print more detailed stats, add another -s to the syntax:

ip -s -s link ls enp0s3

The output now features dedicated lines for RX and TX errors.

Using the -s option twice to show even more information for a single device.

Disable Interface (Offline)

Use the link object to enable or disable network interfaces. For example, to disable the previously mentioned enp0s3 interface, enter:

sudo ip link set enp0s3 down

If successful, the command produces no output. To see the results of the operation, list the available interfaces:

ip link show

The enp0s3 interface no longer shows the UP status, meaning it is disabled.

The output shows that the device is not up.

Enable Interface (Online)

The following command brings the disabled enp0s3 interface online:

ip link set enp0s3 up

The command produces no output unless an error occurs.

Modify Transmit Queue

Speeding up or slowing down a network interface is performed by modifying the transmit queue length (txqueuelen) number. By default, the txqueuelen number is 1000, corresponding to the number of frames that can be on the transmission queue. Change the txqueuelen number by using the following syntax:

sudo ip link set txqueuelen [number] dev [interface]

For example, type the following to increase the number of queued frames to 10000 on the enp0s3 device:

sudo ip link set txqueuelen 10000 dev enp0s3

The successful operation produces no output. Use ip link show to see the device's parameters:

ip link show dev enp0s3

The command output lists txqueuelen number as qlen.

The output shows that the qlen value has changed to 10000.

Set Maximum Transmission Unit (MTU)

Another way to improve network performance is to adjust the Maximum Transmission Unit (MTU) number. It defines the number of bytes for the largest data packet, i.e., the network device rejects packets exceeding the MTU value.

The default MTU value is 1000. Type the command below to change the MTU number:

sudo ip link set mtu [number] dev [interface]

For example, enter the following to increase the MTU to 10000 on the enp0s3 device.

sudo ip link set mtu 10000 dev enp0s3

The command produces no output, but the change is visible in the output of the ip link show command:

ip link show dev enp0s3
The output shows that the mtu value has changed to 10000.

Note: Check out our comprehensive Linux network commands list with a downloadable PDF.

Change MAC Address

The ip command provides a way to change the MAC address of an interface:

1. Disable the interface:

ip link set dev [interface] down

2. Change the MAC address:

ip link set dev [interface] address [mac_address]

3. Re-enable the interface:

ip link set dev [interface] up

Monitor IP Addresses 

Use the address object to list all network interfaces and the associated IP addresses:

ip addr
The ip addr command displays all devices.

Alternatively, achieve the same result by adding the show subcommand:

ip addr show

Provide an interface name to view information about the specific network. For example, type the following to see info about the enp0s3 interface:

ip addr show dev enp0s3
The ip addr command shows information about a single network.

Use the -4 option to list the IPv4 addresses only:

ip -4 addr
The ip addr command shows information about IPv4 networks.

List only IPv6 addresses with the -6 option:

ip -6 addr
The ip addr command shows information about IPv6 networks.

Add IP Address to Interface

Add an IP address to an existing interface by using the following syntax:

sudo ip addr add [ip_address] dev [interface]

The example below adds a 10.0.2.12 IP address to the enp0s3 interface:

sudo ip addr add 10.0.2.12 dev enp0s3

The added address is visible in the network interface list:

ip a
The ip a command shows that an IP address has been added to a device.

To add a broadcast address to an interface, use the command below:

sudo ip addr add brd [ip_address] dev [interface]

Remove IP Address from Interface

Remove an IP address from an interface by typing:

ip addr del [ip_address] dev [interface]

When typing the address, specify the prefix length (e.g., 10.0.2.12/32) to avoid a warning message. For example, to delete the IP address added in the previous section, enter:

ip addr del 10.0.2.12/32 dev enp0s3

Display IP Routing Table

The route object manages route entries on the system. View all the route entries using the following command:

ip route
A list of all route entries.

Alternatively, use the list subcommand:

ip route list

Narrow down the search by adding the SELECTOR object:

ip route list [selector]

The following is a list of available selectors:

  • root [prefix]
  • match [prefix]
  • exact [prefix]
  • table [table_id]
  • proto [rt_proto]
  • type [type]
  • scope [scope]

For example, the following command displays the local routing table, i.e., routes for addresses connected directly to the local machine:

ip route list table local
The ip route command shows the local table.

To view routing for a specific network, provide the network IP address as the argument to the ip route list command:

ip route list [ip_address]

Add New Entry to Routing Table

To add a static routing table entry to a specific device, enter the command below:

ip route add [ip_address] dev [interface]

To add a new route using a gateway address, type:

ip route add [ip_address] via [gateway_IP] dev [interface]

The command below sends all traffic for the 192.168.0.0/24 IP address on enp0s3 through the 192.168.0.254 gateway:

sudo ip route add 192.168.0.0/24 via 192.168.0.254 dev enp0s3

To make the rule default, add the default argument:

sudo ip route add default 192.168.0.0/24 via 192.168.0.254 dev enp0s3

Delete Existing Routing Table Entry

Delete an existing entry in a routing table by using the ip route del subcommand:

ip route del [ip_address]

To delete the default route, type:

ip route del default

Display IP Neighbor Entries

The ip neigh object manages neighbor tables. Display the available neighbor tables by entering:

ip neigh show
A list of neighbor tables showing a single stale entry.

The output shows the MAC addresses and the state of devices in the system. A device entry can be in one of the following states:

  • REACHABLE. A valid, reachable entry until the timeout expires.
  • PERMANENT. An entry that remains in the cache indefinitely unless removed by an administrator.
  • STALE. A valid but unreachable entry. Before sending packets to a stale neighbor, the system verifies the reachability.
  • DELAY signifies that the kernel is still waiting for the stale entry validation.

Add or Remove ARP Table Entries

The neigh object allows the user to manually add an entry to the Address Resolution Protocol (ARP) table. Use the following syntax for this operation:

ip neigh add [ip_address] dev [interface]

To manually delete an ARP table entry, type:

ip neigh del [ip_address] dev [interface]

Print Colored Output

Use the -c option to color the output of any ip command. The following example shows how to color the output of the ip link command:

ip -c link
Colored output of the ip command.

Conclusion

After reading this article, you know how to use the ip command to administer a network on a Linux system. The article also provided a downloadable PDF cheat sheet that helps you recall the essential ip commands.

Next, discover more Linux Commands in our Cheat Sheet and Tutorial With Examples.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Find or Check Your IP Address in Linux
November 17, 2023

An IP address is a code assigned to a computer on a network. It works as a postal...
Read more
How to Check the OpenSSL Version Number
March 28, 2024

OpenSSL is an open-source cryptographic library and SSL toolkit. The applications...
Read more
Nmap Commands - 17 Basic Commands for Linux Network
May 14, 2019

Nmap stands for Network Mapper. It is an open...
Read more
Best Tools to Monitor Network Bandwidth on a Linux Server
September 19, 2024

There are many different tools for monitoring network traffic...
Read more