12

I'm using CentOS 7 and I setup a new connection with nmcli. I set the gateway but it doesn't seem it took. When I check the connection with

nmcli con show conn-name

the gateway is listed as: gw = 0.0.0.0

I can't find how you add/modify the gateway with nmcli.

Nothing is listed in nmcli connection modify.

Can you edit/add the gateway address with nmcli after the connection has been added?

As a work around, I edited /etc/sysconfig/network-scripts/ifcfg-connname and added GATEWAY0=addr

I'm assuming you can't update the gateway address from nmcli?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Oscalation
  • 1,109
  • 4
  • 15
  • 27

4 Answers4

8

For older implementations (such as CentOS 7) you can use:

nmcli con modify <name> ipv4.addresses "<ip addr and mask> <gateway>"

to add a gateway to the connection. For example:

# nmcli con modify eth0 ipv4.addresses "192.168.1.10/24 192.168.1.1"

then confirm with:

# nmcli con show eth0 | grep ipv4\.addresses
ipv4.addresses         { ip = 192.168.1.10/24, gw = 192.168.1.1 }

Later implementations have ipv4.gateway which you can modify:

nmcli con modify <name> ipv4.gateway <gateway>

For example:

# nmcli con modify eth0 ipv4.gateway 192.168.1.1
garethTheRed
  • 33,289
  • 4
  • 92
  • 101
5

If you want to modify/add gateway on interface eth0, you can use the following command:

nmcli con modify eth0 ipv4.gateway "192.168.1.1"
nmcli con reload eth0
AdminBee
  • 21,637
  • 21
  • 47
  • 71
NamNT
  • 51
  • 1
  • 1
3

To create a connection with the name ethernet-eth0, the IPv4 address 192.168.1.10/24 and the default gateway 192.168.1.1, type:

nmcli con add con-name net-eth0 ifname eth0 type ethernet ip4 192.168.1.10/24 gw4 192.168.1.1

If you don’t specify con-name net-eth0, the connection is called ethernet-eth0.

If you don’t specify the ip4 192.168.1.10/24 gw4 192.168.1.1 part, you end up with a connection automatically configured through DHCP.

nmcli con up net-eth0 is not necessary when initially configuring a connection.

GAD3R
  • 63,407
  • 31
  • 131
  • 192
  • Just to make sure I understand, after the connection is created with nmcli, and if you forgot to define the gateway when creating it, you cannot add the gateway using the nmcli command. Correct? To add it you would have to modify the connection in /sysconfig/network-scripts/ ... etc – Oscalation Feb 25 '16 at 15:43
-1

Answering the above question about modifying an existing connection:

nmcli con mod con-name net-eth0 ipv4.gw4 192.168.1.1
nmcli con down net-eth0; nmcli con up net-eth0
FlakRat
  • 19
  • 3
  • It would appear that your answer is essentially the same as the [answer by NamNT](https://unix.stackexchange.com/a/324220/377345). If that was not intended, please consider expanding it to make the difference more obvious. Otherwise, it should probably be a comment to that answer. – AdminBee Jun 13 '23 at 13:16