Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use IP Command in Linux with Examples?
The IP command is a powerful tool for network configuration in Linux. It is used to show, manipulate routing, devices, policy routing, and tunnels. The IP command is part of the iproute2 package, which is installed by default in most Linux distributions. It replaces legacy tools like ifconfig and route, providing a unified interface for network management.
Displaying IP Addresses
To display the IP address of all network interfaces, use the following command
Example
ip addr show
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 pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:15:64:b3 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86378sec preferred_lft 86378sec
You can also view a specific interface using ip addr show eth0 or use the short form ip a.
Managing IP Addresses
Adding an IP Address
To add an IP address to a network interface, use the following command
sudo ip addr add 192.168.1.100/24 dev eth0
This command adds the IP address 192.168.1.100 to the network interface eth0. The /24 at the end of the IP address is the subnet mask in CIDR notation.
Deleting an IP Address
To delete an IP address from a network interface, use the following command
sudo ip addr del 192.168.1.100/24 dev eth0
This command removes the IP address 192.168.1.100 from the network interface eth0.
Route Management
Displaying the Routing Table
To display the routing table, use the following command
ip route show
Output
default via 192.168.1.1 dev eth0 proto static 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
Adding a Route
To add a route, use the following command
sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
This command adds a route to the network 192.168.2.0/24 via the gateway 192.168.1.1 on the network interface eth0.
Deleting a Route
To delete a route, use the following command
sudo ip route del 192.168.2.0/24
Network Interface Management
Displaying Network Interfaces
The ip link show command displays information about the 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 pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:15:64:b3 brd ff:ff:ff:ff:ff:ff
Changing Interface State
To change the state of a network interface, use the following commands
sudo ip link set eth0 down sudo ip link set eth0 up
The first command brings the network interface eth0 down, and the second command brings it back up.
Changing the MTU
To change the Maximum Transmission Unit (MTU) of a network interface
sudo ip link set eth0 mtu 1400
Network Statistics and Information
Displaying Interface Statistics
The ip -s link command displays statistics for the network interfaces
ip -s link
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 RX: bytes packets errors dropped overrun mcast 166114 1982 0 0 0 0 TX: bytes packets errors dropped carrier collsns 166114 1982 0 0 0 0
Displaying Neighbour Table (ARP)
The ip neigh command displays neighbour objects (ARP table)
ip neigh
Output
192.168.1.1 dev eth0 lladdr 00:14:bf:b1:cb:31 REACHABLE
You can add or delete neighbour entries using
sudo ip neigh add 192.168.1.101 lladdr 1:2:3:4:5:6 dev eth0 sudo ip neigh del 192.168.1.101 dev eth0
Network Namespaces
Network namespaces provide isolation of network resources. Each namespace has its own network interfaces, routing tables, and firewall rules.
Managing Namespaces
# List existing namespaces ip netns # Create a new namespace sudo ip netns add mynamespace # Execute commands in a namespace sudo ip netns exec mynamespace ip addr # Delete a namespace sudo ip netns del mynamespace
Common IP Command Options
| Command | Description | Short Form |
|---|---|---|
ip addr show |
Display IP addresses | ip a |
ip link show |
Display network interfaces | ip l |
ip route show |
Display routing table | ip r |
ip neigh show |
Display ARP table | ip n |
Key Features
Unified Interface Single command for all network operations
Feature Rich Supports advanced networking features like namespaces and policy routing
Non-persistent Changes Changes are lost after reboot unless saved to configuration files
Human and Machine Readable Output can be formatted for scripts using
-j(JSON) option
Conclusion
The IP command is a comprehensive tool for Linux network management that replaces legacy utilities. It provides unified access to configure interfaces, routes, and advanced networking features like namespaces. While changes are temporary by default, the IP command is essential for modern Linux network administration and troubleshooting.
