What it is
The ip command is a powerful utility for configuring and inspecting network interfaces, routing tables, ARP tables, and other network-related information on Linux systems. It’s the modern replacement for older tools like ifconfig and route.
Installation
ip is part of the iproute2 package, which is typically installed by default on most Linux distributions.
Debian/Ubuntu:
sudo apt update
sudo apt install iproute2
Fedora/CentOS/RHEL:
sudo dnf install iproute2
# or
sudo yum install iproute2
Arch Linux:
sudo pacman -S iproute2
macOS:
ip is a Linux-specific command. For similar functionality on macOS, you would use ifconfig, route, and arp.
Windows:
ip is a Linux-specific command. For similar functionality on Windows, you would use ipconfig, route, and arp.
Core Concepts
-
Objects:
ipoperates on various network "objects" which are specified as the first argument afterip. Common objects include:link: Network interfaces (e.g.,eth0,wlan0).address(oraddr): IP addresses assigned to interfaces.route: Routing table entries.neighbour(orneigh): ARP cache entries (IP to MAC address mappings).rule: IP routing policy database rules.xfrm: IPsec security policies.
-
Actions: For each object, you can perform actions like
add,del,show,flush,monitor.
Commands / Usage
Managing Network Interfaces (link)
-
Show all network interfaces:
ip link showDisplays a list of all network interfaces, their state (UP/DOWN), MAC address, and MTU.
-
Show a specific network interface:
ip link show eth0Displays detailed information about the
eth0interface. -
Bring an interface UP:
sudo ip link set eth0 upActivates the
eth0network interface. -
Bring an interface DOWN:
sudo ip link set eth0 downDeactivates the
eth0network interface. -
Change the MTU of an interface:
sudo ip link set eth0 mtu 1500Sets the Maximum Transmission Unit (MTU) for
eth0to 1500 bytes. -
Set the MAC address of an interface (requires interface to be DOWN):
sudo ip link set eth0 down sudo ip link set eth0 address 00:11:22:33:44:55 sudo ip link set eth0 upChanges the MAC address of
eth0. -
Add a virtual network interface (VLAN):
sudo ip link add link eth0 name eth0.100 type vlan id 100 sudo ip link set eth0.100 upCreates a VLAN sub-interface named
eth0.100on top ofeth0for VLAN ID 100 and brings it up. -
Delete a virtual network interface:
sudo ip link del eth0.100Removes the
eth0.100virtual interface. -
Monitor link state changes:
ip link monitor eth0Watches for changes in the state of the
eth0interface (e.g., cable plugged/unplugged).
Managing IP Addresses (address / addr)
-
Show all IP addresses:
ip address show # or shorter ip aLists all IP addresses configured on all network interfaces, including IPv4 and IPv6.
-
Show IP addresses for a specific interface:
ip address show eth0 # or shorter ip a show eth0Displays IP addresses assigned to the
eth0interface. -
Add an IPv4 address:
sudo ip address add 192.168.1.100/24 dev eth0Assigns the IP address
192.168.1.100with a subnet mask of255.255.255.0to theeth0interface. -
Add an IPv6 address:
sudo ip address add 2001:db8::1/64 dev eth0Assigns the IPv6 address
2001:db8::1with a prefix length of64to theeth0interface. -
Delete an IP address:
sudo ip address del 192.168.1.100/24 dev eth0Removes the specified IP address from the
eth0interface. -
Flush all IP addresses from an interface:
sudo ip address flush dev eth0Removes all IP addresses from the
eth0interface. -
Flush all IP addresses from all interfaces:
sudo ip address flush allRemoves all IP addresses from all network interfaces.
-
Change the primary IP address (makes it the first one listed, used for some applications):
sudo ip address change 192.168.1.100/24 dev eth0 to 192.168.1.101/24Changes the IP address from
192.168.1.100to192.168.1.101oneth0.
Managing Routing Tables (route)
-
Show the main routing table:
ip route show # or shorter ip rDisplays the kernel’s main routing table, showing how packets are directed to different networks.
-
Show the routing table for a specific network namespace (advanced):
ip route show netns mynetnsShows routes within the
mynetnsnetwork namespace. -
Add a default route:
sudo ip route add default via 192.168.1.1 dev eth0Sets the default gateway to
192.168.1.1via theeth0interface. -
Add a route to a specific network:
sudo ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0Adds a route for the
10.0.0.0/8network, directing traffic through192.168.1.254viaeth0. -
Add a route with a specific device:
sudo ip route add 172.16.0.0/16 dev tun0Adds a route to
172.16.0.0/16using thetun0interface directly (no gateway specified, implies interface handles routing). -
Delete a default route:
sudo ip route del default via 192.168.1.1 dev eth0Removes the default route pointing to
192.168.1.1. -
Delete a route to a specific network:
sudo ip route del 10.0.0.0/8 via 192.168.1.254 dev eth0Removes the route for the
10.0.0.0/8network. -
Flush the entire routing table:
sudo ip route flush allClears all entries from the main routing table.
-
Flush routes for a specific network:
sudo ip route flush 192.168.1.0/24Removes all routes related to the
192.168.1.0/24network. -
Add a blackhole route (discard packets):
sudo ip route add blackhole 1.2.3.4/32Discards all traffic destined for
1.2.3.4. -
Add a route for a specific table (advanced, for policy routing):
sudo ip route add 10.10.10.0/24 via 192.168.1.2 dev eth0 table 100Adds a route to table
100.
Managing ARP Cache (neighbour / neigh)
-
Show the ARP cache (neighbour table):
ip neighbour show # or shorter ip nDisplays the ARP cache, showing mappings between IP addresses and MAC addresses for directly connected devices.
-
Show ARP entries for a specific interface:
ip neighbour show dev eth0Lists ARP entries associated with the
eth0interface. -
Add an ARP entry:
sudo ip neighbour add 192.168.1.50 lladdr 00:AA:BB:CC:DD:EE dev eth0 nud permanentManually adds a permanent ARP entry mapping
192.168.1.50to MAC address00:AA:BB:CC:DD:EEoneth0.nud permanentmeans it won’t time out. -
Delete an ARP entry:
sudo ip neighbour del 192.168.1.50 dev eth0Removes the ARP entry for
192.168.1.50frometh0. -
Flush the entire ARP cache:
sudo ip neighbour flush allClears all entries from the ARP cache.
-
Flush ARP entries for a specific interface:
sudo ip neighbour flush dev eth0Removes all ARP entries associated with the
eth0interface.
Managing Policy Routing Rules (rule)
-
Show routing policy rules:
ip rule show # or shorter ip ruleDisplays the rules used by the IP routing policy database. These rules determine which routing table to consult based on criteria like source IP, destination IP, or interface.
-
Add a routing rule:
sudo ip rule add from 192.168.2.0/24 table 100 priority 1000Adds a rule: if the source IP is
192.168.2.0/24, use routing table100.prioritydetermines the order of rule evaluation (lower numbers are evaluated first). -
Delete a routing rule:
sudo ip rule del from 192.168.2.0/24 table 100Removes the rule that directs traffic from
192.168.2.0/24to table100. -
Flush all routing rules:
sudo ip rule flush allRemoves all rules from the policy routing database.
Monitoring Network Events (monitor)
-
Monitor all network events:
sudo ip monitor allContinuously displays all network-related events, such as interface state changes, address additions/deletions, and route changes.
-
Monitor specific object types:
sudo ip monitor link address routeMonitors only link, address, and route events.
Network Namespaces (Advanced)
-
List available network namespaces:
ip netns listShows all currently active network namespaces on the system.
-
Create a new network namespace:
sudo ip netns add mynetnsCreates a new, isolated network namespace named
mynetns. -
Delete a network namespace:
sudo ip netns del mynetnsRemoves the network namespace
mynetns. -
Execute a command within a network namespace:
sudo ip netns exec mynetns ip addr showRuns the
ip addr showcommand within themynetnsnetwork namespace. -
Enter a network namespace’s shell:
sudo ip netns exec mynetns bashOpens a new bash shell inside the
mynetnsnetwork namespace.
Common Patterns
-
Assigning an IP and setting default route (typical DHCP-like setup):
sudo ip address add 192.168.1.50/24 dev eth0 sudo ip route add default via 192.168.1.1 dev eth0Configures a static IP and default gateway for a network interface.
-
Setting up a point-to-point link (e.g., VPN tunnel):
# Assuming tun0 is already up and has an IP sudo ip address add 10.0.0.1/24 dev tun0 sudo ip route add 10.0.0.2/32 via 10.0.0.1 dev tun0Configures an IP and a route for a point-to-point interface.
-
Checking connectivity to a specific IP:
ip neigh show 8.8.8.8Checks if the system has an ARP entry for
8.8.8.8, indicating it’s on the local network. If not, it will likely try to resolve it. -
Creating a bridge interface and adding a port:
sudo ip link add br0 type bridge sudo ip link set br0 up sudo ip link set eth0 master br0 sudo ip link set eth0 upCreates a network bridge
br0, brings it up, and assignseth0as a port to it. -
Adding an IP address to a bridge:
sudo ip address add 192.168.1.1/24 dev br0Assigns an IP address to the bridge interface itself.
-
Using multiple interfaces for different networks (policy routing):
# Assuming eth0 (192.168.1.0/24) and eth1 (10.0.0.0/24) are configured # Add a rule to use table 100 for traffic originating from 10.0.0.0/24 sudo ip rule add from 10.0.0.0/24 table 100 priority 1000 # Add a default route for table 100 via eth1's gateway sudo ip route add default via 10.0.0.254 dev eth1 table 100 # Ensure eth1 is up and has an IP sudo ip link set eth1 up sudo ip address add 10.0.0.1/24 dev eth1This setup allows traffic from the
10.0.0.0/24network to be routed independently.
Gotchas
- Permissions: Most
ipcommands that modify network configuration require root privileges (sudo). ifconfigvsip: Whileipis the modern standard, many older scripts and tutorials still useifconfig. Be aware of the differences in syntax and capabilities. For example,ifconfig eth0 upis equivalent toip link set eth0 up.- Subnet Masks:
ipuses CIDR notation (e.g.,/24) for subnet masks, which is more concise than the dotted-decimal notation (255.255.255.0). - Interface Names: Interface names (
eth0,wlan0,enp3s0) can vary depending on the Linux distribution and hardware. Useip link showto find the correct names. - ARP (
nud state): Thenud(Neighbor Unreachability Detection) state inip neighbour showindicates the status of the ARP entry. Common states includeREACHABLE,STALE,DELAY,PROBE,PERMANENT. AFAILEDstate means resolution failed. - Routing Tables: Linux supports multiple routing tables. The default table is
main(table ID 254). Policy routing (ip rule) allows you to select different tables based on packet criteria. - Network Namespaces: Commands executed within a network namespace are isolated to that namespace. This is crucial for containerization and advanced network setups. If you run
ip ainside a namespace, you’ll only see interfaces and IPs within that namespace. - Temporary Changes: Changes made with
ipcommands are generally not persistent across reboots unless managed by network configuration services (like NetworkManager or systemd-networkd).