How may I do a SSH tunneling?

After reading an article about how cool can SSH be, I tried the solution given there about tunneling your connection.

This is the very first time I try this.

I've opened a terminal and typed:

ssh -D 9999 -C myUsername@localhost

And I've got this message:

Privileged ports can only be forwarded by root.

So I started again, this time, with sudo:

sudo ssh -D 9999 -C root@localhost

But I still cannot acces. This is the message that I've received this time:

ssh: connect to host localhost port 22: Connection refused

What Am I doing wrong?


A simple tunnel is more usually done as

ssh -L 8080:localhost:80 [email protected]

  • -L = local port forward, ie from where I am to where I'm going
  • 8080 = port on this machine to forward. The port my browser will connect to as http://localhost:8080
  • localhost = The destination on the remote server, in this case itself.
  • 80 = Port on the remote (target) machine to connect to, the port where the service is running.

This would allow you to access the web server on the remote server, when you have ssh access to it, but do not have web access to it.

ssh'ing to your localhost is usually pointless. It is only done to check your ssh server is running properly or keys are correct.

The connection refused error you see is because ssh server is not running on your local machine. sudo apt-get install openssh-server Then you can ssh to your local machine from localhost, or anywhere else you can get a port 22 connection to this machine from (typically behind your local network firewall)

-D (dynamic application forwarding) is a different case, and would more apply if some tricky fat client application (eg mysql) on your local machine needs to access a server on a remote machine. It is less often used.

The message about privileged ports occurs when you try to redirect a local port less than 1024. Hence the use of port 8080 in the above example, to avoid the priviledged port 80. For the example above, your browser would use http://localhost:8080