ip Command Examples
One of the most used networking commands that you must know.
The ip command is one of the most used networking commands in Linux and there are multiple reasons why.
Using the ip command, you can get network stats, configure interfaces, manage network routing, and even monitor the network activity.
Sounds like a Swiss army knife for the network. Right? Yes, it is.
So in this tutorial, I will walk you through how you can use the ip command through the following essentials:
- Syntax and common flags of the ip command
- Practical examples of the ip command
Let's start with the first one.
How to use the ip command in Linux
To use the ip command to its maximum potential, it is important to know the syntax so here's a simple syntax you need to follow:
ip [ OPTIONS ] OBJECT [COMMAND]
Looks a little more complex than the traditional Linux commands, right? Let me break it down for you.
[OPTIONS]
: it is used to change the default behavior of the ip command.OBJECT
: here's where you specify the aspect of the network you want to work with. I will be sharing a list of objects in a moment.COMMAND
: here you specify the action you want to perform on the object.
Here's a list of every object which can be used with the ip command:
Object | Description |
---|---|
link |
Manages network devices and their attributes. |
address |
Configures IP addresses and related settings. |
addrlabel |
Manipulates address labels for routing. |
route |
Displays and manages the IP routing table. |
rule |
Manages routing policy rules. |
neigh |
Shows and manages ARP or neighbor objects. |
ntable |
Manages routing tables for policy routing. |
tunnel |
Manages various tunneling interfaces. |
tuntap |
Creates and manages TUN/TAP devices. |
maddress |
Manages multicast addresses. |
mroute |
Displays and manages IP multicast routing. |
mrule |
Manages rules for multicast groups. |
monitor |
Monitors network events and changes. |
xfrm |
Manages the IPsec (XFRM) framework. |
netns |
Manages network namespaces. |
l2tp |
Manages Layer 2 Tunneling Protocol (L2TP). |
tcp_metrics |
Displays and manages TCP metrics. |
token |
Manages token-based interfaces. |
macsec |
Manages MACsec (Media Access Control Security) settings. |
vrf |
Manages Virtual Routing and Forwarding (VRF). |
mptcp |
Manages Multipath TCP settings. |
ioam |
Manages In-situ OAM (Operations, Administration, and Maintenance). |
Looks like a long list. Right? Well, bear with me as another long list is about to appear for the available options for the ip command.
So here's a list of available options with the ip command:
Option | Description |
---|---|
-V |
Print the version of the ip command. |
-h |
Display help information in a human-readable format. |
-s |
Display statistics about network interfaces. |
-d |
Display extra details in the output. |
-r |
Resolve addresses and names. |
-iec |
Use IEC units for displaying bandwidth (KiB, MiB, GiB, etc.). |
-f |
Specify the address family (inet, inet6, or link). |
-4 |
Use IPv4 addresses. |
-6 |
Use IPv6 addresses. |
-B |
Show only bound addresses. |
-0 |
Show no addresses. |
-l <Number of attempts> |
Set the maximum number of address flush attempts. |
-o |
Output one line per command. |
-rc <size> |
Set the netlink socket to receive buffer size. |
-t |
Include timestamps in the output. |
-ts |
Use short timestamps. |
-n <name> |
Specify the name of a network namespace. |
-N |
Display numeric output. |
-a |
Display all available options. |
-c |
Use color in the output. |
-br |
Use brief output. |
-j |
Output in JSON format. |
-p |
Output in a more readable format (pretty-print JSON). |
Now, let's take a look at practical examples of the ip command.
Practical examples of the ip command
In this section, I will walk you through some practical examples that will be helpful to users ranging from basic to advanced users.
1. List all the network interfaces
To list all the network interfaces of your system, you'd have to use the ip command with the link
option as shown here:
ip link show
As you can see, it gets details about the state and functionality of network interfaces on your system.
2. Show information of a specific interface
If you want to show information of a specific interface, then all you have to do is append the interface name to the previous command ip link show
as shown here:
ip link show <Inerface_name>
For example, if I want to know the details of the wlo1
interface, then I will use the following command:
3. Display the routing table
To display the routing table, you use the route
flag with the ip command as shown here:
ip route show
The above output includes the default gateway associated with the wireless interface wlo1
where packets will be sent to the router with the IP address 192.168.1.1
.
Furthermore, it also shows the routing information of the virtual bridge virbr0
and the linkdown
status indicates that the virtual bridge is yet to be activated.
4. Show the IP addresses of each interface
If you want to list the IP address of every interface, then you have to use the addr
flag with the ip command as shown here:
ip addr show
5. Show neighbor table (ARP cache) of every interface
Showing the neighbor table refers to getting valuable information from the devices you're directly connected to on your network such as their MAC addresses.
To show the neighbor table, all you have to do is use the neigh
flag as shown here:
ip neigh show
The above output explains that I have two active interfaces (reachable) and an assigned IP range. It also shows the MAC address of the device to which the interface is directly connected.
6. Enable network interface
To enable the network interface, you use the link set
flag, specify the name of the interface, and then append up
flag as shown here:
sudo ip link set <interface_name> up
For example, if I want to enable the wlo1
interface, then I'll use the following:
sudo ip link set wlo1 up
Once done, you can use the ping command and specify the interface over which to ping to the server:
7. Disable network interface
To disable a network interface, use the link set
with the down
flag as shown here:
sudo ip link set <interface_name> down
Here's how I disabled wlo1
:
sudo ip link set wlo1 down
Pretty simple. Right?
8. Assign an IP address to the network interface
To assign an IP address to a specific network interface, use the addr add
flag as shown here:
sudo ip addr add <IP/subnet> dev <Interface>
For example, here, I added 192.168.1.100/24
IP address for the enp1s0
interface:
sudo ip addr add 192.168.1.100/24 dev enp1s0
In the above output, I use the ip route
to show the routing table and it is working as expected.
9. Remove IP address from the network interface
If you want to remove IP address, it can be done using the addr del
flag as shown:
sudo ip addr del <IP/subnet> dev <Interface>
Here, I removed 192.168.1.100/24
IP address for the enp1s0
interface:
sudo ip addr del 192.168.1.100/24 dev enp1s0
10. Configure static route in the routing table
To configure a static route, you use the route add
command in the following manner:
sudo ip route add <destination_network>/<subnet_mask> via <gateway_ip> dev <interface_name>
For example, here, I created a static route for 10.0.0.0/24
via 192.168.122.224
:
sudo ip route add 10.0.0.0/24 via 192.168.122.224 dev enp1s0
11. Flush routing cache
To flush the routing cache, you need to use the ip command with route
, flush
and cache
flags. Looks confusing? Here's how it looks:
sudo ip route flush cache
12. Monitor network continuously
This is quite helpful when you have to monitor and print networking events. For that purpose, you'd have to use the monitor
flag as shown here:
ip monitor all
Wrapping Up...
In this tutorial, I went through multiple examples of how you can use the ip command in Linux. If the examples are not enough, then you can refer to the given table where I've mentioned every option possible.
I hope you will find this guide helpful.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.