CentOS Linux: Add Static Routing

I am a new CentOS Linux sysadmin. How can I add static route On CentOS Enterprise Linux server running on HP amd64 server?

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:
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements CentOS/RHEL
Time 10m
  1. ip command – A CentOS Linux command line tool to print / manipulate routing, devices, policy routing and tunnels.
  2. 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 :

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

CentOS: Displaying current routing table

Type any one of the following command:
# netstat -nr
# route -n
# ip route list

Sample outputs:

Fig.01: ip command displaying current CentOS Linux routing table

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

CentOS Linux 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 www.cyberciti.biz
# host google.com

CentOS 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 CentOS 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 &

Fig.01: CentOS command line network config tool (click to large)

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

Fig.02: Setting up IPv4 properties on CentOS including default gateway (click to large)

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. See how to configure networking on CentOS / RHEL / Fedora Linux tutorial for more information.

Recommended readings

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • BT Feb 23, 2014 @ 8:24

    you left out “via” on your sample config, should be
    default via X.X.X.X dev interface

  • Jhon Edison Aug 25, 2015 @ 4:19

    Gracias!!! funciona..

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz