🌐 Configuring Network Routing: Simple Guide
Ready to set up network routing on Alpine Linux? This is exciting! 🎉 We’ll learn how to direct network traffic. Your computer will know where to send data! 😊
🤔 What is Network Routing?
Network routing tells your computer how to send data to other computers. Think of it like GPS for internet traffic!
Routing helps with:
- 🚦 Directing traffic to the right place
- 🔀 Choosing the best path for data
- 🌍 Connecting different networks together
🎯 What You Need
Before we start, you need:
- ✅ Alpine Linux system with network access
- ✅ Root access for network configuration
- ✅ Basic understanding of IP addresses
- ✅ Knowledge of your network setup
📋 Step 1: Understanding Current Routes
Viewing the Routing Table
Let’s see what routes your system already knows! This is the foundation! 😊
What we’re doing: Checking the current routing configuration.
# Show current routing table
route -n
# Alternative: using ip command
ip route show
# Show detailed route information
ip route show table all
# Check default gateway
ip route | grep default
What this does: 📖 Shows all the paths your computer knows for sending data.
Example output:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
What this means: Your computer knows how to reach the internet through 192.168.1.1! ✅
💡 Important Tips
Tip: Always backup your current routes before making changes! 💡
Warning: Wrong routes can break internet connectivity! ⚠️
🛠️ Step 2: Basic Route Configuration
Adding Static Routes
Now let’s add new routes! This directs traffic to specific destinations! 🎯
What we’re doing: Creating custom routes for specific networks.
# Add route to specific network
ip route add 10.0.0.0/24 via 192.168.1.10
# Add route through specific interface
ip route add 172.16.0.0/16 dev eth1
# Add default route
ip route add default via 192.168.1.1
# Add route with metric (priority)
ip route add 10.1.0.0/24 via 192.168.1.20 metric 100
Code explanation:
10.0.0.0/24
: Network destination (IP range)via 192.168.1.10
: Gateway to use for this routedev eth1
: Interface to use directlymetric 100
: Route priority (lower numbers = higher priority)
Expected Output:
✅ Route to 10.0.0.0/24 added successfully
✅ Route via eth1 configured
What this means: Your computer now knows new paths to send data! 🌟
Deleting Routes
What we’re doing: Removing routes we don’t need anymore.
# Remove specific route
ip route del 10.0.0.0/24 via 192.168.1.10
# Remove route by destination only
ip route del 172.16.0.0/16
# Remove default route
ip route del default via 192.168.1.1
# Check routes after deletion
ip route show
What this does: Cleans up routing table by removing unused paths! 📚
📊 Quick Summary Table
Command | Purpose | Result |
---|---|---|
🌐 ip route show | View routes | ✅ Shows routing table |
➕ ip route add | Add new route | ✅ Creates path |
➖ ip route del | Remove route | ✅ Deletes path |
🔧 route -n | Show numeric routes | ✅ Displays all routes |
🎮 Step 3: Advanced Routing Configuration
Setting Up Multiple Gateways
Let’s configure multiple internet connections! This is powerful! 🌟
What we’re doing: Setting up load balancing and failover between gateways.
# Install routing utilities
apk add iproute2 iptables
# Create routing tables
echo "200 wan1" >> /etc/iproute2/rt_tables
echo "201 wan2" >> /etc/iproute2/rt_tables
# Configure first gateway
ip route add default via 192.168.1.1 dev eth0 table wan1
ip rule add from 192.168.1.0/24 table wan1
# Configure second gateway
ip route add default via 192.168.2.1 dev eth1 table wan2
ip rule add from 192.168.2.0/24 table wan2
# Set up load balancing
ip route add default scope global \
nexthop via 192.168.1.1 dev eth0 weight 1 \
nexthop via 192.168.2.1 dev eth1 weight 1
What this does: Creates smart routing with multiple internet connections! 🚀
Policy-Based Routing
What we’re doing: Creating rules that route traffic based on specific criteria.
# Route traffic from specific source
ip rule add from 10.0.1.0/24 table wan1 priority 100
# Route traffic to specific destination
ip rule add to 8.8.8.8 table wan2 priority 200
# Route traffic based on port
iptables -t mangle -A OUTPUT -p tcp --dport 80 -j MARK --set-mark 1
ip rule add fwmark 1 table wan1 priority 300
# Create rules for different services
ip rule add from 192.168.1.100 table wan2 priority 150
ip rule add to 192.168.10.0/24 table wan1 priority 250
# Show all rules
ip rule show
Expected Output:
0: from all lookup local
100: from 10.0.1.0/24 lookup wan1
150: from 192.168.1.100 lookup wan2
200: to 8.8.8.8 lookup wan2
250: to 192.168.10.0/24 lookup wan1
300: from all fwmark 0x1 lookup wan1
32766: from all lookup main
32767: from all lookup default
What this means: Traffic follows smart rules based on source and destination! 🎉
🎮 Practice Time!
Let’s practice what you learned! Try these simple examples:
Example 1: Create Network Segments 🟢
What we’re doing: Setting up routing between different network segments.
# Create virtual interfaces for testing
ip link add veth0 type veth peer name veth1
ip link set veth0 up
ip link set veth1 up
# Assign IP addresses
ip addr add 10.1.1.1/24 dev veth0
ip addr add 10.2.2.1/24 dev veth1
# Create routes between segments
ip route add 10.1.1.0/24 dev veth0
ip route add 10.2.2.0/24 dev veth1
# Test connectivity
ping -c 3 10.1.1.1
ping -c 3 10.2.2.1
# Show routing table
ip route show | grep "10\."
echo "Network segments configured! ✅"
What this does: Creates separate network segments with proper routing! 🌟
Example 2: Configure Route Persistence 🟡
What we’re doing: Making routes survive system reboots.
# Create network configuration file
cat > /etc/network/interfaces << 'EOF'
# Alpine Linux network configuration
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up ip route add 10.0.0.0/8 via 192.168.1.10
up ip route add 172.16.0.0/12 via 192.168.1.20
down ip route del 10.0.0.0/8 via 192.168.1.10
down ip route del 172.16.0.0/12 via 192.168.1.20
EOF
# Create custom routing script
cat > /etc/local.d/custom-routes.start << 'EOF'
#!/bin/sh
# Custom routing configuration
# Add static routes
ip route add 192.168.100.0/24 via 192.168.1.50
ip route add 192.168.200.0/24 via 192.168.1.60
# Configure policy routing
ip rule add from 192.168.1.0/24 table main priority 100
EOF
chmod +x /etc/local.d/custom-routes.start
# Enable local.d service
rc-update add local default
# Test configuration
/etc/local.d/custom-routes.start
echo "Persistent routing configured! 📚"
What this does: Ensures your routing configuration survives reboots! 📚
🚨 Fix Common Problems
Problem 1: Route not working ❌
What happened: Added route but traffic doesn’t flow. How to fix it: Check route details and connectivity!
# Verify route was added
ip route show | grep "your-destination"
# Test gateway connectivity
ping -c 3 192.168.1.1
# Check interface status
ip link show
# Verify IP forwarding is enabled
sysctl net.ipv4.ip_forward
# Enable IP forwarding if needed
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
Problem 2: Multiple routes conflict ❌
What happened: Different routes pointing to same destination. How to fix it: Check route priorities and remove conflicts!
# Show routes with metrics
ip route show | grep "your-destination"
# Remove conflicting route
ip route del conflicting-route
# Add route with specific metric
ip route add 10.0.0.0/24 via 192.168.1.10 metric 100
# Verify only one route exists
ip route show | grep "10.0.0.0/24"
Problem 3: Routes disappear after reboot ❌
What happened: Routes don’t persist after restart. How to fix it: Make configuration permanent!
# Check if networking service is enabled
rc-status | grep networking
# Create persistent route file
echo "ip route add 10.0.0.0/24 via 192.168.1.10" > /etc/conf.d/net.eth0
# Or add to interfaces file
echo " up ip route add 10.0.0.0/24 via 192.168.1.10" >> /etc/network/interfaces
# Restart networking
rc-service networking restart
Don’t worry! Network routing takes practice. You’re doing great! 💪
💡 Simple Tips
- Test carefully 📅 - Small changes first
- Document routes 🌱 - Keep notes of what you add
- Use metrics 🤝 - Set priorities properly
- Monitor traffic 💪 - Watch what happens
✅ Check Everything Works
Let’s verify routing is working correctly:
# Check routing table
ip route show
# Test default gateway
ping -c 3 $(ip route | grep default | awk '{print $3}')
# Test internet connectivity
ping -c 3 8.8.8.8
# Check route to specific destination
traceroute 8.8.8.8
# Verify policy rules
ip rule show
# Test custom routes if configured
ping -c 3 10.0.0.1
echo "Network routing verification completed! ✅"
Good output:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 scope link
PING 192.168.1.1: 3 packets transmitted, 3 received
PING 8.8.8.8: 3 packets transmitted, 3 received
✅ Traceroute shows proper path
Network routing verification completed! ✅
🔧 Advanced Routing Features
Dynamic Routing with BIRD
Let’s set up dynamic routing for complex networks! This is professional! 🎯
What we’re doing: Installing and configuring BIRD routing daemon.
# Install BIRD routing daemon
apk add bird
# Create BIRD configuration
cat > /etc/bird.conf << 'EOF'
# BIRD Routing Daemon Configuration
# Router ID (use your main IP)
router id 192.168.1.100;
# Enable debugging
debug protocols all;
# Device protocol for interface discovery
protocol device {
scan time 10;
}
# Direct protocol for directly connected networks
protocol direct {
interface "*";
}
# Kernel protocol to sync with system routing table
protocol kernel {
export all;
scan time 20;
}
# Static routes
protocol static {
route 10.0.0.0/8 via 192.168.1.10;
route 172.16.0.0/12 via 192.168.1.20;
}
# OSPF protocol for dynamic routing
protocol ospf {
area 0.0.0.0 {
interface "eth0" {
hello 10;
dead count 5;
};
};
}
EOF
# Start BIRD daemon
rc-service bird start
rc-update add bird default
# Check BIRD status
birdc show protocols
birdc show route
What this does: Creates professional dynamic routing capabilities! 🌟
Network Monitoring and Troubleshooting
What we’re doing: Setting up tools to monitor and debug routing.
# Install network monitoring tools
apk add tcpdump wireshark-common mtr
# Create network monitoring script
cat > /usr/local/bin/network-monitor.sh << 'EOF'
#!/bin/sh
# Network Routing Monitor
LOG_FILE="/var/log/network-monitor.log"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" | tee -a "$LOG_FILE"
}
# Check routing table changes
check_routes() {
local current_routes=$(ip route show | md5sum)
local stored_routes="/tmp/last_routes.md5"
if [ -f "$stored_routes" ]; then
if [ "$current_routes" != "$(cat $stored_routes)" ]; then
log_message "Routing table changed!"
ip route show | tee -a "$LOG_FILE"
fi
fi
echo "$current_routes" > "$stored_routes"
}
# Test gateway connectivity
test_gateways() {
ip route | grep "^default" | while read route; do
gateway=$(echo $route | awk '{print $3}')
if ping -c 1 -W 2 "$gateway" >/dev/null 2>&1; then
log_message "Gateway $gateway: OK"
else
log_message "Gateway $gateway: FAILED"
fi
done
}
# Monitor network interfaces
check_interfaces() {
ip link show | grep "state DOWN" | while read line; do
interface=$(echo $line | awk '{print $2}' | sed 's/:$//')
log_message "Interface $interface is DOWN"
done
}
# Run checks
log_message "=== Network monitoring started ==="
check_routes
test_gateways
check_interfaces
log_message "=== Network monitoring completed ==="
EOF
chmod +x /usr/local/bin/network-monitor.sh
# Set up monitoring cron job
echo "*/5 * * * * /usr/local/bin/network-monitor.sh" | crontab -
# Test monitoring
/usr/local/bin/network-monitor.sh
echo "Network monitoring configured! 📊"
Expected Output:
2025-06-03 18:45:01: === Network monitoring started ===
2025-06-03 18:45:01: Gateway 192.168.1.1: OK
2025-06-03 18:45:01: === Network monitoring completed ===
Network monitoring configured! 📊
What this means: You have professional network monitoring! 🎉
🏆 What You Learned
Great job! Now you can:
- ✅ View and understand routing tables
- ✅ Add and remove static routes
- ✅ Configure advanced routing with multiple gateways
- ✅ Set up persistent routing and monitoring!
🎯 What’s Next?
Now you can try:
- 📚 Learning about BGP routing for large networks
- 🛠️ Setting up VPN routing and tunnels
- 🤝 Implementing quality of service (QoS) routing
- 🌟 Building complex multi-homed network setups!
Remember: Every network engineer was once a beginner. You’re doing amazing! 🎉
Keep practicing and you’ll become an expert too! 💫