Automatically set default gateway when switching to wlan

I am using ifconfig on my openSUSE 11.4 installation. I have problems automatically
setting the default gateway when connecting through the wireless device. I use Yast to
setup the wireless connection and ‘ifconfig’ shows that I have an IP address on wlan0.
However, ‘route -n’ tells me that the communication is still going through the default
gateway that was last assigned to the ethernet card (even if the ethernet cable is
disconnected) and as a result I cannot connect. It also does not help that I manually
call ‘/etc/init.d/network restart’. I always need to “guess” the default gateway and then
force it manually. That is always an annoying process (in particular when the default
gateway is not obvious).

For example, right now I have (network cable is disconnected):

eth0 Link encap:Ethernet HWaddr E8:9A:8F:7B:17:0F
inet addr:128.208.19.133 Bcast:128.208.19.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:14477 errors:0 dropped:19 overruns:0 frame:0
TX packets:365 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1325858 (1.2 Mb) TX bytes:44321 (43.2 Kb)
Interrupt:43 Base address:0x8000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:224 errors:0 dropped:0 overruns:0 frame:0
TX packets:224 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:22286 (21.7 Kb) TX bytes:22286 (21.7 Kb)

wlan0 Link encap:Ethernet HWaddr EC:55:F9:C8:62:C8
inet addr:69.91.218.231 Bcast:69.91.218.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4455 errors:0 dropped:1 overruns:0 frame:0
TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:856318 (836.2 Kb) TX bytes:9845 (9.6 Kb)

I have an IP address through wlan0: 69.91.218.231, but route -n shows that my default gateway is still 128.208.19.100:

Destination Gateway Genmask Flags Metric Ref Use Iface
69.91.218.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
128.208.19.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 128.208.19.100 0.0.0.0 UG 0 0 0 eth0

If I manually force the gateway to be “69.91.218.100”, then I can connect through the wireless.

On older openSUSE installations (e.g., openSUSE 10.0) I was able to deactivate eth0
(e.g., through ‘ifconfig eth0 down’) which would then solve the routing problem.
However, in openSUSE 11.4 if I try to delete the eth0 configuration with Yast or
deactivate it with ‘ifconfig eth0 down’ I get:

eth0 serves root filesystem. Leave it up.

Is there any way how I can have the correct default gateway automatically set up? Or, how can I find out what
the default gateway is without having to guess it?

Thanks!

Please, please. When you post computer text as above, copy/paste this between CODE tags: Posting in Code Tags - A Guide To keep it readable.

When you use the ifup method, this means that all network configuration parameters are stored in /etc/sysconfig/network/ (and your default route is in /etc/sysconfig/network/routes). When the network is started, these configuration files are used. And as there can be only one default route in that file, this rout is used irrespective of which network interface is connected. In short, this is by design.

How do I connect to a wireless network then without having to guess the default gateway?

Also, the file /etc/sysconfig/network/routes is empty in my case.

The idea behind ifup is the old idea that you have a system conected through a fixed set of (often only one) network devices and that you carefully configure this. Think of computer room amd desktop systems.

Even with ifup you can use DHCP to be more dynamic. In that case let DHCP not only give you an IP address, but also the default route (and the DNS server). That will give you the correct default route that “belongs” to the router where you connect to.

A systems manager does not “guess” the default gateway. He gets that information from the network manager. And the network manager will tell him to use either a fixed gateway, or to get it from the DHCP server.

With nowadays walk around systems where there is no network manager available, connecting is made available to the end-user through Network Manager (you see the name!). It will give the end-user the possibility to choose between different connections, and will then configure (using DHCP). But you choose for *ifup *which may be the correct choice.

EDIT: Also mark that this has nothing to do with the fact that one of the devices is wired and the other WLAN. As long as the two devices go to a different network (LAN) and thus by definition have another router to act as the default router you have this.

ok, in my case /etc/sysconfig/network/routes is empty because I’m using dhcp to get an IP address.

dhcp should automatically set the default gateway when I switch to wireless, but it does not.

How can I solve this? Any help would be greatly appreciated.

Thanks!

This is what I’m trying to do: get it from the DHCP server. Also, the nameservers in /etc/resolv.conf are setup correctly but the default gw isn’t. In older SUSE installations I was able to delete the ethernet card configuration in Yast so that the communication would go only through the wireless, in which case the routing was set up correctly. Here, I can’t because apparently it serves the root filesystem. Can I change that? And what does it mean that it is serving the root filesystem?

With nowadays walk around systems where there is no network manager available, connecting is made available to the end-user through Network Manager (you see the name!).

I am aware of the existence of NetworkManager, but there should be a way to make it work with ifup (or at least I want to see how far I can get with ifup, since it used to work in older installation where eth0 did not server the root filesystem).

If that means that you manually run “ifconfig”, then I guess you should manually run “route”.

If you are using “ifup”, then configure your ethernet card to start on cable connect. (The default is for it to start at bootup).

Usually, I never have to manually run “route”. Running “/etc/init.d/network restart” takes care of setting nameservers and the default gateway automatically when using dhcp. The problem arises when I want to use a different device other than eth0.

If you are using “ifup”, then configure your ethernet card to start on cable connect. (The default is for it to start at bootup).

You are right, thanks! In my openSUSE 11.3 installation eth0 “activate device” was set to “At Boot Time”, whereas in openSUSE 11.4 it is set to “On NFSroot”. I guess I will switch that one. What is the difference, I mean what does “On NFSroot” do?

You are right, thanks! In my openSUSE 11.3 installation eth0 “activate device” was set to “At Boot Time”, whereas in openSUSE 11.4 it is set to “On NFSroot”. I guess I will switch that one. What is the difference, I mean what does “On NFSroot” do?

Did you do an a network install? IIRC, it can set like that at install time. (I stand to be corrected on this though…)

No, it was from the DVD. I first tried to do a network install, but I think that my network card was not correctly supported by the driver in the install CD, so I had to download and burn the DVD.

I’m not sure, but I think “On NFSroot” means just before trying to mount NFS file systems. Unless you are using NFS mounts, I would not use that one.

The network startup scripts seem to assign priority to the first network started. If you have “eth0” started on boot, then it will be first. If you have it start on cable connect, and there is no cable connected, then your wireless interface will be started first and the gateway assigned to it.

This is what I’m trying to do: get it from the DHCP server

You never told so.

I’m sorry! :shame: Does the DHCP client print in a file what the default gateway is? For example, the name servers are printed in /etc/resolv.conf. Is there a similar file where the default gateway gathered by the DHCP client is printed to?

hcvv, nrickert and deano_ferrari,

Thank you so much for participating in this thread :). The solution was actually quite simple. Once “activate device” is set to “At Boot Time”, all I have to do is:

/etc/init.d/network stop eth0
/etc/init.d/network start wlan0

Default gateway is now automatically set to the right value. (Switch the devices in the two lines to revert the process.)

Quite simple and elegant (if you think that in windows you would have to go through several submenus to force one interface) rotfl!

The name servers are indeed stored (not printed) in /etc/resolv.conf.

The routes are in a table in the kernel and not in a file. You can see your router table with

/sbin/route

or, to see IP addresses instead of names

/sbin/route -n

And yes, maybe I should have asked more about what you have, did, etc. before jumping to any answer. But sometimies I forget, somewhere getting the impression that I know what others do. That proves to be wrong in most cases. But the main fault allways lies with people that assume that others on the forums are clairvoyant. Sigh … :frowning:

I just wonder how a DHCP client works when it asks for an IP address from a DHCP server. Somehow the DHCP server must communicate, besides the IP address, name servers and default gateway in order for this information to be stored in resolv.conf and in the kernel, respectively. I’m curious whether it is possible to find out what the default gateways are when I have multiple interfaces setup with dynamic IPs without having to deactivate any of them. But this is more of a curiosity than a necessity and maybe I just need to pull a documentation about DHCP and read more about it.

And yes, maybe I should have asked more about what you have, did, etc. before jumping to any answer. But sometimes I forget, somewhere getting the impression that I know what others do. That proves to be wrong in most cases. But the main fault always lies with people that assume that others on the forums are clairvoyant. Sigh … :frowning:

You are right. This is why I tried to include as much information as possible in my first post, but forgot about an important detail. Meanwhile, I learned a few more things thanks to everybody here :).

The DHCP protocol is in fact still the old bootp protocol (well, in both cases the last P/p stands allready for protocol). Thus Wikipedia (or Google is your friend for all the details.

But in principle the bootp client contacts the bootp server** inside the same LAN** by doing an ethernet broadcast on that LAN. The server answers this and they establish an ethernet connection not based on any IP address but on the MAC addresses (like every connection inside the LAN is done once the IP ddresses are resolved to MAC addresses using the arp). The server sends the IP address for the client based on the MAC address of the client. Once the IP address is un use, communication can then be upgraded to a TCP/IP connection. The rest of the configuration is send (like the default router) and amongst these there can also be the IP address of a system where the client can download via tftp (trivial ftp) it’s operating system. Thus a complete bootup over the net is possible.

I am not quite sure, but reading your post I guess you have some misundderstanding here. When your system is connected to different LANs via different interfaces, your system will still have only one default router. That is what the expression means. The route to take when all other routes are not applicable.
In other words, the default route is something of the system, not of the interface.

When you did the* route -n*, you will have seen at least one other route. That is the route to your LAN. What it says in fact is: when the destination of a packet is one of he systems of this (sub)net, go through this interface.
When you have two interfaces to two LANs (let us call them LANa and LANb) there will be two of those. As they are needed and it does not require much intelligent guessing what they should look like, they are generated automaticaly.

But for packages going elsewhere you need a route because neither of those two routes fist. It could be that LANc can be reached through a router in LANa. Then you must add a route statement saying that addresses of LANc are to be reached via that IP address in LANa.

And in the end there is a router in LANb that connects to the Internet and that is where we go if we do not go to LANa, LANb or LANc. And that is the default router.

You can compare this (more or less) with a handpost with several signs pointing into directions. You read them from top to bottom (from the longest netmask to the shortest). If the masked part of your destination system’ IP address is the same as the that on the post, you go in that direction. The lowest post has the shortest netmask 0.0.0.0 (see your* route -n*) and thus when you ara still there, go to where the lowest/default sign points

Hope this helps.

I don’t think that I have any misunderstanding, but you have misunderstood my question. I am trying to figure out how the client gets the default gateway from the DHCP server.

The client sends a query to the DHCP server, asking for such information as gateway, DNS servers, domain name.

How it uses that presumably depends on the client software. I think it records the info in a temporary file, then use it to set “/etc/resolv.conf” and to add any routes needed. When the dhcp lease on the IP address approaches expiration, it attempts to renew the lease. If that fails, it appropriately updates “/etc/resolv.conf” and the routing table.

I’m unsure what you are trying to ask there. So I’m not sure if I answered it.

If you are using “dhcpcd” (the default for “ifup”), then look in “/var/lib/dhcpcd” for its data.
If you are using “dhclient” then look in “/var/lib/dhcp”