Score:2

Make an outgoing network connection via a specific outgoing IP address on linux

bg flag

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.

TooTea avatar
in flag
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`).
Score:4
cl flag
A.B

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
TooTea avatar
in flag
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/….
Score:2
in flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.