How to choose between multiple network connections through command line?

Posted on

Problem :

I have two network connections – wifi and ethernet. How to choose a specific network to use while using command line eg. ping

PS: I mentioned ping as an example. I may be running a python script or curl and it should use same interface for all these.

Solution :

It may not be possible to do this without disabling one of the connections, unless you have a special setup.

Generally speaking Linux routes are controlled by the route table, and traffic will be sent out the route with the lowest metric – you can see what this is by using the command “ip route” or “route”. In this cas the easiest solution would be to remove the route you don’t want (using ip route or route) or tear down the interface you don’t want (using ifconfig or ip addr).

If you have more complex routing, its possible your system is using source routing, and then routes packets based on the source or target IP through different tables. In this case the easiest way to do it would be to tell the command (if supported) to send packets with the given source IP address or interface (use man COMMAND to see how this would work). In ping it would be ping -I INTERFACE, in mtr its mtr -a SOURCE.IP.ADDRESS

Leave a Reply

Your email address will not be published. Required fields are marked *