ssh local port to local port forwarding; connection refused

13.1k views Asked by At

I am trying to set up a local-to-local port forwarding on a ssh server. My goal is to forward the local port 12345 to port 22 on the same local machine. Nevertheless, I get connection refused.

Here is what I am running:

me@myHost:~$ ssh -L 12345:localhost:22 10.1.1.6
[...]
Authenticated to 10.1.1.6 ([10.1.1.6]:22).
debug1: Local connections to LOCALHOST:12345 forwarded to remote address 127.0.0.1:22
debug1: Local forwarding listening on ::1 port 12345.
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 12345.
debug1: channel 1: new [port listener]
debug1: channel 2: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8

me@remoteHost:~$ ssh localhost -p 12345
ssh: connect to host localhost port 12345: Connection refused

me@remoteHost:~$ nc -v localhost 12345
nc: connect to localhost port 12345 (tcp) failed: Connection refused
nc: connect to localhost port 12345 (tcp) failed: Connection refused

debug1: Connection to port 12345 forwarding to localhost port 22 requested.
debug1: channel 3: new [direct-tcpip]
debug1: channel 3: free: direct-tcpip: listening port 12345 for localhost port 22, connect from ::1 port 59785 to ::1 port 12345, nchannels 4

It does not work at all and it's very weird, as I have managed to do it with the exact same command sequence on my local machine.


Here is my checklist on the remote server. A couple of different stuff I have investigated into while googling for the issue:

(1) netstat output:

root@remoteServer:~# netstat -tpln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      12791/sshd      
tcp6       0      0 :::22                   :::*                    LISTEN      12791/sshd      

(2) /etc/hosts.allow and /etc/hosts.deny files: empty

(3) Configuration files: exactly the same as on my local machine. I have removed a couple of commented lines to keep it short.

root@remoteHost:~# cat /etc/ssh/sshd_config 
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes

And the second one:

root@remoteHost:~# cat /etc/ssh/ssh_config 

Host *
SendEnv LANG LC_*
HashKnownHosts yes
GSSAPIAuthentication yes
GSSAPIDelegateCredentials no

Finally, in the -L flag I have used localhost, [::1], 10.1.1.6 (the routable IP), 127.0.0.1, all no to avail.

1

There are 1 answers

1
Adama On BEST ANSWER

Problem solved. It was my own fault, I had completely confused local port forwarding -L with remote port forwarding -R. Therefore, there was no issue in the first place.

I figured out that when writing:

me@myHost:~$ ssh -L 12345:localhost:22 10.1.1.6

It's the local port 12345 of myHost that gets forwarded to 10.1.1.6 and from there the remote host forwards it to 10.1.1.6:22.

Thus, typing me@remoteHost:~$ ssh localhost -p 12345 obviously does nothing, but typing me@myHost:~$ ssh localhost -p 12345 initiates a ssh connection, as myHost:12345 is eventually forwarded to remoteHost:22.