Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am currently trying to configure a router to which I connect to (to its web interface) using an Ethernet cable through the eth0 interface. That router doesn't have access to the internet.

Now, I have access to another network (different IP range and all) that does have access to the internet through my wireless interface wlan0.

I would like to use eth0 to only connect to the router I am trying to configure and wlan0 for the rest of the connections. That way, I could do some research while trying to configure the router.

I'm sure it's doable, with some kind of routing but I haven't been able to find anything that works. The issue is that when I plug the Ethernet cable, the system starts using that eth connection for everything.

To sum up a bit:

Assuming the router's address is 10.0.0.1, can I do something to route traffic going to 10.0.0.1 through the eth0 interface and anything else through wlan0?

I am using Debian Squeeze.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

There are a few possible options here.

1

Don't use DHCP for configuring eth0. Use a static configuration instead, and don't set a gateway. DHCP is setting a default gateway of the router so your machine uses it for all traffic. Without the gateway being set, it will only use the interface to talk to hosts on that network

2

Tell the router not to advertise itself as a gateway. This wont be possible on most routers, so this may not be an option at all.

3

It's possible debian might be able to ignore the gateway setting that the router advertises via DHCP, but I don't use debian, so I'm not sure. This is dependent upon what debian uses to configure network devices.

4

Also debian specific, you might be able to run a script after the interface is brought up and have it delete the gateway route that is added by DHCP.

share|improve this answer
    
Debian lets you configure network devices several ways; you can use ifup (traditional Debian way), Network Manager, etc. OP didn't say which he's using. –  derobert Mar 28 '13 at 17:45

The easiest way would be something like this:

  1. Disable DHCP on eth0, or down the interface (disconnect it in Network Manager, or use ifdown eth0, etc.)
  2. ip -4 addr add 10.0.0.2/30 dev eth0
  3. ip link set eth0 up

Alternatively, if you prefer the older ifconfig, I believe that'd be ifconfig eth0 10.0.0.2 netmask 255.255.255.252 up

Final alternative, if you're using Network Manager (probably the case if you configure your networking using a GUI), set up a profile for eth0 with a static IP address of 10.0.0.2 and a netmask of 255.255.255.252. Leave the default route and DNS servers blank.

After doing any of these, you should be able to ping 10.0.0.1 and also still reach the rest of the Internet.

Why does this work?

(This is a simplification, routing on Linux actually has a lot of optional complexity.) When the kernel wants to send an IP packet, it consults the routing table to determine which interface it sends the packet out of.

The routing table will generally contain a route for the local subnet on each interface. The above commands all put the subnet 10.0.0.0/30 on eth0. A /30 is 4 IP addresses: 10.0.0.0–10.0.0.3 in this case. That conveniently includes your router (.1) and your box (.2). The first (.0 = "network") and last (.3 = "broadcast") addresses in most subnets have a special reserved meaning, so we can't use them.

In addition to the routes for the local subnets, there will be any static routes you've configured, and any routes that come from routing protocols (which doesn't apply to you, or 99% of people).

Finally, there will be a "default" route, which says where to send the rest of the traffic. This route isn't actually special, it's a route for 0.0.0.0/0—that is, the entire IPv4 address space. It is default because of how routing lookups work...

By default, it checks in this order:

  1. Is it a local IP address (that is, an IP address of this machine)? If so, route it to myself.
  2. Next, sort all the routes from most-specific (subnet with the fewest IP addresses) to least-specific (subnet with the most IP addresses).
  3. Go down that sorted list, starting from the top, until something matches.
  4. If nothing matches, give up and generate unreachable error.

So your 0.0.0.0/0 route is default because it gets sorted to the bottom of the list. And your 10.0.0.0/30 route will be pretty high on the list, if not the very top. Basically it functions as an exception—"send the traffic to the Internet, unless it's for 10.0.0.0/30"

What are these weird /30 things?

/X, sometimes called "CIDR notation", is a quick way to specify netmasks. It counts the number of bits set in the metmask. So a netmask of 255.255.255.255 (single IP) is /32, because all bits are set. …254 (two IPs) is /31, …252 is /30, all the way to 0.0.0.0 (all IPv4 addresses) is /0. The common 255.255.255.0 is /24.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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