Ask Your Question

Default route magically disappears

asked 2018-01-12 10:17:28 -0500

mfrydl gravatar image

I'm using Network Manager to manage my connections. I have wired connection automatically configured by DHCP (IPv4). Among other things it provides also default gateway.

I need to replace the gateway with another one in certain cases. So I created the plugin in /etc/NetworkManager/dispatcher.d to react on "up" event on the device, delete the default route and add new one instead. This worked as expected on Fedora 17. But now when I tried to use the same trick on Fedora 27, it does not work. The script successfully changes the routing table (I can see that by printing it to file during script execution) but then "something" deletes the default route. It does not put back the original one but just deletes it.

I'm not able to set it back from command line either. Or at least not for long. When I set it, it's gone again within several seconds. What I've found is that Network Manager dispatcher seems to be run again (and the scripts) with "connectivity-change" event. In fact the scripts are called twice. In both cases the routing table is already without default route.

What is interfering with these changes? How can I change the default route without putting static one to network configuration. I don't want that since the script can detect when such change is needed and when not.

edit retag flag offensive close merge delete


According to your writing, am I correct in thinking that your script always establishes your non-DHCP-provided default gateway when you bring up a certain ("the") device? If that's the case, can't you just set the default gateway for that device to be the one you need? If I'm reading this wrong, more details about your configuration and desired solution would help. It very well may be done easily with standard Network Manager configuration options, no custom scripting required.

bitwiseoperator gravatar imagebitwiseoperator ( 2018-01-12 20:23:17 -0500 )edit

What I'm doing is to avoid setting the company proxy server everywhere and remove it each time I disconnect from company network since that is quite complicated in Linux. I have my own transparent proxy server installed on some other machine and what I do is to check if that server exists (i.e. I'm at work) and if so, create route for internal addresses to provided gateway and then set the default gateway to my transparent proxy server. This way I don't have to do anything, either inside or outside company since the script will automatically decide.

mfrydl gravatar imagemfrydl ( 2018-01-13 11:24:21 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-01-13 13:40:12 -0500

updated 2018-01-14 04:59:27 -0500

If I understand your latest comment correctly:

You can use NetworkManager to create a connection specific to work (I imagine your access point or some variable will be readily distinguishable once you're there). It is likely, in fact, that your connection already exists, since NetworkManager makes new connections for new SSIDs (for example). Execute nmcli c to show what you've got.

In the connection you use for work, you may define a default gateway appropriate to work. Just use nmcli c edit <connectionName>. The syntax for nmcli is a little wonky, so look it up, but you'll probably only be interested in modifying the ipv4.gateway value to whatever it needs to be.

For the rest of your connections, the default gateway will then be set by DHCP (or, if you have a static IP address elsewhere, you may define a separate set of configuration values appropriate to that connection).

Edits In Response to Comments

Since you're using two wired connections, you may well need to rely on your script. The proper place to put your script with the new NetworkManager is in /etc/NetworkManager/dispatcher.d; from there, it will be executed every time a connection is brought up or down. As long as your script modifies the default gateway using an nmcli command (such as nmcli con mod em1-1 ipv4.gateway, the value it establishes won't be overwritten by NetworkManager.

edit flag offensive delete link more


I'm not sure if I understand. Does this mean to have two connections defined in Network Manager - one for work with static gateway and another one "rest" with DHCP gateway? And then just select the right one manually? I have both wired so they cannot be automatically selected by Network Manager.

This would work, but I'm missing the "automagic" behavior where I don't have to do anything. I just want to understand why anything working in Fedora 17 does not work in 27.

mfrydl gravatar imagemfrydl ( 2018-01-13 14:09:53 -0500 )edit

Setting ipv4.gateway does not work as it says "gateway cannot be set if there are no addresses configured". I've search for this and it looks like I have to switch to "manual" and set everything manually (which is not nice).

However, it seems like when I set ipv4.never-default=yes (do not set default gateway from DHCP, in nm-applet "Use this connection only for resources on this network"), it still gets it from DHCP server in DHCP4_ROUTERS variable. Now I can modify default route without nm interference. I just have to set it always.

I still need to verify this but looks promising.

mfrydl gravatar imagemfrydl ( 2018-01-15 16:33:55 -0500 )edit

answered 2018-01-12 15:12:57 -0500

toddandmargo gravatar image

Hi mfrydl,

You should fix your DHCP server to give out the correct default router!

But if there is some other reason, try this:

1) do a "ifconfig" to find out the name of your network card. It will be something like "enp6s0" or "eno2".

2) as root, create and/or edit /etc/sysconfig/network and add your gateway and gatewaydev. Mine looks like:


change the IP and the device to your system's default router and name you found with the ifconfig command.

3) restart your interface:

ifdown name; ifup name

sub name for the name you found with ifconfig

You may have to reboot, but I don't think so.


edit flag offensive delete link more


This is almost the same as putting gateway to my network configuration in network manager which I'm trying to avoid. I need to sometimes use this special configuration and sometimes the default one. The script can decide automatically without my interaction.

mfrydl gravatar imagemfrydl ( 2018-01-12 16:57:51 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-01-12 10:17:28 -0500

Seen: 327 times

Last updated: Jan 14