Linux: How to Force a Specific Network Interface to Be Used

Linux: How do I force a specific network interface to be used?

After a hard-fought weekend, I'm pleased to present a solution that addresses most of what I've previously discussed with almost zero hassle.

There is a sysctl called net.ipv4.conf.all.rp_filter that can be set to 0 to disable source validation:


rp_filter - INTEGER
2 - do source validation by reversed path, as specified in RFC1812
Recommended option for single homed hosts and stub network
routers. Could cause troubles for complicated (not loop free)
networks running a slow unreliable protocol (sort of RIP),
or using static routes.

1 - (DEFAULT) Weaker form of RP filtering: drop all the packets
that look as sourced at a directly connected interface, but
were input from another interface.

0 - No source validation.

This can also be set on a per interface basis using /proc/sys/net/ipv4/conf/<interface>/rp_filter.

As one poster explained it, it makes IP routing "less deterministic" in the sense that packets coming from one subnet aren't guaranteed to always go out the same interface. In this instance, this is exactly what it is needed. Please do additional research to determine if this is really what you want.

Broadcasts are still problematic for reasons I do not understand, but I am finally satisfied with this issue and I hope it helps others.

Force Window command line use specific Network Adapter

Since this is not supported by Git itself, as commented, you would need to use the netsh command in order to disable/enable a network adapter from command line

You can see one example in this question or in this script:

@netsh interface set interface name="LAN1" admin=disabled
@netsh interface set interface name="LAN0" admin=Enabled

The idea for this script would be to detect which one is enabled and disabling it, enabling the other: the script would act as a switch and could be called as part of a git alias.

How to bind gpsd to some specific network interface?

A quick and dirty way would be to start gpsd with the -G option, to enable monitoring all interfaces (by default, gpsd will only listen to localhost for security and privacy) and then disable access on unwanted interfaces by having proper firewall rules (disable access on two unneeded interfaces).
Gpsd will listen on port 2947, you can change that with the -S option to fit your needs.

Linux/CentOS: How to force FTP/SSH to use a particular ethernet adapter

Perhaps you can set up a special route for the servers you intend to connect to? Have a look at the route command, or the "ip route" command.

EDIT: This seems to contain an example of what I'm saying: http://www.cyberciti.biz/faq/howto-linux-configuring-default-route-with-ipcommand/

Specifically:

Type the following command to sent all packets to the local enter
code herenetwork 192.168.1.0 directly through the device eth0:,
enter

ip route add 192.168.1.0/24 dev eth0

Is it possible to choose specific network interface to transmit data in Python?

If we're talking about tcp/udp, then (like in any other langauge) the socket interface allows you to bind a specific ip address to it, so you do that by binding the interface's address.

People have the misconception that binding is only for listening on a socket, but this is also true for connecting, and its just that in normal usage the binding is chosen for you.
Try this:

import socket
s = socket.socket()
s.bind(('192.168.1.111', 0))
s.connect(('www.google.com', 80))

Here we use the ip from interface eth0 (mine is 192.168.1.111) with a system-chosen source port to connect to google's web server. (You can also choose the source port if you want by replacing the 0)

EDIT:
To get the IP address that is used for a specific interface you can use this recipe (linux only) - http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/

(if you have multiple IPs on the same interface I'm not sure that it'll work. I assume it will return one of them)



Related Topics



Leave a reply



Submit