Using the route
Command to Display Network Information on Linux
Objective
Learn how to use the route
command on Linux to view and manipulate the system’s routing table. This lab focuses on displaying network information, adding and deleting routes, and troubleshooting connectivity issues.
Prerequisites
- Linux Environment:
- A Linux system with administrative privileges.
- Basic Networking Knowledge:
- Familiarity with IP addresses, gateways, and subnet masks.
- Installed Tools:
- Ensure the
route
command is available:route --version
- If not available, install the
net-tools
package:sudo apt update && sudo apt install net-tools
- Ensure the
Step 1: Viewing the Current Routing Table
- Open a terminal.
- Run the
route
command:route
- Analyze the output:
- Destination: The target network or host.
- Gateway: The next-hop address used to reach the destination.
- Genmask: The subnet mask associated with the destination.
- Iface: The network interface handling the traffic.
Example Output:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
Key Flags:
- U: Route is up.
- G: Route is a gateway.
- Use
-n
to display numeric addresses:route -n
- This avoids resolving hostnames for faster output.
Step 2: Adding a Static Route
- Add a route to a specific network:
sudo route add -net <network> netmask <subnet_mask> gw <gateway> dev <interface>
- Replace
<network>
with the target network (e.g.,192.168.2.0
). - Replace
<subnet_mask>
with the subnet mask (e.g.,255.255.255.0
). - Replace
<gateway>
with the gateway IP (e.g.,192.168.1.1
). - Replace
<interface>
with the network interface (e.g.,eth0
).
Example:
sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
- Replace
- Verify the new route:
route -n
Step 3: Deleting a Static Route
- Remove a route:
sudo route del -net <network> netmask <subnet_mask>
- Example:
sudo route del -net 192.168.2.0 netmask 255.255.255.0
- Example:
- Confirm the route has been removed:
route -n
Step 4: Setting the Default Gateway
- Add or modify the default gateway:
sudo route add default gw <gateway_ip> dev <interface>
- Replace
<gateway_ip>
with the IP of the default gateway (e.g.,192.168.1.1
). - Replace
<interface>
with the network interface (e.g.,eth0
).
Example:
sudo route add default gw 192.168.1.1 dev eth0
- Replace
- Verify the default route:
route -n
- Remove the default gateway if needed:
sudo route del default gw <gateway_ip>
Step 5: Troubleshooting with the Routing Table
- Check for Missing Routes:
- Ensure there is a route for the target network in the routing table.
- Diagnose Gateway Issues:
- Use
ping
to verify the gateway is reachable:ping <gateway_ip>
- Use
- Incorrect Routes:
- Remove and re-add routes if the traffic is being misrouted.
- Monitor Route Usage:
- Use the
ip route show
command for real-time route information:ip route show
- Use the
Additional Tips and Insights
- Use Persistent Routes:
- Add routes to the
/etc/network/interfaces
file or systemd network configuration for persistence across reboots.
- Add routes to the
- Combine Tools:
- Use
traceroute
orping
alongsideroute
to troubleshoot end-to-end connectivity.
- Use
- Transition to Modern Tools:
- The
route
command is deprecated on some distributions. Useip route
for enhanced functionality:ip route add <network>/<prefix> via <gateway> dev <interface>
- The
- Security Awareness:
- Regularly audit routes to ensure no unauthorized changes have been made.
Key Takeaways
- The
route
command is essential for viewing and managing routing tables in Linux. - Understanding how to add, delete, and troubleshoot routes helps ensure proper network connectivity.
- Transitioning to modern tools like
ip route
ensures compatibility with newer Linux distributions.