Configuring a Static Route Using the Command Line

To configure static routes with the nmcli command, use the following syntax:

nmcli connection modify connection_name +ipv4.routes "ip[/prefix] options(s) attribute(s)"[next_hop] [metric] [attribute=value] [attribute=value] ..."
+ipv4.routes
The plus (+) sign indicates that you're creating an IPv4 route. Without the sign, the command changes an existing IPv4 setting.
connection-name
Connection name or label for which you're creating a static route.
ip[/prefix]
IP address of the static route that you're creating. The IP address can also be in CIDR notation.
options
Options include next hop addresses and optional route metrics. These options are separated by spaces. For more information, see the nm-settings-nmcli(5) manual pages.
attributes
Attributes are entered as attribute=value and are also separated by spaces. Some attributes are mtu, src, type, cwnd, and so on. For more information, see the nm-settings-nmcli(5) manual pages.

Suppose that you have the following configurations:

  • Name of the connection: myconnection
  • Default gateway address: 198.51.100.1
  • Network to which you want to create a static route: 192.0.2.0/24

To create the route, ensure first that the default gateway for the route is directly reachable on the interface. Then, do the following:

  1. Create the static route.
    sudo nmcli connection modify myconnection +ipv4.routes "192.0.2.0/24 198.51.100.1"

    To create several static routes in a single command, separate the route gateway entries with commas, for example:

    sudo nmcli connection modify myconnection +ipv4.routes "192.0.2.0/24 198.51.100.1, 203.0.113.0/24 198.51.100.1"
  2. Verify the new routing configuration.
    nmcli connection show myconnection
    –-
    ipv4.routes:   { ip = 192.0.2.0/24, nh = 198.51.100.1 }
    –-
  3. Restart the network connection.

    This step causes the connection to temporarily drop.

    sudo nmcli connection up myconnection
  4. Verify that the new route is active.
    ip route
    ...
    192.0.2.0/24 via 198.51.100.1 dev example proto static metric 100