ip Command in Linux



The ip command in Linux displays or manipulates routing, devices, policy routing, and tunnels. It provides a unified interface for configuring and managing networks on Linux systems.

The ip command is a part of the iproute2 suite of networking tools. It serves as a more advanced replacement for the older ifconfig command, offering greater functionality and more options. This package comes pre-installed on all recent Linux distributions.

Table of Contents

Here is a comprehensive guide to the options available with the ip command −

Syntax of ip Command

The syntax of the Linux ip command is as follows −

ip [options] <object> <command>

In the syntax, the [options] field is used to specify various options to change the command's behavior. The <object> is used to specify the network component that needs to be viewed or configured. The <command> field is used to specify the command to perform an action on the specified network component.

ip Command Options

The commonly used options of ip command are listed below −

Flags Options Description
-a --all Executes the command on all supported objects
-b --batch Allows running multiple commands from a batch file
-br --brief Displays only essential information
-c Enables color output
-d --details Shows detailed information
--echo Requests feedback from the kernel on applied configurations
-f --family

Specifies the protocol family (for example, inet, inet6, bridge, mpls, link)

  • 4 − Equivalent to -family inet
  • 6 − Equivalent to -family inet6
  • B − Equivalent to -family bridge
  • M − Equivalent to -family mpls
  • 0 − Equivalent to -family link
--force Continues execution in batch mode despite errors
-h

--human or

--human-readable

Displays stats in human-readable formats with suffixes
--iec Shows human-readable rates using IEC units
-j --json Outputs in JSON format.
-l --loops Sets the maximum attempts for ip address flush
-n --netns Switches to a specified network namespace (NETNS)
-N --numeric Outputs numeric values for fields like protocol, and scope
-o --oneline Displays each record on a single line
-p --pretty Adds indentation for JSON output
-r --resolve Resolves hostnames instead of showing addresses
--rc --rcvbuf Sets the netlink socket's receive buffer size
-s

--stats or

--statistics

Increases information detail in output; can be repeated to add more detail
-t --timestamp Includes the current timestamp

--ts or

--tshort

Sets the receive buffer size for the netlink socket
-V --version Displays the ip command's version

The object specifies the network component that needs to be viewed or managed. Some common objects include −

Objects Description
addr a Manage IP addresses on interfaces
link l Manage network interfaces
route r Configure and display routing tables
neigh n Manage ARP and neighbor tables
tunnel t Configure tunnels (e.g., VPN or IP-in-IP tunnels)

The commonly used commands are as follows −

Command Description
show Displays information about the object
add Adds a new entry such as IP address or route
del Deletes an existing entry
set Configures or modifies an existing entry

Examples of ip Command in Linux

In this section, the usage of the ip command in Linux will be discussed with examples −

Displaying IP Address

To display the IP address on Linux, use the ip command in the following way −

ip addr

It will show all the IP addresses, to simplify the output, use the -br or --brief option −

ip -br addr
ip Command in Linux1

To list only IPv4 address, use −

ip -4 addr

To display only IPv6 addresses, use the following command −

ip -6 addr

Displaying Network Interface Information

To display the network interface information, use the ip command with the link as an object −

ip link
ip Command in Linux2

The link object in the above command can also be specified as the letter l.

To display information on only a specific device, use the dev option with the device name. For example, if the device name is enp0s1, then use the ip command in the following way −

ip link show dev enp0s1
ip Command in Linux3

Displaying Interface Statistics

To display interface statistics, use the -s option with the link object −

ip -s link
ip Command in Linux4

To view statistics for a specific interface, use the following command −

ip -s link show dev enp0s1

Setting Interface State

To disable an interface, use the set command with the interface name and down option −

sudo ip link set enp0s1 down

To enable it back, replace down with up

sudo ip link set enp0s1 up

Setting the IP Address of an Interface

To assign an IP address to an interface, use the ip command in the following way −

sudo ip addr add 192.168.1.224/24 dev enp0s1

The above command assigns the IP address 192.168.1.224 with a /24 subnet mask (equivalent to 255.255.255.0) to the enp0s1 interface.

Modifying the MTU (Maximum Transmission Unit)

Adjusting the MTU improves the network performance. The MTU defines the largest data packet. The network rejects packets exceeding the MTU value. For example, to change the MTU on interface enp0s1, to 15000, use the following command −

sudo ip link set mtu 15000 dev enp0s1

To verify the updated MTU value, use the following command −

ip link show dev enp0s1
ip Command in Linux5

Displaying Real-time Network Updates

To display real-time updates and changes to the system's network configuration, use the following command −

ip monitor
ip Command in Linux6

The specific object can also be monitored, for example, to monitor the addr, use −

ip monitor addr

The above command monitors the IP address changes.

Displaying MAC Addresses of the Connected Devices

To display the MAC addresses of the connected devices, use the following command −

ip neigh
ip Command in Linux7

Instead of neigh only n can also be used in the above command.

Changing Transmit Queue

The default transmit queue length for most network interfaces is 1000. To modify the transmit queue length (txqueuelen) for a network interface, you can use the ip command in the following way −

sudo ip link set txqueuelen 1500 dev enp0s1

To view the modification, execute the following command −

ip link show dev enp0s1
ip Command in Linux8

Displaying the IP Routing Table

To display the IP routing table, use the following command −

ip route
ip Command in Linux9

Adding Entry to Routing Table

To add an entry to the routing table, use the following command −

sudo ip route add 192.168.1.100/24 via 192.168.0.254 dev enp0s1

The above command routes all traffic for the 192.168.1.100/24 network through the 192.168.0.254 gateway on enp0s1.

Deleting a Route

To delete an existing route from the routing table, use the following command −

sudo ip route delete 192.168.1.100/24 via 192.168.0.254 dev enp0s1

The above command removes the route to the 192.168.1.100/24 network via the gateway 192.168.0.254 through the enp0s1 interface.

Displaying Colored Output

To display colored output, use the -c option with the ip command −

ip -br -c addr
ip Command in Linux10

Conclusion

The ip command in Linux is a powerful tool to manage networking tasks, replacing the older ifconfig tool. The ip command provides many more options for configuring the network compared to the limited ifconfig command. It allows configuring and viewing IP addresses, routes, network interfaces, and more.

With a wide range of options such as -a for all objects, -br for brief outputs, and -4 or -6 for IPv4 and IPv6, the ip command provides good control over network configurations.

Advertisements