How to Add Permanent Routes in CentOS 7

Usually we add ip routes in Linux using either route command or "ip route" command. While both commands work the same way, neither command can use to add permanent routes to the system. They will get removed if you restart the network or if the server rebooted.

We have two methods to add permanent static routes in CentOS 7. One method is to use nmcli command, another method is to add entries manually by directly adding the configuration file into the /etc/sysconfig/network-scripts/ directory.

Method One : Using nmcli command line tool

Manage networking using nmcli command is the preferred way in CentOS 7. Use following format to add persistent route using nmcli command.

nmcli connection modify "connection-name" +ipv4.routes "network address/prefix gateway"

nmcli connection up "connection-name"

You can use the following command to list configured network connections on your CentOS 7 server.

nmcli connection show

Example

For example, lets add an entry that will route all traffic to the 10.0.0.0/24 network through the gateway of 192.168.1.10. And the name of the connection is enp0s3.

nmcli connection modify "enp0s3" +ipv4.routes "10.0.0.0/24 192.168.1.10"

nmcli connection reload

nmcli connection up enp0s3

Do not Forget the + mark before the keyword ipv4.routes (+ipv4.routes). Otherwise, existing entries will be overridden.

Method Two : Manually Create Configuration File

It is also possible to add permanent IP routes directly into the configuration file. First, we need to create the configuration file inside the /etc/sysconfig/network-scripts/ directory using the following format.

route-connectioname

For example, if the name of the network connection is enp0s3, then The name of the configuration file should be

route-enp0s3

Next Insert the routing to the configuration file using following format.

10.0.0.0/24 via 192.168.1.10

192.168.100.0/24 via 192.168.1.10

192.168.50.10/32 via 192.168.1.1

Alternatively, you can also use the following format.

ADDRESS0=10.0.0.0

NETMASK0=255.255.255.0

GATEWAY0=192.168.1.10

ADDRESS1=192.168.50.10

NETMASK1=255.255.255.255

GATEWAY1=192.168.1.1

Finally, you need to restart the network connection using nmcli command.

nmcli connection reload

nmcli connection up enp0s3

You can use the either of these two methods to add permanent routes in CentOS 7 Linux.