How to temporarily change the gateway of a secondary IP?

2

2

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!

Emanuele

Posted 2015-12-28T13:53:19.013

Reputation: 661

That can be made with asymmetric routing, but not as a one-liner as far as I know. More info on this: http://www.microhowto.info/howto/ensure_symmetric_routing_on_a_server_with_multiple_default_gateways.html

– nKn – 2015-12-28T15:28:24.650

1

No, you cannot: re-read my answer here, http://superuser.com/questions/1018196/how-can-i-tell-if-my-multiple-network-interfaces-are-working-ok

– MariusMatutiae – 2015-12-28T15:34:58.767

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? – David Schwartz – 2015-12-28T15:44:28.567

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. – Frank Thomas – 2015-12-28T15:49:18.300

@MariusMatutiae I'll try the below first answer; I hope you're wrong, but you migth be just right :) – Emanuele – 2015-12-28T17:49:59.440

@DavidSchwartz Yes – Emanuele – 2016-01-01T13:44:46.903

If you want to select routes based on more than the destination address, that’s called Policy Routing. I don’t have any experience with it, though. – Daniel B – 2016-01-01T16:47:51.277

Answers

1

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.

MariusMatutiae

Posted 2015-12-28T13:53:19.013

Reputation: 41 321