By default, when a Linux system tries to communicate with a network address, the computer will send the request to the default gateway. The default gateway is usually a router, which can take the system's request and forward it to the next hop, wherever that may be.

This behavior can be overridden by adding one or more static routes to the Linux machine. Such a configuration can be desirable if the network has multiple networks and routers, and a user needs to instruct the computer which way to route certain traffic.

Red Hat based distributions, such as AlmaLinux, can use the nmcli command line utility to configure static routes, along with the ip route command and manual configuration of ifcfg files. Alternatively, you can use your installed desktop environment to apply the configuration. In this guide, we'll go over the step by step instructions to add static routes on AlmaLinux through both command line and GUI methods. These instructions are applicable whether you've freshly installed AlmaLinux or have migrated from CentOS to AlmaLinux.

In this tutorial you will learn:
  • How to add a route via nmcli command
  • How to add a route via ip route command
  • How to add a route via ifcfg files
  • How to add a route via GNOME GUI
Adding a new static route in AlmaLinux
Adding a new static route in AlmaLinux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System AlmaLinux
Software nmcli, GNOME
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

How to add a route via nmcli command

One way to add a static route on AlmaLinux is by using NetworkManager's nmcli command line utility. You can use either the nmcli command or the nmcli interactive editor. We'll look at both methods below.


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


The following command will route traffic destined for the 192.168.1.0/24 subnet to a gateway located at 10.10.10.1. Of course, substitute your own values in place of our examples, including the name of your network interface.

# nmcli connection modify enp0s3 +ipv4.routes "192.168.1.0/24 10.10.10.1"

We can also use the nmcli interactive editor to apply the same configuration. Launch the editor with the following command, being sure to use the name of the network interface you wish to configure.

# nmcli con edit enp0s3

Then, apply the new route.

nmcli> set ipv4.routes 192.168.1.0/24 10.10.10.1
nmcli> save persistent
Connection 'enp0s3' (b737826a-2aef-4b03-99cf-ccb7a500b6a5) successfully updated.
nmcli> quit

How to add a route via ip route command

To add a new route with the ip route command, use the following command syntax. This example will route traffic destined for the 192.168.1.0/24 subnet to a gateway located at 10.10.10.1 on interface enp0s3.

# ip route add 192.168.1.0/24 via 10.10.10.1 enp0s3

To see the configured routes, you can use the following command.

# ip route show

How to add a route via ifcfg files

Another way to add a static route is by directly editing the interface configuration files. These files are stored in the /etc/sysconfig/network-scripts/ directory. Edit the file in this directory named route-enp0s3, but substitute your own interface's name in place of our example.

# nano /etc/sysconfig/network-scripts/route-enp0s3


Make your changes to the first line in this file, adding the route by using the following syntax from our previous examples.

192.168.1.0/24 via 10.10.10.1 dev enp0s3

You'll need to restart the network for the changes to take effect.

How to add a route via GNOME GUI

If you have the default GNOME GUI installed on AlmaLinux, it's very easy to add a new route to your network interface.

  1. Click on the top right settings area of the taskbar. Select your network interface you wish to edit, and open its settings.
  2. Open the settings for your network interface
    Open the settings for your network interface
  3. Click on the cog wheel settings icon.
  4. Click the cog wheel to open the configuration menu
    Click the cog wheel to open the configuration menu
  5. Select the IPv4 or IPv6 tab, depending on which one you want to configure. Then, add your custom routes under the "routes" section. Once you're satisfied with the new settings, click apply and close the settings menu.
  6. Configure static routes
    Configure static routes

Closing Thoughts

In this guide, we saw several ways for adding a static route on AlmaLinux via command line and GUI. AlmaLinux gives us a lot of ways to configure static routes, so we get to pick whichever one we find most convenient. Personally, I find the nmcli interactive editor and GNOME to be the easiest methods. Hopefully this will get your traffic headed to where it's supposed to go.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.