2

I have a Ubuntu (18.04) linux server with a main network interface eth0, which has 2 IP addresses assigned to it (via systemd-networkd). The ip route routing details are pretty simple, just send all outgoing stuff over IP1. However sometimes I need to make outgoing connections (i.e. ssh connections) that come out from the other IP address, IP2. I could just change the default route for this entire server, but is there a better way? Can I run magiccommand --use-ip=$IP2 ssh whatever

I have root on the machine. I tried firejail, but it errored with Error: the software is not supported for /31 networks ?!) I want to ssh into another server which only allows SSH connections from IP2, not IP1.

1
  • You mean you don't want all connections to that other server to use IP2, but only some of them (say, ssh via IP2 but http via IP1)? Because if you didn't mind all connections to $other_server using IP2, it would be trivial to just add a specific route (ip ro add $other_server/32 dev eth0 src $IP2).
    – TooTea
    Oct 22 at 20:12
4

Recent enough versions of ssh have the -B bind_interface and -b bind_address options.

Ubuntu 18.04's ssh might only have the -b bind_address option which is the one needed here anyway. So to use IP2 as source IP address when connecting instead of the default IP address hinted by the routes, one can do:

ssh -b $IP2 whatever
1
  • Even better, put BindAddress IP2 into your ~/.ssh/config into an appropriately defined Host/Match block so that it gets applied automatically to all connections to the affected server(s), including things like scp/sftp/rsync/….
    – TooTea
    Oct 22 at 20:07
2

If you have multiple interfaces, you can bind to a specific interface or IP address. This needs to be supported by the application. Applications that support it often have commandline parameters for that. There is no standard as to which commandline parameters are used.

For ssh, man ssh says:

 -B bind_interface
         Bind to the address of bind_interface before attempting to connect to the destination host.  This is only useful on systems with more than one
         address.

 -b bind_address
         Use bind_address on the local machine as the source address of the connection.  Only useful on systems with more than one address.

So you can either use the interface or IP address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.