19

I have computer with Ubuntu behind router that I can't configure. However I want to have ssh access to that computer. I think it is possible with ssh tunneling, but I don't know how to do it. I have another server to which I would like to setup tunneling. How to do it? Or maybe you have some other idea how to solve this problem?

I tried:

ssh -N user@my_server -L 22/localhost/8090

but it says:

bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 22
Could not request local forwarding.
klew
  • 713
  • 2
  • 11
  • 16

4 Answers4

23

You are asking it to listen on your local port 22 and forward connections to a remote system's port 8090. You can't do that, because your local port 22 is already taken by your local SSH server.

I think what you are looking for is remote forwarding. Replacing -L 22:localhost:8090 with -R 8090:localhost:22 will tell the remote host to listen on port 8090 and forward requests to your SSH server.

If you are leaving the connection running so you can get in later from a remote site, then you are going to want to make sure the connection doesn't time-out due to inactivity by adding the relevant options (-o TCPKeepAlive=yes or -o ServerAliveInterval=30)

So you'll end up with something like:

ssh -N user@my_server -R 8090:localhost:22 -o ServerAliveInterval=30

Also, if one of the network hops between you and the server is down at any point, the connection will drop despite any KeepAlive options you specify, so you might want to add this command to inittab, or look into the daemontools package or your distro's equivalent , so that it always starts on boot and is restarted when it exits for some reason other then system shutdown (or you could run it from a shell script that loops infinitely, but init or daemontools are cleaner solutions).

David Spillett
  • 22,534
  • 42
  • 66
  • It almost works. I can connect to my computer, but I can do it only when I'm logged on server. I would like to connect via that port from everywhere. – klew Jun 29 '09 at 17:59
  • 3
    If you add the server's public address or a wild-card to the port forwarding definition (-R 111.222.333.444:8090:localhost:22 or -R *:8090:localhost:22) then that may work, though that sort of thing can be disabled on the server. If the server is one you control, then ensure that the GatewayPorts option is enabled in sshd_config. – David Spillett Jun 29 '09 at 21:06
  • I wrote a script that helps me to do ssh tunneling, you can check it out at: https://github.com/gdbtek/ssh-tunneling – Nam Nguyen Apr 27 '14 at 09:43
  • Adding `GatewayPorts Yes` to my sshd_config helped me open the port to the public. – Adam F Jan 13 '17 at 04:39
8

The reason you can't do this is because you're trying to forward port 22 on the local computer to port 8090 on the remote server and something is already running on port 22 on the local server. Mostly likely you have an SSH server running. You can fix this by changing the 22 to a different value. You can check to see if a port is free by running:

# netstat -lep --tcp

This lists all the listening sockets, so if the port isn't listed, then it's free.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
3

I'm using lsof -i :PortNumber command to check if port is free:

# lsof -i :2272

if port is free you will see nothing in output.

Taras
  • 228
  • 1
  • 2
  • 8
1

I've had the same problem, and I ran

$ ps aux | grep ssh

I found that sshd was working in the background, so I terminated the process, I only want to ssh into another machine, not in my machine

$ sudo service sshd stop

kenlukas
  • 2,886
  • 2
  • 14
  • 25
polendina
  • 61
  • 1
  • 4