Home / How to add a new network gateway or static route

How to add a new network gateway or static route

How can I add an additional network gateway to an AlmaLinux and CentOS Linux host?

Configure Using Network Manager

Use nmcli, nmtui, or the GUI tools to change connection routes.

Modify the ipv4.routes property of the connection with nmcli to add a route. For a connection named “external,” for example, to add a route to the 10.20.30.0/24 network via 192.168.100.10.

# nmcli connection modify external ipv4.routes "10.20.30.0/24 192.168.100.10"

To add additional routes, use the + (plus) modifier on the ipv4.routes property of the connection.

# nmcli connection modify external +ipv4.routes "10.0.1.0/24 192.168.100.20"

Numerous routes can be added simultaneously, separated by a comma.

# nmcli connection modify external ipv4.routes "10.20.30.0/24 192.168.100.10, 10.0.1.0/24 192.168.100.20"

To delete a route, specify the route to be deleted using the – (minus) modifier on the connection’s ipv4.routes property.

# nmcli connection modify external -ipv4.routes "10.0.1.0/24 192.168.100.20"

To disable all routes, change the value of the ipv4.routes property to “” [empty] (*1).

# nmcli connection modify external ipv4.routes ""

(*1) If no “+/-” is specified, it means that the entire list of current routes will be updated. However, if you specify “” [empty], it will remove all routes regardless of whether “+/-” is specified.

Bring the connection back up after making any of the modifications listed above. For instance, following the modification of the properties of the “external” connection.

# nmcli connection up external

Configure Without Network Manager

To add static routes, create or modify a route-<interface> file in the /etc/sysconfig/network-scripts/ directory, where <interface> is the name of the interface to which the routes should be added. Each route should be contained on a single line and take the basic form <network/prefix> via <gateway>, where <network/prefix> denotes the remote network’s IP address with prefix and gateway> denotes the next hop’s IP address. For instance, to add a route to the 10.20.30.0/24 network via 192.168.100.10 and another to the 10.0.1.0/24 network via 192.168.10.20, both of which should be active when eth0 is up.

$ cat /etc/sysconfig/network-scripts/route-eth0
10.20.30.0/24 via 192.168.100.10
10.0.1.0/24 via 192.168.10.20

For changes to take effect, the interface must be brought up again.

# ifup eth0

There is an older syntax for route-<interface> files recognized by all versions of CentOS that don’t use NetworkManager files.

ADDRESS<N>=X.X.X.X
NETMASK<N>=Y.Y.Y.Y
GATEWAY<N>=Z.Z.Z.Z
e.g.
ADDRESS0=10.10.10.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.1.2
ADDRESS1=20.20.20.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.1.2

This format has three fields: GATEWAY, NETMASK, and ADDRESS. These are the three fields this format deals with. Each field should have a number next to it that tells you which route it is for.

The gateway IP address is Z.Z.Z.Z in the example above. The following entries must be numbered in order (for example, ADDRESS1=, NETMASK1=, GATEWAY1=). Multiple entries must be numbered sequentially and must not skip a value (0 must be followed by 1, not a number greater than 1).

Leave a Reply