Proper way to set gateway in /etc/network/interfaces file.

davenmccalla

New Member
Joined
Apr 9, 2025
Messages
5
Reaction score
1
Credits
57
I have a device with one wired interface (eth0) and one wireless interface (wlan0).

The wired interface is configured for static IP address:
iface eth0 inet static
address 172.16.11.200
netmask 255.255.252.0
post-up route add default gw 172.16.8.91 dev eth0
pre-down route del default gw 172.16.8.91 dev eth0
The wireless interface is configured for DHCP:
auto wlan0
iface wlan0 inet dhcp
wpa-conf /home/inkjet/cfg/wpa0.cfg
When I power up the device with the cable unplugged from the wired interface, the wireless can't communicate over the network. It get an IP address from DHCP server but I can't even ping the gateway.
wlan0 Link encap:Ethernet HWaddr 08:BE:AC:2E:06:9E
inet addr:172.16.9.153 Bcast:172.16.11.255 Mask:255.255.252.0
inet6 addr: fe80::abe:acff:fe2e:69e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23347 errors:0 dropped:1 overruns:0 frame:0
TX packets:7284 errors:0 dropped:2 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4721335 (4.5 MiB) TX bytes:2738477 (2.6 MiB)
eth0 Link encap:Ethernet HWaddr 00:06:B3:02:00:92
inet addr:172.16.11.200 Bcast:172.16.11.255 Mask:255.255.252.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:182
The wireless interface starts working when I remove the gateway from the (unplugged) wired interface:
iface eth0 inet static
address 172.16.11.200
netmask 255.255.252.0
It there a way to define a gateway for the wired interface that doesn't prevent the wireless interface from working?
 


It will probably be useful to know for those replying what distribution and version you are running, as different distributions will use different ways to manage the network.
 

 
I don't have network-manager installed. This is an embedded system designed several years ago. It's running Debian 8.5 with 4.4 kernel. I'm in the process of migrating to Debian 11.8 with 5.10 kernel. I wasn't planning on changing the tools that control the network subsystem, but I may have to in order to get support from the community. I guess no one uses, or remembers how the old tools work.
 
Migrating is a tricky word, If you're really going to move from 8.5 to 11.8 that's a pretty significant upgrade.
For now, it's looks like you're trying to mix some systemd-network with some legacy commands.
You need to go one way or the other. When you move to 11, legacy networking isn't really a viable option anymore.
Then you are forced to use either systemd-network or networkManager. Since it looks like you're managing multiple
interfaces, I would personally recommend networkManager. It's a little heavier. (You don't have to install the GUI, all
you need is nmcli). That doesn't help you in the meantime.

It sounds like your routing is all going out one interface. What I would do is set metrics on the gateways.

/etc/network/interfaces

(eth0)
Code:
iface eth0 inet static
    address 172.16.11.200
    netmask 255.255.252.0
    gateway 172.16.8.91
    metric 100

(wlan0)
Code:
auto wlan0
iface wlan0 inet dhcp
    wpa-conf /home/inkjet/cfg/wpa0.cfg
    metric 10
 
Last edited:
From what I've been able to learn, the wireless interface is likely getting a default gateway from DHCP server. Then the wired interface creates a static route, which somehow prevents the communication over wireless. I added the static route because setting the "gateway" parameter for static IP interface didn't work, possibly because the wireless interface already was given a default gateway from DHCP server. I don't know.
Migrating is a tricky word, If you're really going to move from 8.5 to 11.8 that's a pretty significant upgrade.
For now, it's looks like you're trying to mix some systemd-network with some legacy commands.
You need to go one way or the other. When you move to 11, legacy networking isn't really a viable option anymore.
Then you are forced to use either systemd-network or networkManager. Since it looks like you're managing multiple
interfaces, I would personally recommend networkMamanger. It's a little heavier. (You don't have to install the GUI, all
you need is nmcli). That doesn't help you in the meantime.

It sounds like your routing is all going out one interface. What I would do is set metrics on the gateways.

/etc/network/interfaces

(eth0)
Code:
iface eth0 inet static
    address 172.16.11.200
    netmask 255.255.252.0
    gateway 172.16.8.91
    metric 100

(wlan0)
Code:
auto wlan0
iface wlan0 inet dhcp
    wpa-conf /home/inkjet/cfg/wpa0.cfg
    metric 10
Thanks for the suggestion. I tried it, and it still doesn't want to use wlan0 when eth0 has a gateway. If I take eth0 down using ifdown, wlan0 starts working immediately.
root@BBG-inkjet:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.8.91 0.0.0.0 UG 10 0 0 wlan0
0.0.0.0 172.16.8.91 0.0.0.0 UG 100 0 0 eth0
10.2.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
10.3.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlan2
10.4.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlan1
172.16.8.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0
172.16.8.0 0.0.0.0 255.255.252.0 U 0 0 0 wlan0
There really isn't a use case for a customer to use both the wired and wireless interfaces at the same time. But what happens is they switch from using the wired interface to the using wireless without unconfiguring the wired. When they call the help desk, we tell them to remove the gateway from the wired settings.

If I can't figure out how to properly configure the interfaces so they work at the same time, I'll have to change the user interface so they can select which one the want to use and blank out the other interface in the /etc/network/interfaces file.
 
This works:
auto wlan0
iface wlan0 inet static
address 172.16.11.65
netmask 255.255.252.0
gateway 172.16.8.91
metric 10
wpa-conf /home/inkjet/cfg/wpa0.cfg

auto eth0
iface eth0 inet static
address 172.16.11.135
netmask 255.255.252.0
gateway 172.16.8.91
metric 100
This also works (keep in mind eth0 cable is unplugged, so it never gets IP or gateway from DHCP server):
auto wlan0
iface wlan0 inet static
address 172.16.11.65
netmask 255.255.252.0
gateway 172.16.8.91
metric 10
wpa-conf /home/inkjet/cfg/wpa0.cfg

auto eth0
iface eth0 inet dhcp
metric 100
This also works (keep in mind eth0 cable is unplugged, so it never gets IP or gateway from DHCP server):
auto wlan0
iface wlan0 inet dhcp
wpa-conf /home/inkjet/cfg/wpa0.cfg
metric 10

auto eth0
iface eth0 inet dhcp
metric 100
but this does NOT work, or at least not reliably. I may have seen it work once, but can't repeat it:
auto wlan0
iface wlan0 inet dhcp
wpa-conf /home/inkjet/cfg/wpa0.cfg
metric 10

auto eth0
iface eth0 inet static
address 172.16.11.135
netmask 255.255.252.0
gateway 172.16.8.91
metric 100
 
What I'm expecting it to do may not make sense. When I ping the gateway, I expect it to use the wireless interface because the interface is up and on the same subnet as the gateway address. However, the wired interface is also up, and also configured to be on the same subnet as the gateway, although the link is not up because the cable is unplugged. It must be iterating over the interfaces and sending the packet out the first interface that's on the same subnet as the gateway, regardless of whether the link is up or not. I don't know what determines the order of interfaces it iterates over.

I can ping the gateway over wireless, but I have to specify the interface in order to get a response.
ping -I wlan0 172.16.8.91
Otherwise, I think it goes to eth0 whose link is down.

Can anyone confirm that my understanding is correct?
 
I don't know what determines the order of interfaces it iterates over.

The metric level. Lower metric gets accessed first. By default they are the same metric. You have to make one side higher or lower. You probably don't have traceroute on your printer, but you can test this on any Linux computer with two interfaces using traceroute. It will tell you which interface it's trying to use.
 
Last edited:


Follow Linux.org

Members online


Top