YouTip LogoYouTip

Linux Comm Route

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) * * * ## 1. route Command Overview The route command is a powerful tool in Linux systems for viewing and manipulating IP routing tables. As a fundamental command for network configuration, it allows administrators to: * Display current routing table information * Add/delete static routes * Modify existing routing rules * Manage the transmission path of network packets ### Basic Syntax route * * * ## 2. Routing Table Fundamentals ### 2.1 What is a Routing Table A routing table is a data structure maintained by the operating system kernel that determines how network packets are transmitted from source addresses to destination addresses. It can be compared to: > "City traffic navigation system: when you want to go to a certain destination, the system tells you which route to take for the fastest and most efficient journey" ### 2.2 Key Fields of Routing Table When viewing the routing table with `route -n`, the following important columns are displayed: | Column Name | Description | Example Value | | --- | --- | --- | | Destination | Target network or host | 192.168.1.0 | | Gateway | Next hop gateway | 192.168.1.1 | | Genmask | Network mask | 255.255.255.0 | | Flags | Route flags | U (Route is up) | | Metric | Route cost | 0 | | Iface | Egress interface | eth0 | * * * ## 3. Common route Command Operations ### 3.1 Viewing the Routing Table ## Example # Display the complete routing table (without resolving hostnames) route -n # Output example: 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 ### 3.2 Adding Routes ## Example # Add a route to a specific host route add -host 192.168.1.100 dev eth0 # Add a route to a network route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1 # Add a default gateway route add default gw 192.168.1.1 ### 3.3 Deleting Routes ## Example # Delete a specific route route del -net 192.168.2.0 netmask 255.255.255.0 # Delete the default gateway route del default gw 192.168.1.1 * * * ## 4. Command Options Explained The route command supports the following common options: | Option | Description | | --- | --- | | -n | Display addresses in numeric form (do not resolve hostnames) | | -e | Display routing table in netstat format | | -v | Display detailed operation information | | -A family | Specify address family (inet, inet6, etc.) | | -F | Display kernel FIB routing table | | -C | Display routing cache | ### Special Command Parameters: * `add`: Add a route * `del`: Delete a route * `-host`: Operation targets a specific host * `-net`: Operation targets an entire network * `gw`: Specify gateway * `dev`: Specify network interface * * * ## 5. Practical Application Scenarios ### Scenario 1: Multi-NIC Routing Configuration When a server has multiple network interfaces, you need to specify egress interfaces for different networks: ## Example # Make traffic destined for 10.0.0.0/24 go through the eth1 interface route add -net 10.0.0.0 netmask 255.255.255.0 dev eth1 ### Scenario 2: VPN Routing Configuration After connecting to a VPN, you need to add specific routes: ## Example # Only let specific network segments use the VPN tunnel route add -net 172.16.0.0 netmask 255.240.0.0 dev tun0 ### Scenario 3: Temporary Route Testing ## Example # Temporarily add a route for testing (will be lost after reboot) route add -host 8.8.8.8 gw 192.168.1.254 ping 8.8.8.8 # Test if the route takes effect route del -host 8.8.8.8 # Delete after testing is complete * * * ## 6. Persistent Routing Configuration Routes added through the route command will be lost after reboot. To make them permanent: ### 6.1 Debian/Ubuntu Systems Edit `/etc/network/interfaces`: ## Example auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 up route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.2 ### 6.2 RHEL/CentOS Systems Create a route configuration file `/etc/sysconfig/network-scripts/route-eth0`: 10.0.0.0/8 via 192.168.1.2 * * * ## 7. Common Troubleshooting ### 7.1 Route Not Taking Effect - Check Steps !(#) ### 7.2 Typical Error Solutions **Error 1**: "SIOCADDRT: Network is unreachable" * Cause: Gateway is not within the local network's direct broadcast domain * Solution: First ensure you can ping the gateway address **Error 2**: "RTNETLINK answers: File exists" * Cause: Attempting to add a route that already exists * Solution: Delete the old route first before adding, or use the `replace` option * * * ## 8. Alternative Command: ip route Modern Linux systems recommend using the `ip route` command: ## Example # Display routing table ip route show # Add a route ip route add 10.0.0.0/8 via 192.168.1.2 # Delete a route ip route del 10.0.0.0/8 Although `ip route` is more powerful, the `route` command is still widely used due to its simplicity and intuitiveness. * * * Through this guide, you should have mastered the core usage of the route command. It is recommended to practice these commands in actual environments and observe how routing changes affect network connections. When encountering complex routing problems, you can combine tools like `traceroute` and `ping` for comprehensive diagnosis. [![Image 3: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm HostnameLinux Comm Curl β†’