Introduction
Before we start talking about linux ip command with examples, let's first understand-What is an IP Command ?
The ip
command is a Linux networking utility used to manage network interfaces, IP addresses, routes, tunnels, and more. It provides extensive functionality for configuring and troubleshooting networking components.
Any Linux system administrator should be familiar with the ip
command, which is a powerful tool for configuring network interfaces. It can be used to bring the interfaces up or down, assign and delete addresses and routes, control ARP cache, and more.
This tutorial uses practical examples and extensive descriptions of the most frequent options to demonstrate how to use the ip command.
How to Use the ip
Command
All contemporary Linux distributions have the iproute2 package, which includes the ip
program.
The ip
command has the following syntax:
ip [ OPTIONS ] OBJECT { COMMAND | help }
The object type you want to control is OBJECT. The following are the most frequently used objects (or sub-commands):
link
(l
) - Network interfaces can be viewed and modified.address
(a
) - IP addresses can be viewed and modified.route
(r
) - The routing table can be viewed and modified.neigh
(n
) - Display and manipulate items in your immediate vicinity (ARP table).
The item might be written in its entirety or in its reduced (short) version. Enter ip OBJECT help
to see a list of commands and arguments for each object.
You must run the commands as root or as a user with sudo access while setting network interfaces. Otherwise, the script will print the following RTNETLINK responses: Operation is not permitted
.
The ip
command does not set persistent configurations. All changes are lost after a system restart. You'll need to edit the distro-specific configuration files or add the instructions to a startup script to make the changes permanent.
Displaying and Modifying IP Addresses
When working with the addr
object, you'll use the following syntax:
ip addr [ COMMAND ] ADDRESS dev IFNAME
The addr
object's most commonly used COMMANDS are show
, add
, and del
.
Display information about all IP addresses
Type the following command to see a list of all network interfaces and their associated IP addresses:
ip addr show
You will get an output like below:
Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:8c:62:44 brd ff:ff:ff:ff:ff:ff
inet 192.168.121.241/24 brd 192.168.121.255 scope global dynamic eth0
valid_lft 2900sec preferred_lft 2900sec
inet6 fe80::5054:ff:fe8c:6244/64 scope link
valid_lft forever preferred_lft forever
If you leave off the show
command and just type ip addr
, you'll get the same result.
Use ip -4 addr
or ip -6 addr
if you only want to see IPv4
or IPv6
addresses.
Display information about a single network interface
Use ip addr show dev
followed by the device name to get information about a specific network interface. To query eth0
, for example, you might type:
ip addr show dev eth0
Assign IP addresses to an interface
Use the following syntax to allocate an IP address to an interface:
ip addr add ADDRESS dev IFNAME
The interface name is IFNAME
, and the IP address you want to assign to the interface is ADDRESS
.
To add address 192.168.121.45
to device eth0
with netmask 24, type:
sudo ip address add 192.168.121.45/24 dev eth0
The command will not provide any output if it is successful. You'll get the message Cannot find device "eth0"
if the interface doesn't exist.
Assign multiple IP addresses to the same interface
You can use ip
to assign several addresses to a single interface. Consider the following scenario:
Output
sudo ip address add 192.168.121.241/24 dev eth0
sudo ip address add 192.168.121.45/24 dev eth0
Type ip -4 addr show dev eth0
or ip -4 a show dev eth0
to validate the IPs are assigned:
Output
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 192.168.121.241/24 brd 192.168.121.255 scope global dynamic eth0
valid_lft 3515sec preferred_lft 3515sec
inet 192.168.121.45/24 scope global secondary eth0
valid_lft forever preferred_lft forever
Remove / Delete an IP address from the interface
To delete an IP address from an interface, use the following syntax:
ip addr dev ADDRESS dev IFNAME
The interface name is IFNAME, and the IP address you want to remove from the interface is ADDRESS.
To remove the address 192.168.121.45/24
from the eth0
device, do the following:
sudo ip address del 192.168.121.45/24 dev eth0
Displaying and Modifying Network Interfaces
Use the link
object to manage and examine the state of network interfaces.
The most typically used commands when working with link
objects are show
, set
, add
, and del
.
Display information about network interfaces
Type the following command to see a list of all network interfaces:
ip link show
Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 52:54:00:8c:62:44 brd ff:ff:ff:ff:ff:ff
Unlike ip addr show
, ip link show
does not provide information about the device's IP addresses.
Use ip link show dev
followed by the device name to retrieve information about a specific network interface. To query eth0
, for example, you might type:
ip link show dev eth0
Output
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 52:54:00:8c:62:44 brd ff:ff:ff:ff:ff:ff
Alter the status of the interface UP/DOWN
Use the ip link set dev
command, followed by the device name and the desired state, to bring interfaces up or down:
ip link set dev {DEVICE} {up|down}
To bring the interface eth0
online, for instance, type:
ip link set eth0 up
And to bring if offline
ip link set eth0 down
Displaying and Altering the Routing Table
The route
object is used to assign, remove, and display the kernel routing table. When working with routes objects, the most typically used commands are list
, add
, and del
.
Display routing table
Use one of the following commands to acquire a list of kernel route entries:
ip route
ip route list
ip route list SELECTOR
If the command is run without a SELECTOR
, it will list all the kernel's route entries:
ip route list
Output
default via 192.168.121.1 dev eth0 proto dhcp src 192.168.121.241 metric 100
192.168.121.0/24 dev eth0 proto kernel scope link src 192.168.121.241
192.168.121.1 dev eth0 proto dhcp scope link src 192.168.121.241 metric 100
If you only want to see the routing for a single network, such as 172.17.0.0/16
, type:
ip r list 172.17.0.0/16
Output
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
Add a new route
Use the route add
command followed by the network or device name to add a new entry to the routing table.
Create a route from the gateway at 192.168.121.1 to 192.168.121.0/24.
ip route add 192.168.121.0/24 via 192.168.121.1
Add a route that can be accessible on device eth0 to 192.168.121.0/24.
ip route add 192.168.121.0/24 dev eth0
Use the term default
to create a default route. The following command will create a default route for device eth0
via the local gateway 192.168.121.1
.
ip route add default via 192.168.121.1 dev eth0
Delete a route
Use the route add
command to remove an entry from the routing database. The syntax for deleting and adding routes is the same.
The default route can be removed with the following command:
ip route del default
Using the gateway at 192.168.121.1, delete a route for 192.168.121.0/24.
ip route add 192.168.121.0/24 via 192.168.121.1
FAQs on Linux ip Command with Examples
How do I check the IP address of my network interface?
You can use the ip addr show
command to view the IP addresses assigned to all network interfaces or specify a particular interface, such as ip addr show eth0
.
How can I assign an IP address to a network interface?
To assign an IP address to a network interface, use the command ip addr add <IP_ADDRESS>/<SUBNET_MASK> dev <INTERFACE>
. For example, ip addr add 192.168.1.100/24 dev eth0
.
How do I bring up/down a network interface?
Use the command ip link set <INTERFACE> up
to bring up a network interface or ip link set <INTERFACE> down
to bring it down.
How can I add a network route?
To add a network route, use the command ip route add <NETWORK>/<SUBNET_MASK> via <GATEWAY> dev <INTERFACE>
. For example, ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
.
How can I delete a network route?
Use the command ip route del <NETWORK>/<SUBNET_MASK> via <GATEWAY> dev <INTERFACE>
to delete a network route.
How can I create a network tunnel with the ip
command?
The ip tunnel
command provides various options to create different types of network tunnels, such as IPsec, IPv6, GRE (Generic Routing Encapsulation), or IPIP (IP over IP).
How do I configure VLAN (Virtual LAN) tagging with the ip
command?
To add a VLAN interface, use the command ip link add link <PHYSICAL_INTERFACE> name <VLAN_INTERFACE> type vlan id <VLAN_ID>
. For example, ip link add link eth0 name eth0.10 type vlan id 10
.
Conclusion
You should have a basic idea of how to use the Linux ip
command at this point. Visit the ip command man page or type man ip
in your terminal for additional information about the other ip options.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.