In server administration, understanding how to manage static routes is a crucial skill.
This guide will walk you through the process of adding static routes in CentOS 5.6, a popular Linux distribution often used in server environments. This tutorial was carried out on a VPS server, so the Network Interface Card (NIC) interface used is ‘venet0’ instead of ‘eth0’. However, the steps remain the same regardless of your environment.
It’s important to note that the configurations discussed here will be erased once the server is rebooted. If you want to permanently apply the routing table, you should use a persistent static route.
Understanding Your Original Routing Table
Before we begin, let’s take a look at the original routing table. You can view this by using the ‘netstat -rn’ command. The output will look something like this:
[root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
The routing table contains information about IP routing and is crucial for the operation of an IP network.
Adding a Static Route for a Specific Network
To add a static route for a specific network in Linux, you can use the ‘route add’ command followed by the ‘-net’ option, the network address, and the gateway address. For example:
[root@server ~]# route add -net 192.168.2.0/24 gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. You should see the new static route added to the table.
[root@server ~]# route add -net 192.168.2.0/24 gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.2.0 192.168.13.1 255.255.255.0 UG 0 0 0 venet0 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
To delete a static route for a specific network, you can use the ‘route del’ command followed by the ‘-net’ option, the network address, and the gateway address. For example:
[root@server ~]# route del -net 192.168.2.0/24 gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. The static route you added earlier should now be removed from the table.
[root@server ~]# route del -net 192.168.2.0/24 gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
Adding a Static Route for a Specific Host
To add a static route for a specific host in Linux, you can use the ‘route add’ command followed by the ‘-host’ option, the host IP address, and the gateway address. For example:
[root@server ~]# route add -host 192.168.2.5 gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. You should see the new static route added to the table.
[root@server ~]# route add -host 192.168.2.5 gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.2.5 192.168.13.1 255.255.255.255 UGH 0 0 0 venet0 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
To delete a static route for a specific host, you can use the ‘route del’ command followed by the ‘-host’ option, the host IP address, and the gateway address. For example:
[root@server ~]#] route del -host 192.168.2.5 gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. The static route you added earlier should now be removed from the table.
[root@server ~]# route del -host 192.168.2.5 gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
Adding a Default Gateway
A default gateway is a device that forwards traffic from the local subnet to devices on other subnets. To add a default gateway in Linux, you can use the ‘route add’ command followed by the ‘default’ keyword and the gateway address. For example:
[root@server ~]# route add default gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. You should see the new default gateway added to the table.
[root@server ~]# route add default gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 192.168.13.1 0.0.0.0 UG 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
To delete a default gateway, you can use the ‘route del’ command followed by the ‘default’ keyword and the gateway address. For example:
[root@server ~]# route del default gw 192.168.13.1
After running this command, you can check the updated routing table with ‘netstat -rn’. The default gateway you added earlier should now be removed from the table.
[root@server ~]# route del default gw 192.168.13.1 [root@server ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 venet0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 venet0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
Commands Mentioned
- netstat -rn – Displays the routing table
- route add -net [network address] gw [gateway address] – Adds a static route for a specific network
- route del -net [network address] gw [gateway address] – Deletes a static route for a specific network
- route add -host [host IP address] gw [gateway address] – Adds a static route for a specific host
- route del -host [host IP address] gw [gateway address] – Deletes a static route for a specific host
- route add default gw [gateway address] – Adds a default gateway
- route del default gw [gateway address] – Deletes a default gateway
Conclusion
Understanding how to add and delete static routes in CentOS 5.6 is a fundamental skill for any server administrator. This guide has walked you through the process step-by-step, explaining how to add and delete static routes for specific networks and hosts, as well as how to add and delete a default gateway. Remember, these configurations will be erased once the server is rebooted, so if you want to permanently apply the routing table, you should use a persistent static route.
Whether you’re managing a dedicated server, a VPS server, or a cloud hosting environment, these skills will prove invaluable. For more in-depth tutorials and guides, be sure to explore other topics on our best web servers page.
FAQs
-
What is a static route?
A static route is a manually configured network route that is used by the network layer protocols of a host’s operating system to determine where to send packets for a specific network destination.
-
What is a default gateway?
A default gateway is a device that forwards traffic from the local subnet to devices on other subnets. It serves as the forwarding host (router) for traffic that is addressed to a different subnet.
-
What is the differencebetween a static route and a default gateway?
A static route is a pre-determined path that network information must follow to reach a specific host or network, while a default gateway is a device that serves as an access point to another network.
-
What is the ‘route’ command in Linux?
The ‘route’ command in Linux is used to show or modify the IP routing table. It allows you to add, delete, or display routes.
-
What is the ‘netstat -rn’ command in Linux?
The ‘netstat -rn’ command in Linux is used to display the kernel routing table in numeric format. It’s a useful command for checking your network configuration and setting up a computer on a network.