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?
3 Answers
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;
-
1Scriptable 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
– colanJan 10, 2017 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?– JohnyTexSep 27, 2018 at 9:19
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
-
17What if the desired interface is configured via DHCP and I don't know the gateway address it will assign? Aug 31, 2012 at 10:40
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.
/etc/network/interfaces
file, or a with software like Network Manager or wicd?