Return to SSH and Tunneling

SSH options, Port Forwarding over SSH, Keepalives

The ssh command has a number of options, and I don’t plan to cover all of them. Some of them are things that even the documentation warns against, suggesting they are only for real experts. But I want to mention the ones that I think you will find important. These options take the form of switches in the command:

  • -1 : Forces the connection to use SSH v.1 protocol only. The question here is why would you want to do that if you have SSH v.2 available. It is a real improvement, after all.
  • -2 : Forces the connection to use SSH v.2 protocol only.
  • -4 : Forces ssh to use IPv4 addresses only
  • -6 : Forces ssh to use IPv6 addresses only
  • -b : Bind address. Useful for machines that have two IP addresses, such as systems with two NICs. This tells SSH which IP address on the local machine to use for the connection
  • -L : Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address.
  • -p : Port to connect to on the remote host. This can be specified on a per-host basis in the configuration file.
  • -R : Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side. This works by allocating a socket to listen to port on the remote side, and whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the local machine.
  • -v : Verbose mode. This shows all commands and replies, and is useful for debugging
  • -W : Requests that standard input and output on the client be forwarded to host on port over the secure channel. Works with v.2 only.
  • -X : Enables X11 forwarding. But note that this can open a vulnerability

Port Forwarding

One of the handy things you can do, and something useful for tunneling. is port forwarding over ssh. The basic idea is to connect via SSH to a remote machine, and ask it to send something to a specific port other than the default port. The basic way you do this is to use the ssh command with the appropriate flags, -L and -R, which not surprisingly stand for Local and Remote. You need to specify the port you want to use, and what will be forwarded to it.

  • Local Port Forwarding – This takes a port on your local machine and forwards it to a specified port on the server.  So you can make a request on a local port like 7280 on address, and your ssh client would intercept that call and send it to port 119 on the server. Then you would have a secure connection to get whatever port 119 is configured to serve (typically Usenet traffic, but this is just an example). So you use this to configure your newsgroup client to securely grab messages from a public server, assuming it allows SSH connections.
  • Remote Port Forwarding – This is the reverse of Local Port Forwarding. Here, the idea is to specify a port on the remote server and have it forwarded to your local server. This is not very common, and you may not ever need to do this. Essentially, all traffic coming in to the server on the specified port would then be forwarded to your local machine.
  • Dynamic Port Forwarding – This creates a SOCKS proxy and is not not restricted to one port or one type of traffic.

Local Port Forwarding

Suppose you are work (or school), and you just cannot bear to miss out on your Facebook stream. But the man has a filter stopping you from accessing the site. But for the sake of argument, you can create an SSH connection to a server outside the network (which could be your computer at home). You could then do something clever using Local Port Forwarding. Create a connection as follows:

ssh -L address of home machine

Now, your home machine does need to have a public IP address, or you would need to set up your router to forward the traffic, for this to get through.

Once you have done this, you would open your browser and set it to go to http://localhost:7280, and traffic would then flow to your home machine, and from there to Facebook. You can now browse to your hearts content on your Facebook stream. Of course, this also illustrates why network admins might want to shut down SSH traffic, which they might do by blocking any outbound traffic going to port 22 (the default SSH port). And you could then try changing the default port on your home SSH server to something other than port 22, and then the admins could do deep packet inspection, and so on.

But SSH Port Forwarding is not just a matter of a security breach in the making, it can be used very legitimately in a number of situations. For example, you have a company with a number of geographically dispersed locations. In that case, SSH Port Forwarding would be a very useful way to connect sites to exchange data. You might have a database server that employees might need to connect to, and don’t want that traffic flowing through the Internet unsecured. Or perhaps you have set up a server for yourself, such as OwnCloud, and it is in a remote hosting center. Creating an SSH connection and using Port Forwarding might make your data a lot more secure.


There are a few things you need to watch out for. One is that not all ports may be available to you. If you are in a Unix-like environment, for instance, port 1024 and all ports below that can only be used by root. But any port above 1024 should be usable by a user with normal privileges as long as no one else is already using it.

The other thing you need to remember is that if the connection is dropped the port forwarding is gone. And in general TCP connections are configured to close after a period of inactivity, and on some firewalls that can be as little as 300 seconds (5 minutes). This can be controlled by a rule (or perhaps more than one) in your iptables, or directly by /proc/sys/net/ipv4/tcp_keepalive_time. But if you want a persistent connection you need to use a Keep Alive.

Keep Alives

There are two, basically. One is the TCP Keep Alive, which is simple but spoofable, and the other is the ssh keepalive, also called serveralive. Serveralive messages travel through the encrypted connection between you and the server, and thus cannot be spoofed. Assuming security is your reason for creating SSH connections, it would be more secure to use serveralive messages, therefore, though I expect using TCP keepalives is far from the worst thing that could happen. In Linux, you can set this up either for everyone (if you have root privileges), or just for yourself, by editing the appropriate config file.

  • For everyone – edit /etc/ssh/ssh_config and insert the following:
    Host *
    ServerAliveInterval 300
    ServerAliveCountMax 2
  • For just you, edit ~/.ssh/config, and add the same code to it as above

The first line, ServerAliveInterval, specifies how often a null packet should be sent to the server to keep the connection alive. However, sometimes the server may go off or drop the connection, so the second line specifies how many times you should send a packet without getting a response. The setting I have shown will send a packet, and if no response is received, it will send a second packet 300 seconds later. If no response is received to the second consecutive packet the connection will be dropped by your client.

On Windows, assuming you are using PuTTY, there is a good explanation at, but essentially you go to Connection, and then on the right under Sending of null packets to keep session active, you can set Seconds between keepalives (0 to turn off) to 300 seconds to get a similar result.

Once you understand Port Forwarding and Keepalives, you are most of the way to SSH tunneling, which is our next topic.

 Save as PDF