How can I update a Network Manager connection from the command line?

I have a bunch of Ubuntu machines on my work ethernet LAN and I need to modify the default gateway.

I'd rather not traipse round, interrupt people's work, to right click an applet, preferring instead to ssh in, which can be automated :-)

I know I can change the default live gateway with ip route replace default via 1.2.3.4 but this does not update NM's stored connection.

In my case each machine will only have one stored wired connection.

How can I update the stored gateway IP from the command line?


Solution 1:

All configuration from Network Manager are in /etc/NetworkManager/system-connections/

On this please you will find Wired connection 1 witch is probably configuration file of you lan connection. You can edit it

sudo nano /etc/NetworkManager/system-connections/Wired connection 1

[802-3-ethernet]
duplex=full
mac-address=00:22:64:4E:6F:70

[connection]
id=Wired connection 1
uuid=57a2a340-c113-406f-9abc-eb816e58b3db
type=802-3-ethernet
timestamp=1430138708

[ipv6]
method=auto

[ipv4]
method=manual
dns=xxx.xxx.xxx.243;8.8.8.8;
dns-search=eunet.rs;
address1=xxx.xxx.47.5/24,xxx.xxx.47.254

You can change ip, network mask or gateway. After change you made, save and restart NM with command

sudo service network-manager restart

Solution 2:

I accepted 2707974's answer (great name) but here for anyone wanting to automate this stuff with a one-liner to change from 1.2.3.4 to 5.6.7.8

sudo find /etc/NetworkManager/system-connections/ -type f -name "Wired*" \
 | while read c ; do sudo sed -i 's/1\.2\.3\.4/5.6.7.8/g' "$c" ; done ;  \
 sudo service network-manager restart

Of course this assumes that 1.2.3.4 is only found in the files in relation to the appropriate thing - so for me it was the default gateway and so the IP address only occurred once.