Chapter 14. Setting a default gateway of an existing connection

In most situations, administrators set the default gateway when they create a connection. However, you can also set the default gateway after creating the connection.

This section describes how to set the default gateway of an existing network connection.

14.1. Setting the default gateway on an existing connection using nmcli

In most situations, administrators set the default gateway when they create a connection as explained in, for example, Section 11.1, “Configuring a static Ethernet connection using nmcli”.

This section describes how to set or update the default gateway on a previously created connection using the nmcli utility.

Prerequisites

  • At least one static IP address must be configured on the connection on which the default gateway will be set.
  • If the user is logged in on a physical console, user permissions are sufficient. Otherwise, user must have root permissions.

Procedure

  1. Set the IP address of the default gateway.

    For example, to set the IPv4 address of the default gateway on the example connection to 192.0.2.1:

    $ sudo nmcli connection modify example ipv4.gateway "192.0.2.1"

    For example, to set the IPv6 address of the default gateway on the example connection to 2001:db8:1::1:

    $ sudo nmcli connection modify example ipv6.gateway "2001:db8:1::1"
  2. Restart the network connection for changes to take effect. For example, to restart the example connection using the command line:

    $ sudo nmcli connection up example
    Warning

    All connections currently using this network connection are temporarily interrupted during the restart.

  3. Optionally, verify that the route is active.

    To display the IPv4 default gateway:

    $ ip -4 route
    default via 192.0.2.1 dev example proto static metric 100

    To display the IPv6 default gateway:

    $ ip -6 route
    default via 2001:db8:1::1 dev example proto static metric 100 pref medium

14.2. Setting the default gateway on an existing connection using the nmcli interactive mode

In most situations, administrators set the default gateway when they create a connection as explained in, for example, Section 11.5, “Configuring a dynamic Ethernet connection using the nmcli interactive editor”.

This section describes how to set or update the default gateway on a previously created connection using the interactive mode of the nmcli utility.

Prerequisites

  • At least one static IP address must be configured on the connection on which the default gateway will be set.
  • If the user is logged in on a physical console, user permissions are sufficient. Otherwise, the user must have root permissions.

Procedure

  1. Open the nmcli interactive mode for the required connection. For example, to open the nmcli interactive mode for the example connection:

    $ sudo nmcli connection edit example
  2. Set the default gateway.

    For example, to set the IPv4 address of the default gateway on the example connection to 192.0.2.1:

    nmcli> set ipv4.gateway 192.0.2.1

    For example, to set the IPv6 address of the default gateway on the example connection to 2001:db8:1::1:

    nmcli> set ipv6.gateway 2001:db8:1::1
  3. Optionally, verify that the default gateway was set correctly:

    nmcli> print
    ...
    ipv4.gateway:                           192.0.2.1
    ...
    ipv6.gateway:                           2001:db8:1::1
    ...
  4. Save the configuration:

    nmcli> save persistent
  5. Restart the network connection for changes to take effect:

    nmcli> activate example
    Warning

    All connections currently using this network connection are temporarily interrupted during the restart.

  6. Leave the nmcli interactive mode:

    nmcli> quit
  7. Optionally, verify that the route is active.

    To display the IPv4 default gateway:

    $ ip -4 route
    default via 192.0.2.1 dev example proto static metric 100

    To display the IPv6 default gateway:

    $ ip -6 route
    default via 2001:db8:1::1 dev example proto static metric 100 pref medium

14.3. Setting the default gateway on an existing connection using nm-connection-editor

In most situations, administrators set the default gateway when they create a connection. This section describes how to set or update the default gateway on a previously created connection using the nm-connection-editor application.

Prerequisites

  • At least one static IP address must be configured on the connection on which the default gateway will be set.

Procedure

  1. Open a terminal, and enter nm-connection-editor:

    $ nm-connection-editor
  2. Select the connection to modify, and click the gear wheel icon to edit the existing connection.
  3. Set the IPv4 default gateway. For example, to set the IPv4 address of the default gateway on the connection to 192.0.2.1:

    1. Open the IPv4 Settings tab.
    2. Enter the address in the gateway field next to the IP range the gateway’s address is within:

      set default gw in nm connection editor ipv4

  4. Set the IPv6 default gateway. For example, to set the IPv6 address of the default gateway on the connection to 2001:db8:1::1:

    1. Open the IPv6 tab.
    2. Enter the address in the gateway field next to the IP range the gateway’s address is within:

      set default gw in nm connection editor ipv6

  5. Click OK.
  6. Click Save.
  7. Restart the network connection for changes to take effect. For example, to restart the example connection using the command line:

    $ sudo nmcli connection up example
    Warning

    All connections currently using this network connection are temporarily interrupted during the restart.

  8. Optionally, verify that the route is active.

    To display the IPv4 default gateway:

    $ ip -4 route
    default via 192.0.2.1 dev example proto static metric 100

    To display the IPv6 default gateway:

    $ ip -6 route
    default via 2001:db8:1::1 dev example proto static metric 100 pref medium

14.4. Setting the default gateway on an existing connection using control-center

In most situations, administrators set the default gateway when they create a connection. This section describes how to set or update the default gateway on a previously created connection using the control-center application.

Prerequisites

  • At least one static IP address must be configured on the connection on which the default gateway will be set.
  • The network configuration of the connection is open in the control-center application.

Procedure

  1. Set the IPv4 default gateway. For example, to set the IPv4 address of the default gateway on the connection to 192.0.2.1:

    1. Open the IPv4 tab.
    2. Enter the address in the gateway field next to the IP range the gateway’s address is within:

      set default gw in control center ipv4

  2. Set the IPv6 default gateway. For example, to set the IPv6 address of the default gateway on the connection to 2001:db8:1::1:

    1. Open the IPv6 tab.
    2. Enter the address in the gateway field next to the IP range the gateway’s address is within:

      set default gw in control center ipv6

  3. Click Apply.
  4. Back in the Network window, disable and re-enable the connection by switching the button for the connection to Off and back to On for changes to take effect.

    Warning

    All connections currently using this network connection are temporarily interrupted during the restart.

  5. Optionally, verify that the route is active.

    To display the IPv4 default gateway:

    $ ip -4 route
    default via 192.0.2.1 dev example proto static metric 100

    To display the IPv6 default gateway:

    $ ip -6 route
    default via 2001:db8:1::1 dev example proto static metric 100 pref medium

14.5. Setting the default gateway on an existing connection when using the legacy network scripts

This procedure describes how to configure a default gateway when you use the legacy network scripts. The example sets the default gateway to 192.0.2.1 that is reachable via the enp1s0 interface.

Prerequisites

  • The NetworkManager package is not installed, or the NetworkManager service is disabled.
  • The network-scripts package is installed.

Procedure

  1. Set the GATEWAY parameter in the /etc/sysconfig/network-scripts/ifcfg-enp1s0 file to 192.0.2.1:

    GATEWAY=192.0.2.1
  2. Add the default entry in the /etc/sysconfig/network-scripts/route-enp0s1 file:

    default via 192.0.2.1
  3. Restart the network:

    # systemctl restart network