How to add route/addr → ip route add ...; ip addr add ..
How to Add Routes and Addresses Using ip route add and ip addr add Commands
Network configuration is a fundamental skill for system administrators, network engineers, and DevOps professionals working with Linux systems. The `ip` command suite provides powerful tools for managing network interfaces, routes, and addressing. This comprehensive guide will walk you through the essential `ip route add` and `ip addr add` commands, enabling you to effectively configure network routing and addressing on Linux systems.
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding the ip Command](#understanding-the-ip-command)
4. [IP Address Management with ip addr add](#ip-address-management)
5. [Route Management with ip route add](#route-management)
6. [Practical Examples and Use Cases](#practical-examples)
7. [Advanced Configuration Scenarios](#advanced-configuration)
8. [Troubleshooting Common Issues](#troubleshooting)
9. [Best Practices and Professional Tips](#best-practices)
10. [Conclusion](#conclusion)
Introduction
The `ip` command is part of the iproute2 package and serves as the modern replacement for older networking tools like `ifconfig`, `route`, and `arp`. Two of its most frequently used subcommands are `ip addr add` for managing IP addresses on network interfaces and `ip route add` for configuring routing tables. These commands are essential for network configuration, virtual machine setup, container networking, and complex network topologies.
Understanding these commands enables you to:
- Configure static IP addresses on network interfaces
- Set up custom routing rules for traffic management
- Create complex network topologies
- Troubleshoot network connectivity issues
- Implement advanced networking scenarios in virtualized environments
Prerequisites
Before proceeding with this guide, ensure you have:
- Root or sudo privileges on a Linux system
- Basic understanding of networking concepts including IP addressing, subnetting, and routing
- Familiarity with the Linux command line and terminal operations
- Access to a test environment where network changes won't disrupt production services
- The iproute2 package installed (available by default on most modern Linux distributions)
Checking Your System
Verify that the `ip` command is available on your system:
```bash
ip --version
```
List current network interfaces:
```bash
ip link show
```
Understanding the ip Command
The `ip` command uses a consistent syntax structure across its various subcommands:
```bash
ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]
```
Where:
- OPTIONS: Global options like `-4` (IPv4), `-6` (IPv6), or `-c` (colored output)
- OBJECT: The network object to manipulate (addr, route, link, etc.)
- COMMAND: The action to perform (add, delete, show, etc.)
- ARGUMENTS: Specific parameters for the command
Key Objects for This Guide
- addr: Manages IP addresses on network interfaces
- route: Manages the kernel routing table entries
IP Address Management with ip addr add
The `ip addr add` command assigns IP addresses to network interfaces. This is crucial for establishing network connectivity and enabling communication between systems.
Basic Syntax
```bash
ip addr add ADDRESS/PREFIX_LENGTH dev INTERFACE_NAME
```
Essential Parameters
- ADDRESS: The IP address to assign
- PREFIX_LENGTH: The subnet mask in CIDR notation
- dev INTERFACE_NAME: The target network interface
- scope: Address scope (global, link, host)
- label: Interface alias label
Adding IPv4 Addresses
Basic IPv4 Address Assignment
```bash
Add IP address 192.168.1.100 with /24 subnet to eth0
sudo ip addr add 192.168.1.100/24 dev eth0
```
Adding Multiple Addresses to One Interface
```bash
Primary address
sudo ip addr add 192.168.1.100/24 dev eth0
Secondary address
sudo ip addr add 192.168.1.101/24 dev eth0
Address with different subnet
sudo ip addr add 10.0.0.50/16 dev eth0
```
Using Interface Labels
```bash
Add address with label for easier identification
sudo ip addr add 192.168.1.100/24 dev eth0 label eth0:primary
Add secondary address with label
sudo ip addr add 192.168.1.101/24 dev eth0 label eth0:secondary
```
Adding IPv6 Addresses
```bash
Add IPv6 address
sudo ip addr add 2001:db8::1/64 dev eth0
Add link-local IPv6 address
sudo ip addr add fe80::1/64 dev eth0 scope link
```
Verification Commands
```bash
Show all addresses on all interfaces
ip addr show
Show addresses for specific interface
ip addr show dev eth0
Show only IPv4 addresses
ip -4 addr show
Show only IPv6 addresses
ip -6 addr show
```
Route Management with ip route add
The `ip route add` command creates entries in the kernel routing table, determining how network traffic is forwarded to its destination.
Basic Syntax
```bash
ip route add DESTINATION via GATEWAY [dev INTERFACE] [OPTIONS]
```
Essential Parameters
- DESTINATION: Target network or host (can be default for default route)
- via GATEWAY: Next-hop router IP address
- dev INTERFACE: Outgoing network interface
- metric: Route priority (lower values have higher priority)
- scope: Route scope (global, link, host)
Adding Basic Routes
Default Route Configuration
```bash
Add default route via gateway
sudo ip route add default via 192.168.1.1
Add default route with specific interface
sudo ip route add default via 192.168.1.1 dev eth0
```
Network-Specific Routes
```bash
Route to specific network
sudo ip route add 10.0.0.0/8 via 192.168.1.1
Route to host with specific interface
sudo ip route add 203.0.113.1 via 192.168.1.1 dev eth0
Direct route (no gateway needed)
sudo ip route add 172.16.0.0/16 dev eth1
```
Routes with Metrics
```bash
Primary route with low metric (higher priority)
sudo ip route add 10.0.0.0/8 via 192.168.1.1 metric 100
Backup route with high metric (lower priority)
sudo ip route add 10.0.0.0/8 via 192.168.1.2 metric 200
```
Advanced Routing Options
Multiple Path Routes (Load Balancing)
```bash
Equal-cost multi-path routing
sudo ip route add 10.0.0.0/8 \
nexthop via 192.168.1.1 weight 1 \
nexthop via 192.168.1.2 weight 1
```
Source-Specific Routes
```bash
Route based on source address
sudo ip route add 10.0.0.0/8 via 192.168.1.1 src 192.168.1.100
```
Verification Commands
```bash
Show all routes
ip route show
Show routes for specific destination
ip route get 8.8.8.8
Show only IPv4 routes
ip -4 route show
Show routes with detailed information
ip route show table all
```
Practical Examples and Use Cases
Example 1: Basic Server Network Configuration
Setting up a web server with static IP configuration:
```bash
Assign static IP address
sudo ip addr add 192.168.1.100/24 dev eth0
Add default gateway
sudo ip route add default via 192.168.1.1
Verify configuration
ip addr show dev eth0
ip route show
```
Example 2: Multi-Homed Server Setup
Configuring a server with multiple network interfaces:
```bash
Configure first interface (internal network)
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip route add 192.168.0.0/16 dev eth0
Configure second interface (DMZ network)
sudo ip addr add 10.0.1.100/24 dev eth1
sudo ip route add 10.0.0.0/8 via 10.0.1.1 dev eth1
Set default route through internal network
sudo ip route add default via 192.168.1.1 dev eth0
```
Example 3: Virtual Machine Networking
Setting up networking for a virtual machine or container:
```bash
Create virtual interface (if needed)
sudo ip link add veth0 type veth peer name veth1
Configure host side
sudo ip addr add 172.17.0.1/24 dev veth0
sudo ip link set veth0 up
Configure container side
sudo ip addr add 172.17.0.2/24 dev veth1
sudo ip link set veth1 up
Add route in container namespace
sudo ip route add default via 172.17.0.1 dev veth1
```
Example 4: Site-to-Site VPN Configuration
Routing traffic through VPN tunnels:
```bash
Add address to VPN interface
sudo ip addr add 10.10.10.1/30 dev tun0
Route remote network through VPN
sudo ip route add 10.20.0.0/16 dev tun0
Route specific hosts through VPN
sudo ip route add 203.0.113.0/24 via 10.10.10.2 dev tun0
```
Advanced Configuration Scenarios
Policy-Based Routing
Using routing tables and rules for advanced traffic management:
```bash
Create custom routing table
echo "100 custom" >> /etc/iproute2/rt_tables
Add route to custom table
sudo ip route add default via 192.168.2.1 table custom
Create rule to use custom table
sudo ip rule add from 192.168.1.0/24 table custom
```
VLAN Configuration
Setting up VLAN interfaces with proper addressing and routing:
```bash
Create VLAN interface
sudo ip link add link eth0 name eth0.100 type vlan id 100
Configure VLAN interface
sudo ip addr add 192.168.100.1/24 dev eth0.100
sudo ip link set eth0.100 up
Add VLAN-specific routes
sudo ip route add 192.168.100.0/24 dev eth0.100
```
Bridge Configuration
Setting up bridge interfaces for virtualization:
```bash
Create bridge
sudo ip link add br0 type bridge
Add interface to bridge
sudo ip link set eth0 master br0
Configure bridge
sudo ip addr add 192.168.1.1/24 dev br0
sudo ip link set br0 up
Add routes for bridge network
sudo ip route add 192.168.1.0/24 dev br0
```
Troubleshooting Common Issues
Issue 1: Address Already in Use
Problem: Error when adding an IP address that already exists.
Solution:
```bash
Check existing addresses
ip addr show dev eth0
Remove existing address if necessary
sudo ip addr del 192.168.1.100/24 dev eth0
Add the address
sudo ip addr add 192.168.1.100/24 dev eth0
```
Issue 2: Route Conflicts
Problem: Conflicting routes causing connectivity issues.
Diagnosis:
```bash
Check current routes
ip route show
Test specific route
ip route get 10.0.0.1
Check route conflicts
ip route show | grep 10.0.0.0
```
Solution:
```bash
Remove conflicting route
sudo ip route del 10.0.0.0/8 via 192.168.1.2
Add correct route
sudo ip route add 10.0.0.0/8 via 192.168.1.1
```
Issue 3: Interface Not Ready
Problem: Cannot add address to interface that's down.
Solution:
```bash
Check interface status
ip link show dev eth0
Bring interface up
sudo ip link set eth0 up
Add address
sudo ip addr add 192.168.1.100/24 dev eth0
```
Issue 4: Subnet Mask Problems
Problem: Incorrect subnet mask causing connectivity issues.
Diagnosis:
```bash
Check current configuration
ip addr show dev eth0
Verify network reachability
ping -c 3 192.168.1.1
```
Solution:
```bash
Remove incorrect address
sudo ip addr del 192.168.1.100/16 dev eth0
Add with correct subnet mask
sudo ip addr add 192.168.1.100/24 dev eth0
```
Issue 5: Persistent Configuration
Problem: Configuration lost after reboot.
Solution for systemd-networkd:
Create `/etc/systemd/network/eth0.network`:
```ini
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
```
Solution for NetworkManager:
```bash
Create persistent connection
nmcli connection add type ethernet con-name eth0-static ifname eth0 \
ip4 192.168.1.100/24 gw4 192.168.1.1
```
Diagnostic Commands
```bash
Check interface statistics
ip -s link show
Monitor route changes
ip monitor route
Check ARP table
ip neigh show
Verify connectivity
ping -c 3 gateway_ip
traceroute destination_ip
```
Best Practices and Professional Tips
1. Planning and Documentation
- Document all network changes with clear comments and configuration files
- Plan IP address allocation to avoid conflicts and ensure scalability
- Use consistent naming conventions for interface labels and routing table names
- Test configurations in non-production environments first
2. Security Considerations
```bash
Use specific source addresses for security
sudo ip route add 10.0.0.0/8 via 192.168.1.1 src 192.168.1.100
Implement route filtering where appropriate
sudo ip route add blackhole 192.0.2.0/24
```
3. Performance Optimization
```bash
Use appropriate metrics for route prioritization
sudo ip route add default via 192.168.1.1 metric 100
sudo ip route add default via 192.168.1.2 metric 200
Configure multi-path routing for load balancing
sudo ip route add 10.0.0.0/8 \
nexthop via 192.168.1.1 weight 2 \
nexthop via 192.168.1.2 weight 1
```
4. Monitoring and Maintenance
```bash
Regular configuration backups
ip addr show > /backup/network-addresses-$(date +%Y%m%d).txt
ip route show > /backup/network-routes-$(date +%Y%m%d).txt
Monitor for changes
ip monitor all
```
5. Automation and Scripting
Create reusable scripts for common configurations:
```bash
#!/bin/bash
network-setup.sh
INTERFACE="eth0"
IP_ADDRESS="192.168.1.100/24"
GATEWAY="192.168.1.1"
Configure interface
ip addr add $IP_ADDRESS dev $INTERFACE
ip link set $INTERFACE up
ip route add default via $GATEWAY
echo "Network configuration completed for $INTERFACE"
```
6. Validation and Testing
Always validate configurations:
```bash
Comprehensive network test function
test_network() {
local gateway=$1
local dns_server=$2
echo "Testing network connectivity..."
# Test gateway
if ping -c 3 $gateway > /dev/null 2>&1; then
echo "✓ Gateway reachable"
else
echo "✗ Gateway unreachable"
return 1
fi
# Test DNS
if ping -c 3 $dns_server > /dev/null 2>&1; then
echo "✓ DNS server reachable"
else
echo "✗ DNS server unreachable"
return 1
fi
echo "Network test completed successfully"
}
Usage
test_network 192.168.1.1 8.8.8.8
```
7. Common Pitfalls to Avoid
- Don't forget to bring interfaces up after configuration
- Avoid overlapping subnets that can cause routing confusion
- Be careful with default routes - multiple default routes can cause issues
- Test before making permanent - always verify temporary changes work
- Consider the order of operations - some configurations depend on others
Conclusion
Mastering the `ip addr add` and `ip route add` commands is essential for effective Linux network administration. These powerful tools provide fine-grained control over network configuration, enabling everything from basic server setup to complex multi-homed routing scenarios.
Key takeaways from this guide:
1. ip addr add manages IP address assignment to network interfaces with support for multiple addresses, labels, and scopes
2. ip route add controls traffic routing through custom routes, gateways, and metrics
3. Proper planning and testing are crucial for successful network configuration
4. Troubleshooting skills help quickly resolve common networking issues
5. Best practices ensure reliable, secure, and maintainable network configurations
Next Steps
To further develop your networking skills:
- Explore advanced routing policies and traffic shaping
- Learn about network namespaces and container networking
- Study routing protocols and dynamic routing configuration
- Practice with complex multi-network scenarios
- Investigate network monitoring and performance optimization tools
Remember that network configuration changes can have significant impacts on system connectivity. Always test thoroughly in controlled environments and maintain proper backups of working configurations. With practice and careful application of these concepts, you'll be able to handle complex networking challenges with confidence.
The `ip` command suite offers many more capabilities beyond address and route management. Continue exploring its features to build comprehensive networking expertise that will serve you well in modern infrastructure management and DevOps practices.