How to add a new static route on RHEL/CentOS 4.x/5.x/6.x/7.x Linux

by Vivek Gite

I have two network interfaces connected to two different routers as follows:

[a] eth0 LAN network 10.0.0.0/8 – gateway IP – 10.8.2.65

[b] eth1 ISP assigned network 202.54.22.128/28 – gateway IP – 202.54.22.129

I can only ping to public server but not to another servers inside my LAN? I am not able to route traffic via 10.8.2.65. How do I configure static routing under Red Hat Enterprise Linux 5.x/6.x/7.x or CentOS Linux 5.2/6.x/7.x?

How to add a new static route on a centos rhel linux
Under Red Hat Enterprise Linux or CentOS Linux you need to define static routing using the route command or ip command. The configuration is stored under /etc/sysconfig/network-scripts/route-eth0 for eth0 interface.

Update route using route command

Type the following command:
# route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.8.2.65 eth0
# route -n

OR use the ip command to update/set new routing for you:
# ip route add 10.0.0.0/8 via 10.8.2.65
# ip r

OR
# ip route add 10.0.0.0/8 via 10.8.2.65 dev eth0
# ip r

RHEL 7/CentOS 7.x can use the following syntax to set static routing:
# nmcli connection modify eth0 +ipv4.routes "10.0.0.0/8 via 10.8.2.65"

Create static routing file for CentOS/RHEL version 4.x/5.x/6.x and older only

The drawback of above route command is that, when RHEL/CentOS server reboots it will forget static routes. So store them in a configuration file for eth0 interface:
# echo '10.0.0.0/8 via 10.8.2.65' >> /etc/sysconfig/network-scripts/route-eth0
Restart networking:
# service network restart
Verify new changes:
# route -n
# ip r
# ip a
# ping 10.8.2.65
# ping 10.8.2.10
# ping google.com
# traceroute google.com
# traceroute 10.8.2.10

How to add a new static route on CentOS/RHEL version 7.x and above

To see current ip settings run:
# ip a
# ip r

Create a file named /etc/sysconfig/network-scripts/route-enp6s0 to route traffic for sub/net 10.0.0.0/8 via 10.8.2.65 for enp6s0 interface:
# echo "10.0.0.0/8 via 10.8.2.65 dev enp6s0" >> /etc/sysconfig/network-scripts/route-enp6s0
Finally restart the networking:
# systemctl restart network
# ip r
# ping 10.8.2.65
# ping 10.8.2.10
# ping google.com

Further readings:
  • man pages ip, route command

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Shared via Inoreader

iPhoneから送信

How to add a new static route on RHEL/CentOS 4.x/5.x/6.x/7.x Linux

留下评论