SSH – How to Specify a Local Port When Establishing SSH Connections

networkingssh

I have two servers hosted in IDC. I can only use ports 20/21/22/23/3389/33101-33109 to establish connections between two servers. The IDC network device will block any other packets whose source or destination port is not in the 20/21/22/23/80/3389/33101-33109 list/range. But the source port of SSH is random.

Using the command
ssh username@server -p remote_port
one can easily specify a remote port.

So is there an ssh command parameter or some other way to specify a local source port so I can use, for example, port 33101 to establish the SSH connection?

My network topology is like this:
network topology

Best Answer

You can not specify the source port for ssh client.

But you can use nc as a proxy, like this:

ssh -p 33101 -o 'ProxyCommand nc -p 33101 %h %p' $SERVER_2

From How can i set the source port for SSH on unbuntu server? (on ServerFault).

Related Question