How to show IP/links/routes → ip addr|link|route
How to Show IP/Links/Routes → ip addr|link|route
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding the ip Command](#understanding-the-ip-command)
4. [Displaying IP Addresses with ip addr](#displaying-ip-addresses-with-ip-addr)
5. [Managing Network Links with ip link](#managing-network-links-with-ip-link)
6. [Viewing Routing Information with ip route](#viewing-routing-information-with-ip-route)
7. [Advanced Usage and Filtering](#advanced-usage-and-filtering)
8. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
9. [Troubleshooting Common Issues](#troubleshooting-common-issues)
10. [Best Practices and Professional Tips](#best-practices-and-professional-tips)
11. [Conclusion](#conclusion)
Introduction
Network administration and troubleshooting require a deep understanding of how to examine network interfaces, IP addresses, and routing configurations on Linux systems. The `ip` command suite has become the modern standard for network configuration and inspection, replacing older tools like `ifconfig`, `route`, and `arp`. This comprehensive guide will teach you how to effectively use `ip addr`, `ip link`, and `ip route` commands to display and analyze network information on your Linux systems.
Whether you're a system administrator diagnosing connectivity issues, a developer setting up network environments, or a Linux enthusiast learning network fundamentals, mastering these commands is essential for effective network management. This article provides detailed explanations, practical examples, and professional insights to help you become proficient with these powerful networking tools.
Prerequisites
Before diving into the `ip` command usage, ensure you have:
System Requirements
- A Linux system with the `iproute2` package installed (available on most modern distributions)
- Basic familiarity with the Linux command line interface
- Understanding of fundamental networking concepts (IP addresses, subnets, routing)
Permission Considerations
- Most `ip` display commands can be run by regular users
- Administrative privileges (root or sudo) are required for configuration changes
- Some advanced features may require elevated permissions
Verification of Tools
Check if the `ip` command is available on your system:
```bash
which ip
ip --version
```
Expected output should show the path to the `ip` binary and version information.
Understanding the ip Command
The `ip` command is part of the `iproute2` package and serves as a comprehensive tool for network administration. It provides a unified interface for managing network interfaces, IP addresses, routing tables, and various network parameters.
Command Structure
The basic syntax follows this pattern:
```bash
ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]
```
Where:
- OPTIONS: Global modifiers affecting output format or behavior
- OBJECT: The network component to work with (addr, link, route, etc.)
- COMMAND: The action to perform (show, add, delete, etc.)
- ARGUMENTS: Specific parameters for the command
Key Objects
- `addr` (or `address`): Manages IP addresses on network interfaces
- `link`: Controls network interface properties and states
- `route`: Handles routing table entries and policies
- `neighbor`: Manages ARP/neighbor table entries
- `rule`: Controls routing policy rules
Displaying IP Addresses with ip addr
The `ip addr` command is your primary tool for examining IP address assignments on network interfaces. It provides comprehensive information about interface configurations, including IPv4 and IPv6 addresses, network masks, and interface states.
Basic Usage
Show All Interfaces
```bash
ip addr show
or simply
ip addr
or even shorter
ip a
```
This command displays all network interfaces with their associated IP addresses and configuration details.
Sample Output Explanation
```bash
1: lo: 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:bb:cc:dd brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86313sec preferred_lft 86313sec
inet6 fe80::a00:27ff:febb:ccdd/64 scope link
valid_lft forever preferred_lft forever
```
Understanding the Output:
- Interface Index: Number identifying the interface (1, 2, etc.)
- Interface Name: Logical name (lo, eth0, wlan0, etc.)
- Flags: Interface state indicators (UP, BROADCAST, MULTICAST, etc.)
- MTU: Maximum Transmission Unit size
- MAC Address: Physical hardware address
- IP Addresses: Both IPv4 and IPv6 with subnet masks
- Scope: Address scope (host, link, global)
- Lifetime: Address validity and preference timers
Specific Interface Information
Show Single Interface
```bash
ip addr show eth0
ip addr show dev eth0
Short form
ip a s eth0
```
Show Only IPv4 Addresses
```bash
ip -4 addr show
ip -4 a s eth0
```
Show Only IPv6 Addresses
```bash
ip -6 addr show
ip -6 a s eth0
```
Advanced Filtering Options
Filter by Address Family
```bash
Show only interfaces with IPv4 addresses
ip -4 addr show scope global
Show only link-local addresses
ip addr show scope link
```
JSON Output Format
```bash
ip -j addr show | jq '.'
```
This provides machine-readable JSON output, useful for scripting and automation.
Managing Network Links with ip link
The `ip link` command focuses on the physical and data-link layer properties of network interfaces. It provides information about interface hardware, operational states, and link-layer configurations.
Basic Link Information
Show All Network Interfaces
```bash
ip link show
Short form
ip link
ip l
```
Sample Output
```bash
1: lo: 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: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:bb:cc:dd brd ff:ff:ff:ff:ff:ff
3: wlan0: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 12:34:56:78:9a:bc brd ff:ff:ff:ff:ff:ff
```
Understanding Link States
Interface Flags
- UP: Interface is administratively up
- LOWER_UP: Physical link is up
- BROADCAST: Interface supports broadcasting
- MULTICAST: Interface supports multicasting
- LOOPBACK: Loopback interface
- POINTOPOINT: Point-to-point connection
Interface States
- UP: Interface is operational
- DOWN: Interface is not operational
- UNKNOWN: State cannot be determined
Specific Link Operations
Show Specific Interface
```bash
ip link show eth0
ip link show dev wlan0
```
Show Interface Statistics
```bash
ip -s link show eth0
ip -s -s link show eth0 # More detailed statistics
```
This displays packet counters, error statistics, and traffic information:
```bash
2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:bb:cc:dd brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1234567 9876 0 0 0 123
TX: bytes packets errors dropped carrier collsns
7654321 8765 0 0 0 0
```
Link Type Information
Show Only Specific Link Types
```bash
Show only Ethernet interfaces
ip link show type ether
Show only wireless interfaces
ip link show type wlan
Show only bridge interfaces
ip link show type bridge
```
Viewing Routing Information with ip route
The `ip route` command is essential for understanding how network traffic flows through your system. It displays and manages the kernel routing table, which determines the path packets take to reach their destinations.
Basic Routing Information
Show All Routes
```bash
ip route show
Short forms
ip route
ip r
```
Sample Output
```bash
default via 192.168.1.1 dev eth0 proto dhcp metric 100
169.254.0.0/16 dev eth0 scope link metric 1000
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100
```
Understanding Route Entries
Each route entry contains several components:
- Destination Network: Target network or host (default for 0.0.0.0/0)
- Gateway: Next hop router (via keyword)
- Interface: Outgoing network interface (dev keyword)
- Protocol: How the route was learned (kernel, dhcp, static)
- Scope: Route scope (global, link, host)
- Source: Preferred source address (src keyword)
- Metric: Route priority (lower values preferred)
Specific Route Queries
Show Default Route
```bash
ip route show default
ip route show 0/0
```
Show Routes for Specific Interface
```bash
ip route show dev eth0
```
Show Routes to Specific Destination
```bash
ip route get 8.8.8.8
ip route get 192.168.1.50
```
This command shows the exact route that would be used to reach a specific destination:
```bash
8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.100 uid 1000
cache
```
IPv6 Routing
Show IPv6 Routes
```bash
ip -6 route show
ip -6 r
```
IPv6 Default Route
```bash
ip -6 route show default
```
Route Table Management
Show Specific Routing Table
```bash
ip route show table main
ip route show table local
ip route show table all
```
Linux maintains multiple routing tables:
- main: Primary routing table
- local: Local and broadcast addresses
- default: Fallback table
Advanced Usage and Filtering
Output Formatting Options
Compact Output
```bash
ip -o addr show # One line per address
ip -o link show # One line per interface
ip -o route show # One line per route
```
Brief Output
```bash
ip -br addr show # Brief format
ip -br link show
```
Brief format example:
```bash
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.1.100/24 fe80::a00:27ff:febb:ccdd/64
wlan0 DOWN
```
Colored Output
```bash
ip -c addr show # Colored output for better readability
ip -c link show
ip -c route show
```
Filtering and Selection
Show Only Running Interfaces
```bash
ip link show up
```
Show Interfaces by State
```bash
ip addr show up # Only UP interfaces
ip addr show down # Only DOWN interfaces
```
Filter by Address Type
```bash
ip addr show permanent # Permanent addresses
ip addr show dynamic # Dynamic addresses
ip addr show temporary # Temporary addresses
```
Combining with Other Tools
Using with grep
```bash
ip addr show | grep inet
ip route show | grep default
ip link show | grep -E "(UP|DOWN)"
```
Using with awk
```bash
ip addr show | awk '/inet / {print $2}' # Extract IP addresses
ip route show | awk '/default/ {print $3}' # Extract default gateway
```
Piping to Other Commands
```bash
ip -o addr show | cut -d' ' -f2,4 # Interface and IP pairs
ip route show | head -5 # First 5 routes
```
Practical Examples and Use Cases
Network Troubleshooting Scenarios
Scenario 1: Diagnosing Connectivity Issues
When users report network connectivity problems, start with these commands:
```bash
Check interface status
ip link show
Verify IP address assignment
ip addr show
Check routing table
ip route show
Test specific route
ip route get 8.8.8.8
```
Scenario 2: Monitoring Network Changes
Track network configuration changes over time:
```bash
Before changes
ip addr show > /tmp/network_before.txt
ip route show >> /tmp/network_before.txt
After changes
ip addr show > /tmp/network_after.txt
ip route show >> /tmp/network_after.txt
Compare
diff /tmp/network_before.txt /tmp/network_after.txt
```
Scenario 3: Documenting Network Configuration
Create comprehensive network documentation:
```bash
#!/bin/bash
echo "=== Network Interface Summary ===" > network_report.txt
ip -br addr show >> network_report.txt
echo -e "\n=== Detailed Interface Information ===" >> network_report.txt
ip addr show >> network_report.txt
echo -e "\n=== Routing Table ===" >> network_report.txt
ip route show >> network_report.txt
echo -e "\n=== Interface Statistics ===" >> network_report.txt
ip -s link show >> network_report.txt
```
Automation and Scripting
Extract Specific Information
```bash
#!/bin/bash
Get primary interface name
PRIMARY_IF=$(ip route show default | awk '{print $5}' | head -1)
Get primary IP address
PRIMARY_IP=$(ip addr show $PRIMARY_IF | awk '/inet / {print $2}' | cut -d'/' -f1 | head -1)
Get default gateway
DEFAULT_GW=$(ip route show default | awk '{print $3}' | head -1)
echo "Primary Interface: $PRIMARY_IF"
echo "Primary IP: $PRIMARY_IP"
echo "Default Gateway: $DEFAULT_GW"
```
Monitor Interface Status
```bash
#!/bin/bash
Monitor interface status changes
while true; do
echo "$(date): Interface Status Check"
ip -br link show | grep DOWN
sleep 60
done
```
Performance Monitoring
Network Statistics Collection
```bash
Collect interface statistics
ip -s -s link show eth0 | grep -E "(RX|TX):"
Monitor packet rates
watch -n 1 'ip -s link show eth0 | grep -E "(RX|TX):"'
```
Traffic Analysis
```bash
Show interface traffic summary
for iface in $(ip -o link show | awk -F': ' '{print $2}' | grep -v lo); do
echo "=== $iface ==="
ip -s link show $iface | grep -A 2 -E "(RX|TX):"
done
```
Troubleshooting Common Issues
Interface Not Showing Up
Problem: Network interface missing from ip link show
Symptoms:
- Interface not visible in `ip link show`
- Device appears in `lspci` or `lsusb` but not in network tools
Solutions:
1. Check if driver is loaded:
```bash
lsmod | grep -i ethernet
dmesg | grep -i network
```
2. Verify hardware detection:
```bash
lspci -v | grep -i ethernet
lsusb | grep -i wireless
```
3. Load appropriate kernel module:
```bash
sudo modprobe
```
IP Address Assignment Issues
Problem: Interface up but no IP address
Symptoms:
- `ip link show` shows interface as UP
- `ip addr show` shows no inet entries
- Network connectivity fails
Diagnostic Steps:
```bash
Check interface status
ip link show eth0
Check for IP addresses
ip addr show eth0
Check DHCP client status
systemctl status dhclient
or
systemctl status NetworkManager
```
Solutions:
1. Request DHCP address:
```bash
sudo dhclient eth0
```
2. Check network manager configuration:
```bash
nmcli device status
nmcli connection show
```
Routing Problems
Problem: Cannot reach specific networks
Symptoms:
- Local network accessible
- Remote networks unreachable
- Default gateway issues
Diagnostic Commands:
```bash
Check routing table
ip route show
Test specific route
ip route get
Verify default gateway
ip route show default
Test gateway connectivity
ping $(ip route show default | awk '{print $3}' | head -1)
```
Common Solutions:
1. Add missing route:
```bash
sudo ip route add / via
```
2. Fix default gateway:
```bash
sudo ip route del default
sudo ip route add default via
```
Performance Issues
Problem: High packet loss or errors
Symptoms:
- Network slowness
- Intermittent connectivity
- High error counters
Investigation:
```bash
Check interface statistics
ip -s -s link show eth0
Look for errors, drops, overruns
ip -s link show eth0 | grep -E "(errors|dropped|overrun)"
Monitor statistics changes
watch -n 1 'ip -s link show eth0'
```
Permission and Access Issues
Problem: Commands fail with permission denied
Symptoms:
- "Operation not permitted" errors
- Cannot modify network configuration
- Limited information display
Solutions:
1. Use sudo for configuration changes:
```bash
sudo ip addr add 192.168.1.50/24 dev eth0
sudo ip link set eth0 up
```
2. Check user permissions:
```bash
groups $USER
# Add user to netdev group if needed
sudo usermod -a -G netdev $USER
```
Best Practices and Professional Tips
Command Efficiency
Use Short Forms
Master the abbreviated versions for faster typing:
- `ip a` instead of `ip addr show`
- `ip l` instead of `ip link show`
- `ip r` instead of `ip route show`
Combine Options
```bash
ip -c -br addr show # Colored brief output
ip -4 -o addr show # IPv4 one-line format
ip -s -s link show # Detailed statistics
```
Documentation and Logging
Create Network Baselines
```bash
Create baseline documentation
mkdir -p /opt/network-docs/$(date +%Y-%m-%d)
ip addr show > /opt/network-docs/$(date +%Y-%m-%d)/addresses.txt
ip route show > /opt/network-docs/$(date +%Y-%m-%d)/routes.txt
ip link show > /opt/network-docs/$(date +%Y-%m-%d)/interfaces.txt
```
Log Network Changes
```bash
Before making changes
echo "$(date): Before network changes" >> /var/log/network-changes.log
ip addr show >> /var/log/network-changes.log
ip route show >> /var/log/network-changes.log
After making changes
echo "$(date): After network changes" >> /var/log/network-changes.log
ip addr show >> /var/log/network-changes.log
ip route show >> /var/log/network-changes.log
```
Monitoring and Alerting
Create Monitoring Scripts
```bash
#!/bin/bash
Network health check script
LOGFILE="/var/log/network-health.log"
check_interface() {
local iface=$1
local status=$(ip link show $iface 2>/dev/null | grep -o "state [A-Z]*" | cut -d' ' -f2)
if [ "$status" != "UP" ]; then
echo "$(date): WARNING - Interface $iface is $status" >> $LOGFILE
return 1
fi
return 0
}
Check critical interfaces
for iface in eth0 wlan0; do
check_interface $iface
done
Check default route
if ! ip route show default >/dev/null 2>&1; then
echo "$(date): ERROR - No default route found" >> $LOGFILE
fi
```
Performance Monitoring
```bash
#!/bin/bash
Network performance monitoring
collect_stats() {
local iface=$1
local timestamp=$(date +%s)
local stats=$(ip -s link show $iface | grep -A 1 "RX:" | tail -1)
echo "$timestamp,$iface,$stats" >> /var/log/network-stats.csv
}
Collect stats for all interfaces
for iface in $(ip -o link show | awk -F': ' '{print $2}' | grep -v lo); do
collect_stats $iface
done
```
Security Considerations
Regular Security Checks
```bash
Check for unexpected interfaces
ip link show | grep -v -E "(lo:|eth0:|wlan0:)"
Monitor for unusual routes
ip route show | grep -v -E "(default|192.168|10\.|172\.(1[6-9]|2[0-9]|3[01])\.)"
Check for promiscuous mode interfaces
ip link show | grep PROMISC
```
Access Control
- Limit access to network configuration commands
- Use sudo rules for specific network operations
- Monitor network configuration changes
- Implement change approval processes for production systems
Integration with Other Tools
Combining with Network Utilities
```bash
Network connectivity test suite
test_connectivity() {
echo "=== Network Interface Status ==="
ip -br addr show
echo -e "\n=== Default Route ==="
ip route show default
echo -e "\n=== DNS Resolution Test ==="
nslookup google.com
echo -e "\n=== Connectivity Test ==="
ping -c 3 $(ip route show default | awk '{print $3}' | head -1)
}
```
JSON Processing
```bash
Extract information using jq
ip -j addr show | jq -r '.[] | select(.ifname=="eth0") | .addr_info[] | select(.family=="inet") | .local'
Create structured reports
ip -j route show | jq '.[] | {destination: .dst, gateway: .gateway, interface: .dev}'
```
Automation Best Practices
Error Handling
```bash
#!/bin/bash
check_interface_exists() {
local iface=$1
if ! ip link show "$iface" >/dev/null 2>&1; then
echo "Error: Interface $iface does not exist" >&2
return 1
fi
return 0
}
Usage
if check_interface_exists eth0; then
ip addr show eth0
else
exit 1
fi
```
Configuration Validation
```bash
validate_ip() {
local ip=$1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
return 0
else
echo "Invalid IP address: $ip" >&2
return 1
fi
}
```
Conclusion
The `ip addr`, `ip link`, and `ip route` commands form the foundation of modern Linux network administration. These powerful tools provide comprehensive visibility into network interface configurations, link states, and routing behavior. By mastering these commands, you gain the ability to effectively diagnose network issues, monitor system performance, and maintain robust network configurations.
Key takeaways from this comprehensive guide include:
1. Command Mastery: Understanding the syntax, options, and output formats of each command enables efficient network troubleshooting and administration.
2. Practical Application: Real-world scenarios demonstrate how these tools solve common networking challenges, from basic connectivity issues to complex routing problems.
3. Automation Potential: Scripting capabilities allow for automated monitoring, documentation, and maintenance of network configurations.
4. Professional Practices: Following best practices ensures reliable, secure, and maintainable network management procedures.
5. Integration Benefits: Combining these commands with other Linux utilities creates powerful network analysis and monitoring solutions.
As network environments continue to evolve with virtualization, containerization, and cloud technologies, the fundamental skills covered in this guide remain essential. Whether you're managing traditional servers, virtual machines, or container networks, the `ip` command suite provides the visibility and control needed for effective network administration.
Continue practicing these commands in different environments, explore advanced features like network namespaces and policy routing, and stay updated with the latest developments in Linux networking tools. The investment in mastering these fundamental commands will serve you well throughout your career in system administration, network engineering, or DevOps.
Remember that network troubleshooting is often an iterative process requiring patience, systematic thinking, and thorough documentation. Use these tools as part of a comprehensive approach to network management that includes proper monitoring, change control, and disaster recovery planning.