3

I have the following situation:

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.129  0.0.0.0         UG    0      0        0 usb0
192.168.0.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan1
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 usb0

And I would want to use a one-liner to setup the gateway of 192.168.0.0/24 to be 192.168.0.1 (current secondary ip is 192.168.0.4).
Can I use something like

sudo ip route add from 192.168.0.4 via 192.168.0.1 dev wlan1
RTNETLINK answers: File exists

?
Again, I need something temporary which forces all packets sent from 192.168.0.4 to pass from gateway 192.168.0.1 (don't want to setup a set of scripts/whatnot to do this). Is there a one liner I can use?

Cheers!

7
  • That can be made with asymmetric routing, but not as a one-liner as far as I know. More info on this: microhowto.info/howto/…
    – nKn
    Commented Dec 28, 2015 at 15:28
  • 1
    No, you cannot: re-read my answer here, superuser.com/questions/1018196/… Commented Dec 28, 2015 at 15:34
  • What do you mean by "sent from 192.168.0.4"? Do you mean having a source IP address of exactly 192.168.0.4? Commented Dec 28, 2015 at 15:44
  • Gateways are per route, and Routes are per destination (Not Source) so you cannot set a gateway for a secondary IP; you can only set the gateway on the 192.168.0.0/24 to 192.168.0.1. In that case all traffic destined for the 192.168.0.0 net will go through 192.168.0.1, but it will do so regardless of whether the traffic is sourced from 192.168.0.4 or another ip on that LAN. Commented Dec 28, 2015 at 15:49
  • @MariusMatutiae I'll try the below first answer; I hope you're wrong, but you migth be just right :)
    – Emanuele
    Commented Dec 28, 2015 at 17:49

1 Answer 1

2

The way to force all of your traffic thru wlan1 instead instead of usb0 is to force a new gateway: as sudo,

ip route del default
ip route add default via 192.168.0.1 dev wlan1

(I assume your gateway has IP address 192.168.0.1, if not change accordingly). You can restore the previous situation by means of

ip route del default 
ip route add default via 192.168.42.129 dev usb0

If you do not know/remember the IP address of the gateway, then use instead

ip route del default
dhclient -v usb0

(-v option is for verbose, it does not exist on all distros, you may have to drop it).

You may also decide that you would like to have two gateways, one per interface; with the Linux kernel (and only with the Linux kernel) this can be done, you find in David'Schwartz's answer an excellent short explanation of how to do it. Once you do, you will have to decide thru which interface the output of each application passes (you can decide on a per-application basis). This means that a given application, say ssh may bind to either interface, or, in other words, that the IP address from which ssh starts can be either that of usb0 or that of wlan1; the rule discussed by David Schwartz then takes care automatically of routing correctly ssh.

Notice that this way, you may have one ssh connection go thru wlan1, and another one going thru wlan1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .