Linux IP Command with Usage Examples

The IP command is part of iproute package that is by default installed in Modern Linux distributions. In Linux, the IP command is used to manage and display Network Interfaces, IP Addresses, IP Routing Table, and IP Neighbor Entries. It is commonly used to assign an IP address to a network interface and configure network interface settings.

In this tutorial, we will learn Linux IP command with usage examples.

How to use Linux IP command

The following is the syntax of the IP command.

$ ip [ OPTIONS ] OBJECT { COMMAND | help }
$ ip help
IP help command

OBJECT is the object type that you want to manage which includes:

  1. link (l): It displays and modifies network interfaces.
  2. address (a): It displays and modifies IP addresses.
  3. route (r): It displays and alters the routing table.
  4. neigh (n): It displays and modifies ARP table.

You can use the full or short form to execute the command. For example, using ip addr or ip address will display the same output.

1. Display and Manage IP Addresses

The ip addr command allows you to show, assign, delete IP address in a Linux system. The ip addr object uses the following syntax.

ip addr [ COMMAND ] ADDRESS dev IFNAME

Use the help command to find more about address commands.

ip addr help
IP address help command

Display a list of all network interfaces

The ip addr show command list of all the network interfaces and assigned IP addresses.

For example:

ip addr show
IP address show command

To display output for only IPv4 or IPv6, use the following command.

To display IPv4 ip address
ip -4 addr

To display IPv6 ip address
ip -6 addr

Display information about specific network interface

To display information about a specific interface, use the following syntax.

ip addr show dev [Device Name]

For example, to display information about ens33, type the following.

ip addr show dev ens33
IP address show device ens33

Assign an IP address to an interface

Use ip addr add command to assign an IP address to an interface.

Syntax:

ip addr add [ ADDRESS ] dev [Interface Name]

The [ ADDRESS ] is the address you want to assign to the [Interface Name].

For example, to add address 192.168.239.140 with subnet mask 24 to device ens33:

ip addr add 192.168.239.140/24 dev ens33

No output is displayed if the IP address is added to the device or an error is displayed.

Add multiple IP addresses to an interface

To add multiple IP addresses to an interface, you need to use the same command as above multiple times.

For example, to add addresses 192.168.239.140 and 192.168.239.135 with subnet mask 24 to device ens33, type the following.

ip addr add 192.168.239.140/24 dev ens33
ip addr add 192.168.239.135/24 dev ens33

Delete IP address from an interface

To delete an IP address from an interface, use the following syntax.

ip addr del [ ADDRESS ] dev [ Interface Name ]

For example, to delete address 192.168.30.12 with subnet mask 24 from device ens33, type the following.

ip addr del 192.168.239.140/24 dev ens33

No output is displayed if the IP address is deleted from the device or an error is displayed.

2. Display and Manage Network Interfaces

The ip link command is used to manage and show the state of all network interfaces. You can set TCP/IP stack network interface values such as txqueuelen, interface up/down and MTU. We can use ip link set command for this.

To display a list of link object commands, you can use the help command as follows.

$ ip link help
IP link help command

Get Network Interface Information

The ip link show command display information for all network interfaces.

$ ip link show
IP link show command

To display information about a specific device, use the following syntax.

$ ip link show dev [Device Name]

For example, to display network information about ens33 you would use the following.

$ ip link show dev ens33
IP link show device ens33

Set interface status as UP or DOWN

To set the interface up or down, use the following syntax.

ip link set dev [ Device Name ] [ up/down ]

For example, to bring ens33 online, use the following command.

ip link set dev ens33 up

No output is displayed if the device is turned up or an error is displayed.

Similarly, to bring ens33 down, use the following command.

ip link set dev ens33 down

No output is displayed if the device is turned down or an error is displayed.

Change txqueuelen of the device

To change the transmit queue of a device, use the following syntax.

ip link set txqueuelen [ Number ] dev [ Interface Name ]

For example, to set the transmit queue 5 of the ens33 device, use the following command.

ip link set txqueuelen 5 dev ens33

No output is displayed if the transmit queue is set or an error is displayed.

Change MTU

To change the MTU (Maximum Transmit Unit), use the following syntax.

ip link set mtu [ Number ] dev [ Interface Name ]

For example, set the MTU of device ens33 to 1400, use the following command.

ip link set mtu 1400 dev ens33

No output is displayed if the MTU is changed or an error is displayed if the device name does not exist or the MTU is set less than the device minimum.

Change MAC address

To change the MAC address of a device, use the following syntax.

ip link set dev [ Device Name ] address [ MAC ADDRESS ]

Delete Virtual Interface

You can use ip link delete command to delete virtual interface. For example

ip link delete vip

This will delete the virtual interface named vip.

Add bridge and Virtual interface

The ip link add allows to add bridge, virtual machine, and vlan.

Syntax:

ip link add [ link DEVICE ] type TYPE [ ARGS ]
TYPE := [ bridge | can | dummy | ifb | ipoib | macvlan | vcan | veth | vlan | vxlan |
ip6tnl | ipip | sit | gre | gretap | ip6gre | ip6gretap ]

For example:

ip link add br0 type bridge

This creates a bridge named br0.

3. Display and Manage IP Routing Table

The route object is used to display and manage the IP routing table.

To display a list of IP route commands, use the help command as follows.

ip route help
IP Route help command

Show routing table

To get a list of all the route entries, use any one of the following commands.

ip route
ip route list
IP Route list command

The output displayed above is all of the route entries in the kernel. To narrow it down, add the SELECTOR object as follows.

ip route list SELECTOR

To display routing for a specific network, use the following syntax.

ip route list [ IP ADDRESS ]

Add a new route

To add a new route to the routing table, use the following syntax.

ip route add [ IP ADDRESS ] dev [ Interface Name ]

You can add a new route via the gateway using the following syntax.

ip route add [ IP ADDRESS ] via [ Gateway ]

For example, to add a new route 192.168.239.0/24 via the gateway 192.168.239.1, use the following command.

ip route add 132.168.239.0/24 via 192.168.239.1

Delete a route

To delete an existing entry in the routing table, use the following syntax.

ip route del [ IP ADDRESS ]

To delete the default route, use the following command.

ip route del default

You can delete a route via the gateway using the following syntax.

ip route del [ IP ADDRESS ] dev [ Interface Name ]

4. Display and Modify IP Neighbor Entries

To display and modify IP Neighbor Entries, a neigh object is used. To display a list of all neigh commands, use the help command as follows.

ip neigh help
IP Neigh help command

Display neighbor/arp cache

To display a neighbor/arp cache, use the following command.

ip neigh show
IP neigh show command

Add an ARP entry

To add an ARP entry, use the following syntax.

ip neigh add [ IP ADDRESS ] dev [ Interface Name ]

For example, to add 192.168.239.140 to device ens33, use the following command.

ip neigh add 192.168.239.140 dev ens33

Delete an ARP entry

To add an ARP entry, use the following syntax.

ip neigh del [ IP ADDRESS ] dev [ Interface Name ]

For example, to delete 192.168.239.140 from device ens33, use the following command.

ip neigh del 192.168.239.140 dev ens33

Flush ARP entry

The flush or f command flushes the ARP entry. To flush an ARP entry, use the following syntax.

ip -s -s n flush [ IP ADDRESS ]

For example, to flush arp entry for 192.168.239.140, use the following command.

ip -s -s n flush 192.168.239.140

Conclusion

In this tutorial, we covered the Linux IP command with its usage examples. For more information, you can find the manual page of ip command.

Thanks for reading, please provide your feedback and suggestions.

Leave a Comment