Q:

How to toggle NetworkManager's "Use this connection only for resources on its network" option via command line

At my work we have some test machines that we run scripts on via ssh. During the test we want to only have the machines access the local network through their Ethernet connection, but then would like to have them be able to access the Internet through the same interface for cleanup and data transmission after. 
We can do this by going to each machine and toggling the "Use this connection only for resources on its network" option in the network manager > IPv4 options > routes menu, but it would be a lot more convenient to be able to add this into our test script.
I can find some similar questions that have to do with VPN connections, but I want to make sure I handle the routing correctly for the regular Ethernet interface. Thanks for any help.

A:

You can use nmcli to do it, the key is ipv4.never-default:
nmcli connection modify enp3s0 ipv4.never-default true

change enp3s0 with your own connection name.
use false or true to toggle between this config.

A:

If you'd rather edit files directly, you can do that as well.
Navigate to /etc/NetworkManager/system-connections/ directory and find the connection you want. Open the network using sudo nano MyNetwork.
Once you have the file open, look for the [ipv4] section, and add never-default=true, like so:
[ipv4]
dns-search=
method=auto
never-default=true

If you also want IPv6 routes to be handled in the same way, add the same line to the [ipv6] section:
[ipv6]
addr-gen-mode=stable-privacy
dns-search=
ip6-privacy=0
method=auto
never-default=true

Save and quit the editor with Ctrl+X and then restart NetworkManager with the below to ensure that the changes are picked up:
sudo systemctl restart NetworkManager.service

Reconnect to the network, and it should work just fine.

