Static routes are generally required for traffic that must not, or should not, go through the default gateway. In this article we will discuss how to add static routes in various nix.

Scenario: Suppose if you want that all the traffic to network 172.168.102.0/24 should use 172.168.101.1 as gateway. This can be done by adding a static route in the kernel routing table as shown below.

Adding static route in Linux from the command line

# route add -net 172.168.102.0 netmask 255.255.255.0 gw 172.168.101.1 dev eth0

OR

# ip route add 172.168.102.0/24 via 172.168.101.1 dev eth0

Above Commands will make changes to the routing table temporary and not permanent. Use any of below mention command To check Routing tables in Linux:

Steps to make the static Route Persistent Across the reboot :

For RHEL/CentOS 5.X

Create a route file as shown below:

# vi /etc/sysconfig/network-scripts/route-eth0
172.168.102.0/24 via 172.168.101.1 dev eth0

Save and close the file and Restart network service:

# service network restart

For RHEL/CentOS 6.X

Create a route file as shown below:

# vi /etc/sysconfig/network-scripts/route-eth0
GATEWAY0= 172.168.101.1
NETMASK0=255.255.255.0
ADDRESS0= 172.168.102.0

Save and close the file and Restart network service:

# service network restart

Adding Static Routes in AIX

Step 1: Go to the SMITTY menu for routes.

Step 2: Select Type of route ‘net’ or ‘host’ (if default route then leave set to ‘net’).

Step 3: Enter the destination address.

Step 4: Enter the gateway address ( on the line that “* default GATEWAY Address”)

Step 5: If a ‘net’ or default route , enter the ‘Network Mask’ , if host do not set ‘Network Mask’

Step 6: Enter the network interface for this route. To select from the list arrow down to the ‘Network Interface’ line and hit[F4] or [ESC]+ [4] to display list of available interfaces.

Step 7: Hit [ENTER] TO APPLY . You should recieve a return status of “OK”

Step 8: To exit the smitty, type [F10] or [0].

Step 9: Verify that your routes have been configured.

Adding Static Route in HP-UX

Step 1: Make a backup copy of ‘/etc/rc.config.d/netconf‘.

Step 2: Add a stanza to /etc/rc.config.d/netconf for the new route. Make sure you use a new array number for the stanza.

Example : Replace ‘nn’ with the next number in the list.

ROUTE_DESTINATION[nn]="IP-of-NewHost"
ROUTE_MASK[nn]=" "
ROUTE_GATEWAY[nn]="IP-of-Router"
ROUTE_COUNT[nn]=""
ROUTE_ARGS[nn]=""

Save and Close the file.

Step 3: Now run the below command to re-read the netconf file and add the route.

Note: Run the above command with start option only because it will add new route without effecting existing network configuration.