How to manage network (modern) → ip
How to Manage Network (Modern) → IP
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding the IP Command](#understanding-the-ip-command)
4. [Basic IP Command Syntax](#basic-ip-command-syntax)
5. [Managing Network Interfaces](#managing-network-interfaces)
6. [IP Address Configuration](#ip-address-configuration)
7. [Routing Management](#routing-management)
8. [Network Namespace Operations](#network-namespace-operations)
9. [Advanced IP Command Features](#advanced-ip-command-features)
10. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
11. [Troubleshooting Common Issues](#troubleshooting-common-issues)
12. [Best Practices and Professional Tips](#best-practices-and-professional-tips)
13. [Conclusion](#conclusion)
Introduction
Modern network management has evolved significantly from the traditional `ifconfig` and `route` commands to the more powerful and comprehensive `ip` command. The `ip` command, part of the iproute2 package, provides a unified interface for managing network configurations in Linux systems. This comprehensive guide will walk you through everything you need to know about managing network configurations using the modern `ip` command.
Whether you're a system administrator, network engineer, or developer working with Linux systems, understanding the `ip` command is essential for effective network management. This article covers everything from basic IP address assignment to advanced routing configurations and network namespace management.
The `ip` command offers superior functionality compared to legacy tools, providing better performance, more features, and cleaner output formatting. It's the recommended approach for modern Linux network administration and is actively maintained and developed.
Prerequisites
Before diving into the `ip` command, ensure you have the following:
System Requirements
- Linux operating system (kernel 2.2 or later)
- iproute2 package installed (usually pre-installed on modern distributions)
- Root or sudo privileges for most network configuration tasks
- Basic understanding of networking concepts (IP addresses, subnets, routing)
Required Packages
Most modern Linux distributions include the iproute2 package by default. If not installed, use your distribution's package manager:
Ubuntu/Debian:
```bash
sudo apt-get update
sudo apt-get install iproute2
```
CentOS/RHEL/Fedora:
```bash
sudo yum install iproute2
or for newer versions
sudo dnf install iproute2
```
Verification
Verify the installation by checking the version:
```bash
ip -V
```
Understanding the IP Command
The `ip` command is a powerful networking tool that replaces several legacy commands:
| Legacy Command | IP Command Equivalent | Purpose |
|----------------|----------------------|---------|
| `ifconfig` | `ip addr`, `ip link` | Interface configuration |
| `route` | `ip route` | Routing table management |
| `arp` | `ip neigh` | Neighbor (ARP) table management |
| `netstat -i` | `ip -s link` | Interface statistics |
| `netstat -r` | `ip route` | Routing table display |
Key Advantages of the IP Command
1. Unified Interface: Single command for multiple network operations
2. Better Performance: More efficient than legacy tools
3. Rich Output Options: Multiple output formats including JSON
4. Advanced Features: Support for network namespaces, VLANs, and more
5. Active Development: Continuously updated with new features
Basic IP Command Syntax
The basic syntax of the `ip` command follows this pattern:
```bash
ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]
```
Common Options
- `-4`: Force IPv4 only
- `-6`: Force IPv6 only
- `-s`: Show statistics
- `-d`: Show detailed information
- `-j`: Output in JSON format
- `-p`: Pretty print (human-readable JSON)
- `-c`: Use colors in output
Main Objects
- `link`: Network interfaces
- `addr`: IP addresses
- `route`: Routing table entries
- `neigh`: Neighbor (ARP) entries
- `netns`: Network namespaces
Getting Help
Access help information for any object:
```bash
ip help
ip addr help
ip route help
```
Managing Network Interfaces
Network interface management is one of the most common uses of the `ip` command. This section covers how to view, configure, and manage network interfaces.
Viewing Network Interfaces
Display all network interfaces:
```bash
ip link show
```
Show specific interface:
```bash
ip link show eth0
```
Display interface statistics:
```bash
ip -s link show eth0
```
Show only active interfaces:
```bash
ip link show up
```
Interface State Management
Bring an interface up:
```bash
sudo ip link set eth0 up
```
Bring an interface down:
```bash
sudo ip link set eth0 down
```
Changing Interface Properties
Change MAC address:
```bash
sudo ip link set eth0 down
sudo ip link set eth0 address 00:11:22:33:44:55
sudo ip link set eth0 up
```
Change interface name:
```bash
sudo ip link set eth0 down
sudo ip link set eth0 name newname
sudo ip link set newname up
```
Set MTU (Maximum Transmission Unit):
```bash
sudo ip link set eth0 mtu 1400
```
Creating Virtual Interfaces
Create a VLAN interface:
```bash
sudo ip link add link eth0 name eth0.100 type vlan id 100
```
Create a bridge interface:
```bash
sudo ip link add name br0 type bridge
```
Create a bonding interface:
```bash
sudo ip link add bond0 type bond mode 802.3ad
```
IP Address Configuration
Managing IP addresses is a fundamental aspect of network configuration. The `ip addr` command provides comprehensive address management capabilities.
Viewing IP Addresses
Show all IP addresses:
```bash
ip addr show
```
Show addresses for specific interface:
```bash
ip addr show eth0
```
Show only IPv4 addresses:
```bash
ip -4 addr show
```
Show only IPv6 addresses:
```bash
ip -6 addr show
```
Adding IP Addresses
Add IPv4 address:
```bash
sudo ip addr add 192.168.1.100/24 dev eth0
```
Add IPv6 address:
```bash
sudo ip addr add 2001:db8::1/64 dev eth0
```
Add address with broadcast:
```bash
sudo ip addr add 192.168.1.100/24 broadcast 192.168.1.255 dev eth0
```
Add secondary IP address:
```bash
sudo ip addr add 192.168.1.101/24 dev eth0 label eth0:1
```
Removing IP Addresses
Remove specific address:
```bash
sudo ip addr del 192.168.1.100/24 dev eth0
```
Remove all addresses from interface:
```bash
sudo ip addr flush dev eth0
```
Address Scope and Types
Addresses can have different scopes:
- `global`: Valid everywhere
- `site`: Valid only within a site
- `link`: Valid only on this link
- `host`: Valid only inside this host
Example with scope:
```bash
sudo ip addr add 192.168.1.100/24 scope global dev eth0
```
Routing Management
Routing is crucial for network communication. The `ip route` command provides comprehensive routing table management.
Viewing Routing Tables
Show main routing table:
```bash
ip route show
```
Show specific route:
```bash
ip route show 192.168.1.0/24
```
Show routing cache:
```bash
ip route show cache
```
Show all routing tables:
```bash
ip route show table all
```
Adding Routes
Add default gateway:
```bash
sudo ip route add default via 192.168.1.1
```
Add network route:
```bash
sudo ip route add 10.0.0.0/8 via 192.168.1.1
```
Add route through specific interface:
```bash
sudo ip route add 172.16.0.0/16 dev eth1
```
Add route with metric:
```bash
sudo ip route add 10.0.0.0/8 via 192.168.1.1 metric 100
```
Removing Routes
Remove specific route:
```bash
sudo ip route del 10.0.0.0/8 via 192.168.1.1
```
Remove default route:
```bash
sudo ip route del default
```
Route Replacement and Changes
Replace existing route:
```bash
sudo ip route replace 10.0.0.0/8 via 192.168.1.2
```
Change route parameters:
```bash
sudo ip route change 10.0.0.0/8 via 192.168.1.1 metric 200
```
Multiple Routing Tables
Linux supports multiple routing tables. Common tables include:
- `local` (table 255): Local addresses
- `main` (table 254): Normal routes
- `default` (table 253): Default routes
View specific table:
```bash
ip route show table local
```
Add route to specific table:
```bash
sudo ip route add 10.0.0.0/8 via 192.168.1.1 table 100
```
Network Namespace Operations
Network namespaces provide network isolation, allowing multiple network stacks to coexist on a single system.
Managing Network Namespaces
List network namespaces:
```bash
ip netns list
```
Create network namespace:
```bash
sudo ip netns add myns
```
Delete network namespace:
```bash
sudo ip netns del myns
```
Executing Commands in Namespaces
Run command in namespace:
```bash
sudo ip netns exec myns ip addr show
```
Start shell in namespace:
```bash
sudo ip netns exec myns bash
```
Moving Interfaces Between Namespaces
Move interface to namespace:
```bash
sudo ip link set eth1 netns myns
```
Move interface back to default namespace:
```bash
sudo ip netns exec myns ip link set eth1 netns 1
```
Connecting Namespaces
Create virtual ethernet pair:
```bash
sudo ip link add veth0 type veth peer name veth1
```
Move one end to namespace:
```bash
sudo ip link set veth1 netns myns
```
Configure interfaces:
```bash
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip link set veth0 up
sudo ip netns exec myns ip addr add 10.0.0.2/24 dev veth1
sudo ip netns exec myns ip link set veth1 up
```
Advanced IP Command Features
Neighbor (ARP) Management
View ARP table:
```bash
ip neigh show
```
Add static ARP entry:
```bash
sudo ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0
```
Delete ARP entry:
```bash
sudo ip neigh del 192.168.1.100 dev eth0
```
Flush ARP cache:
```bash
sudo ip neigh flush all
```
Traffic Control Integration
The `ip` command integrates with Linux traffic control:
Show queueing disciplines:
```bash
ip link show
tc qdisc show dev eth0
```
Monitoring and Statistics
Monitor route changes:
```bash
ip monitor route
```
Monitor address changes:
```bash
ip monitor addr
```
Monitor all changes:
```bash
ip monitor all
```
Show detailed statistics:
```bash
ip -s -s link show eth0
```
JSON Output for Automation
Get interface information in JSON:
```bash
ip -j addr show
```
Pretty-print JSON output:
```bash
ip -j -p addr show
```
This is particularly useful for scripting and automation:
```bash
#!/bin/bash
interfaces=$(ip -j link show | jq -r '.[].ifname')
for iface in $interfaces; do
echo "Interface: $iface"
done
```
Practical Examples and Use Cases
Example 1: Setting Up a Static IP Configuration
Complete static IP setup:
```bash
Bring interface down
sudo ip link set eth0 down
Add IP address
sudo ip addr add 192.168.1.100/24 dev eth0
Bring interface up
sudo ip link set eth0 up
Add default route
sudo ip route add default via 192.168.1.1
Verify configuration
ip addr show eth0
ip route show
```
Example 2: Creating a Bridge Network
Set up a bridge for virtual machines:
```bash
Create bridge
sudo ip link add name br0 type bridge
Bring bridge up
sudo ip link set br0 up
Add physical interface to bridge
sudo ip link set eth0 master br0
Configure bridge IP
sudo ip addr add 192.168.1.1/24 dev br0
Verify bridge
ip link show type bridge
```
Example 3: VLAN Configuration
Configure VLAN interfaces:
```bash
Create VLAN interface
sudo ip link add link eth0 name eth0.100 type vlan id 100
Configure VLAN IP
sudo ip addr add 192.168.100.1/24 dev eth0.100
Bring VLAN interface up
sudo ip link set eth0.100 up
Verify VLAN
ip link show type vlan
```
Example 4: Network Namespace for Testing
Create isolated network environment:
```bash
Create namespace
sudo ip netns add testns
Create veth pair
sudo ip link add veth0 type veth peer name veth1
Move one end to namespace
sudo ip link set veth1 netns testns
Configure host side
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip link set veth0 up
Configure namespace side
sudo ip netns exec testns ip addr add 10.0.0.2/24 dev veth1
sudo ip netns exec testns ip link set veth1 up
sudo ip netns exec testns ip link set lo up
Test connectivity
ping 10.0.0.2
sudo ip netns exec testns ping 10.0.0.1
```
Example 5: Multiple IP Addresses on Single Interface
Configure multiple IPs:
```bash
Primary IP
sudo ip addr add 192.168.1.100/24 dev eth0
Secondary IPs
sudo ip addr add 192.168.1.101/24 dev eth0 label eth0:1
sudo ip addr add 192.168.1.102/24 dev eth0 label eth0:2
Verify all addresses
ip addr show eth0
```
Troubleshooting Common Issues
Issue 1: Interface Won't Come Up
Symptoms: Interface remains down despite commands
Diagnosis:
```bash
ip link show eth0
dmesg | grep eth0
```
Solutions:
1. Check cable connections
2. Verify driver loading: `lsmod | grep ethernet_driver`
3. Check for hardware issues: `ethtool eth0`
4. Reload network driver:
```bash
sudo modprobe -r driver_name
sudo modprobe driver_name
```
Issue 2: IP Address Assignment Fails
Symptoms: Cannot assign IP address
Diagnosis:
```bash
ip addr show
ip link show
```
Solutions:
1. Ensure interface is up: `sudo ip link set eth0 up`
2. Check for conflicting addresses: `ip addr show`
3. Verify subnet mask: Use correct CIDR notation
4. Check permissions: Ensure sudo/root access
Issue 3: Routing Problems
Symptoms: Cannot reach remote networks
Diagnosis:
```bash
ip route show
ip route get 8.8.8.8
ping -c 1 gateway_ip
```
Solutions:
1. Add default route: `sudo ip route add default via gateway_ip`
2. Check route metrics: Lower metrics have higher priority
3. Verify gateway accessibility
4. Check routing table conflicts
Issue 4: DNS Resolution Issues
Symptoms: IP connectivity works but DNS doesn't
Diagnosis:
```bash
ping 8.8.8.8 # Should work
ping google.com # Might fail
cat /etc/resolv.conf
```
Solutions:
1. Configure DNS servers in `/etc/resolv.conf`:
```
nameserver 8.8.8.8
nameserver 8.8.4.4
```
2. Use systemd-resolved if available
3. Check firewall rules for DNS ports
Issue 5: VLAN Not Working
Symptoms: VLAN interface created but no connectivity
Diagnosis:
```bash
ip link show type vlan
tcpdump -i eth0 vlan 100
```
Solutions:
1. Verify switch VLAN configuration
2. Check if physical interface supports VLANs
3. Ensure VLAN module loaded: `modprobe 8021q`
4. Verify VLAN ID matches switch configuration
Issue 6: Bridge Not Forwarding Traffic
Symptoms: Bridge created but VMs can't communicate
Diagnosis:
```bash
ip link show type bridge
brctl show # If bridge-utils installed
```
Solutions:
1. Enable bridge forwarding:
```bash
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
```
2. Check bridge STP settings
3. Verify interfaces are added to bridge
4. Check firewall rules
Best Practices and Professional Tips
Security Best Practices
1. Use Network Namespaces for Isolation
```bash
# Isolate services in separate namespaces
sudo ip netns add webapp
sudo ip netns exec webapp service start
```
2. Implement Proper Access Controls
- Limit sudo access to network commands
- Use specific sudoers entries for network operations
- Implement role-based access control
3. Regular Security Audits
```bash
# Check for unusual routes
ip route show table all
# Monitor ARP table for anomalies
ip neigh show
```
Performance Optimization
1. Interface Optimization
```bash
# Optimize MTU for your network
sudo ip link set eth0 mtu 9000 # For jumbo frames
# Enable hardware features
ethtool -K eth0 gso on
ethtool -K eth0 tso on
```
2. Routing Optimization
```bash
# Use appropriate metrics
sudo ip route add 10.0.0.0/8 via 192.168.1.1 metric 100
# Implement policy routing for multiple uplinks
ip rule add from 192.168.1.0/24 table 100
```
Monitoring and Maintenance
1. Regular Monitoring Scripts
```bash
#!/bin/bash
# Network health check script
# Check interface status
for iface in $(ip -o link show | cut -d: -f2 | tr -d ' '); do
status=$(ip link show $iface | grep -o 'state [A-Z]*' | cut -d' ' -f2)
echo "Interface $iface: $status"
done
# Check routing table
echo "Default routes:"
ip route show default
```
2. Automated Configuration Backup
```bash
#!/bin/bash
# Backup network configuration
ip addr show > /backup/ip-addr-$(date +%Y%m%d).txt
ip route show > /backup/ip-route-$(date +%Y%m%d).txt
ip link show > /backup/ip-link-$(date +%Y%m%d).txt
```
Documentation and Change Management
1. Document All Changes
- Maintain network topology diagrams
- Document IP address assignments
- Keep change logs for routing modifications
2. Use Configuration Management
```bash
# Example Ansible task
- name: Configure network interface
shell: |
ip addr add {{ ip_address }}/{{ netmask }} dev {{ interface }}
ip link set {{ interface }} up
```
Testing and Validation
1. Always Test Changes
```bash
# Test connectivity before and after changes
ping -c 1 gateway_ip
# Use temporary configurations when possible
ip addr add 192.168.1.100/24 dev eth0
# Test...
ip addr del 192.168.1.100/24 dev eth0
```
2. Implement Rollback Procedures
```bash
#!/bin/bash
# Rollback script
# Save current config
ip addr show > /tmp/current-config
# Apply changes with timeout
timeout 30 bash -c 'your_network_changes_here'
# If timeout occurs, rollback
if [ $? -eq 124 ]; then
echo "Rolling back changes..."
# Restore previous configuration
fi
```
Advanced Scripting Techniques
1. JSON Processing for Automation
```bash
#!/bin/bash
# Get interface information in structured format
interfaces=$(ip -j link show | jq -r '.[].ifname')
for iface in $interfaces; do
ip_info=$(ip -j addr show $iface | jq -r '.[0].addr_info[]?.local // empty')
if [ ! -z "$ip_info" ]; then
echo "$iface: $ip_info"
fi
done
```
2. Integration with System Services
```bash
# systemd service for network configuration
[Unit]
Description=Custom Network Configuration
After=network-pre.target
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/network-setup.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
```
Conclusion
The `ip` command represents the modern standard for Linux network management, offering comprehensive functionality that surpasses traditional networking tools. Throughout this guide, we've explored the extensive capabilities of the `ip` command, from basic interface management to advanced features like network namespaces and complex routing scenarios.
Key takeaways from this comprehensive guide include:
Modern Network Management: The `ip` command provides a unified, powerful interface for all network configuration tasks, replacing multiple legacy tools with a single, consistent command structure.
Comprehensive Functionality: From simple IP address assignment to complex routing policies and network isolation through namespaces, the `ip` command handles virtually all network configuration requirements.
Professional Implementation: Understanding proper syntax, troubleshooting techniques, and best practices ensures reliable network configurations in production environments.
Automation and Scripting: The JSON output capabilities and consistent command structure make the `ip` command ideal for automation scripts and configuration management systems.
Future-Proof Skills: As Linux networking continues to evolve, the `ip` command remains actively developed and maintained, ensuring your skills remain relevant.
Moving forward, continue to practice these concepts in lab environments before implementing them in production. Stay updated with the latest features and improvements in the iproute2 package, and always maintain proper documentation and change management procedures for network modifications.
The networking landscape continues to evolve with technologies like containers, software-defined networking, and cloud infrastructure. The `ip` command provides the foundational knowledge needed to adapt to these emerging technologies while maintaining solid fundamentals in Linux network management.
Remember that effective network management combines technical knowledge with proper procedures, security awareness, and continuous learning. Use this guide as a reference for your ongoing network administration tasks, and don't hesitate to explore the extensive help documentation available through the `ip help` command system.