1829065 Members
2476 Online
109986 Solutions
New Discussion

PORT FORWARD SSH

 
SOLVED
Go to solution
Inter_1
Frequent Advisor

PORT FORWARD SSH



Server1
Port 2222

Server2
Port 22

How can I forward the Port 22 from Server to Port 2222 to Server1. I want also that the Server1 to use 2222 to interact with the Port 22 of Server2.

Thanks

I like to fix things.
7 REPLIES 7
hpuxrox
Respected Contributor

Re: PORT FORWARD SSH

So what your saying is, when you connect to port 22 via ssh on Server1 you want to really access server2? And, when you access port 2222 on server2 you will be accessing server one. Accentually, you will be able to connect to both machines from just one of the machines.

On Server1
ssh -L2222:server1:22 server2 sleep 1000000

On Server2
ssh -L22:server1:2222 server2 sleep 1000000
hpuxrox
Respected Contributor

Re: PORT FORWARD SSH

Be sure to verify your sshd_config file for the following settings

# Port forwarding
AllowTcpForwarding yes

# If port forwarding is enabled, specify if the server can bind to INADDR_ANY.
# This allows the local port forwarding to work when connections are received
# from any remote host.
GatewayPorts no
Matti_Kurkela
Honored Contributor
Solution

Re: PORT FORWARD SSH

If you have sshd running on a server, you cannot use port 22 as a forwarding source, because sshd is already occupying that port.
If a port is occupied by a process listening on it, you cannot re-use that port number.

When using the -L option the syntax is
-L::

The is where the ssh sets up a listening socket on the local host (where the command is executed). Then the data is taken (using the encrypted SSH tunnel) to the destination host of the SSH connection.

*From there* it is forwarded to :. This last step is *not* protected by the SSH encryption, so it's good to make this step as short as possible.

So, if you enter on server1 a command like:
ssh -L 2222:localhost:22 server2 sleep 1000000

it actually makes sense (the "localhost" is interpreted according to server2's point of view).

After this, if you connect to server1:22, you'll get server1 as normal. But if you connect to server1:2222, it will behave just as if you'd connected to server2:22.

However, unless you use the -g option, connections to server1:2222 will be refused unless they are originating from server1.
MK
Inter_1
Frequent Advisor

Re: PORT FORWARD SSH

Thank you for your answers. I have done the changes as you suggested, but I would like to know how can I test that is working or not?

I like to fix things.
Matti_Kurkela
Honored Contributor

Re: PORT FORWARD SSH

Err... connect to the ports and see whether you get the connectivity you required?

Was this a trick question? :-)
MK
Inter_1
Frequent Advisor

Re: PORT FORWARD SSH

That was not a tricky question. Some time the brain doesn't function proper and the easy things looks difficult :)
I like to fix things.
Inter_1
Frequent Advisor

Re: PORT FORWARD SSH

thanks
I like to fix things.