Permanent background ssh connection to create reverse tunnel: what is correct way?
It sounds like you want the -N
option to ssh.
-N Do not execute a remote command. This is useful for just forwarding ports
(protocol version 2 only).
I would strongly suggest you consider autossh
. It has certain heuristics that will allow it to determine if connection loss is the underlying reason and will lower the frequency of reconnection attempts. In addition it monitors the connection using extra tunnels, which makes it highly useful for scenarios like the one you are asking about.
If you are on Ubuntu, for example, you can do a web search for autossh upstart
to find some useful examples on how to configure Ubuntu so that the tunnel is kept up in a persistent fashion.
I am using this to keep a tunneled connection open to my server at all times for certain services.
I'll second @0xC0000022L's suggestion and use autossh
as well. I use it to maintain a SSH connection from my laptop as I take it from place to place and it just works. I use this connection to tunnel back ports 25 and 2143 for access to my personal SMTP and IMAP servers.
Here's the script that I use:
$ more /home/saml/bin/autossh_mail.sh
#!/bin/bash
autossh -M 0 -f -N -L 2025:localhost:25 -L 2143:localhost:143 sam@imap-o
I then maintain a Host
entry in my $HOME/.ssh/config
file for host imap-o
.
$ more $HOME/.ssh/config
ServerAliveInterval 15
ForwardX11 yes
ForwardAgent yes
ForwardX11Trusted yes
GatewayPorts yes
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
IdentityFile ~/.ssh/id_dsa
Host imap-o
User sam
ProxyCommand ssh [email protected] nc `echo %h|sed 's/-o//'` %p
The autossh_mail.sh
script is run as part of my desktop when I log in. You can access it via gnome-session-properties
.