31

I am using 3 ethernet interfaces on Ubuntu but when I restart, the default gateway is changing to a different interface. Each time I boot I have to change default gateway back to the eth1 by deleting the default gateway and adding it back with eth1. How can I fix this problem?

4
  • Have you configured it manually using the /etc/network/interfaces file, or a with software like Network Manager or wicd?
    – Lekensteyn
    Mar 23 '11 at 12:56
  • 1
    "Gateway" is not equal to "route". A gateway is specific to an interface where an route says which interface should be used.
    – Lekensteyn
    Jun 2 '11 at 19:24
  • The gateway is usually an interface on a remote device which the local node is relying on for routing decisions. A route statement itself can refer to a local interface or not, but the routing destination has to be discoverable from a recursive lookup, connected interface, or default route. A default route or default gateway is the 'gateway of last resort' -- which will be used in the absence of a more specific route being available.
    – belacqua
    Oct 29 '11 at 22:14
  • Can you provide your routing table and list of interface, and other any relevant network configuration you've done?
    – belacqua
    Oct 29 '11 at 22:17
40

To temporarily change the default route you can use an ip command like this:

$ sudo ip route change default via 192.168.1.1 dev eth0

If you have a static ip configuration in /etc/network/interfaces you can add a gateway statement to make this permanent.

iface eth0 inet static
    gateway 192.168.1.1
    […]

Using DHCP to configure networking you have to adjust another file instead. Add the supersede statement in /etc/dhcp/dhclient.conf.

supersede routers 192.168.1.1;
2
  • 1
    Scriptable version (should be a space between the quotation marks): echo supersede routers $(ip route | grep default | cut -f3 -d" ")\; | sudo tee --append /etc/dhcp/dhclient.conf
    – colan
    Jan 10 '17 at 22:15
  • What if I have multiple interfaces? Shouldn't they all have a 'gateway' for each interface? But what affects the example ''default via 192.168.3.1 dev eth6" output of ip route command? It lists a specific interface, right?
    – JohnyTex
    Sep 27 '18 at 9:19
8

Open the file /etc/network/interfaces

find desired interface and add following:

gateway 192.x.x.x

Restart network:

sudo /etc/init.d/networking restart
1
  • 16
    What if the desired interface is configured via DHCP and I don't know the gateway address it will assign? Aug 31 '12 at 10:40
1

I had the same issue for my wifi interface wlp3s0 on Ubuntu 18.04. It was affecting my ability to connect to Android tethering and router outside home. The way to solve the issue for me was to comment in file /etc/dhcpcd.conf the following:

interface wlp3s0
        static ip_address=192.168.0.16/24
        static routers=192.168.0.1
        static domain_name_servers=127.0.0.1

to get the following

#interface wlp3s0
#        static ip_address=192.168.0.16/24
#        static routers=192.168.0.1
#        static domain_name_servers=127.0.0.1

Then, I've disconnected from my current network and reconnected an tried route -n or nmcli to see changes

You can also remove the same lines when you are sure it works for you.

Because of these lines, the NetworkManager was always adding a default route with a wrong gateway, in particular when I was not on my home network. I was getting "Destination Host Unreachable" when I was doing a ping 8.8.8.8. Hardcoding a gateway in /etc/network/interfaces was not a viable solution as I connect a lot to public wifis (so, never the same wifi router or gateway) and normally DHCP should work "out of the box" IMHO.

PS: I've never edited manually /etc/dhcpcd.conf so I don't know why the file /etc/dhcpcd.conf did contain these lines.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.