I know how to connect to a port using ssh

ssh user@remotehostip -p XXX

Is there a way to establish ssh from the port which my local computer will be using?

Best Answer


This is not immediately possible How that can be done depends on where the source port shall be seen: locally, too, or is it enough if it's the right port from the perspective of the external network?

You can run the ssh client in an lxc container So i can't explain it all to you in detail But you create a virtual network interface and attach it to this container so that ssh uses this interface because it is the only (external) interface it sees.

The host system should be able to detect a packet coming from this interface Thus you can use netfilter's nat (snat) for rewriting the source address with something like.

iptables -t nat -A POSTROUTING -o vnet0 -p tcp --dport 22 -j SNAT --to-source :1234

Of course, this does not work (or becomes more complicated) if you connect to ports different from 22.