Add static Route on Linux

To totally unlock this section you need to Log-in


Static routes are for traffic that must not, or should not, go through the default gateway. Routing is often handled by devices on the network dedicated to routing (although any device can be configured to perform routing).

Therefore, it is often not necessary to configure static routes on Red Hat Enterprise Linux servers or clients. Exceptions include traffic that must pass through an encrypted VPN tunnel or traffic that should take a specific route for reasons of cost or security.

The default gateway is for any and all traffic which is not destined for the local network and for which no preferred route is specified in the routing table. The default gateway is traditionally a dedicated network router.

You can use any one of the following command line utility to add, delete, display, or manipulate the Linux kernel routing table on CentOS and friends:

ip command: a Linux command line tool to print / manipulate routing, devices, policy routing and tunnels.

route command: older command line utility to show or manipulate the Linux kernel routing table. I suggest that you use ip command instead of route command. This command exists for historical and compatibility reasons only.

You need to edit the following configuration files for static route configuration:

/etc/sysconfig/network - Edit this file to set default gateway IP address.

/etc/sysconfig/network-scripts/route-ethX - Edit this file to set additional static gateway IP address.

Displaying current routing table

Type any one of the following command:

# netstat -nr

# route -n
# ip route list

Sample outputs:

Add static Route on Linux

Warning: It is important that you configure routing correctly over ssh based session; otherwise, you will be locked out due to wrong network configuration.

Add a default gateway

In this example, route all traffic via gateway connected via eth0 network interface. The following command will set a default gateway for both internal and external network (if any):

# route add default gw eth0

# ip route add dev eth0

How do I make routing changes persistent across CentOS Linux server reboots?

To set default gateway edit /etc/sysconfig/network as follows:

# cat /etc/sysconfig/network

Sample configuration file:


## server name ##
## Default route ##

Save and close the file. Restart the networking service on CentOS Linux, type:

# service network restart

# ip route list

To verify new settings ping to the default gateway and external network:

# ping

# ping
# host

Linux static routing config for eth1 interface

The following is a sample route-eth1 file. The default gateway set to, interface eth1. The static route is for network:

# cat /etc/sysconfig/network-scripts/route-eth1

Sample configurations:

default dev eth1 via dev eth1

Alert: for eth0 interface use /etc/sysconfig/network-scripts/route-eth0 file. Avoid setting duplicate default gateways; either use /etc/sysconfig/network or /etc/sysconfig/network-scripts/route-ethX file. Do not use both files to configure default gateways.

A note about GUI/TUI tool

If you don't have X windows GUI (gnome/kde desktop) installed on server / system, than type the following command at shell prompt. This method can only set the default gateway. It can not be used to set additional static routing for interface. Type the following command:

# system-config-network-tui &

Add static Route on Linux

Select your ethernet card such as eth0 or eth1 and hit [Enter] or [F12] special key to configure IP properties for selected NIC:

Add static Route on Linux

You can obtain an IP address using DHCP or setup IP address manually. Once an IP address assigned, click on the OK button to save the changes.