How to toggle NetworkManager's "Use this connection only for resources on its network" option via command line
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
ortrue
to toggle between this config.
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.
You can remove the default route and they'll be accessible only from your lan subnet.
ip route delete default
And to add it back:
ip route add default via <you_gateway_ip_address>