Ask Your Question

Default route magically disappears

asked 2018-01-12 16:17:28 +0000

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 ( 2018-01-13 02:23:17 +0000 )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 ( 2018-01-13 17:24:21 +0000 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2018-01-13 19:40:12 +0000

updated 2018-01-14 10:59:27 +0000

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 ( 2018-01-13 20:09:53 +0000 )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 ( 2018-01-15 22:33:55 +0000 )edit

answered 2018-01-12 21:12:57 +0000

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 ( 2018-01-12 22:57:51 +0000 )edit

Your Answer

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

Add Answer

[hide preview]

Use your votes!

  • Use the 30 daily voting points that you get!
  • Up-vote well framed questions that provide enough information to enable people provide answers.
  • Thank your helpers by up-voting their comments and answers. If a question you asked has been answered, accept the best answer by clicking on the checkbox on the left side of the answer.
  • Down-voting might cost you karma, but you should consider doing so for incorrect or clearly detrimental questions and answers.


Asked: 2018-01-12 16:17:28 +0000

Seen: 158 times

Last updated: Jan 14