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!
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.6501
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.767What 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