This is a nice working fix. However, I want direct access to my desktop behind firewall via nx. This is on the top list of TODO.
Login to the computer behind the firewall.
- Generate a new ssh private key that is to be used as the authentication for the tunnel. It will have no password to enable automatic login.
ssh-keygen -f ~/.ssh/tunnel-id -t rsa -N ""
- Create a new entry in your ~/.ssh/config that will be used to initiate the tunnel. Set the Host entry to an uncommon name so that you will also be able to login to the relay computer normally. It will establish a connection from port 11111 on the loopback interface on the relay computer to the ssh port on the loopback interface on the server.
Host relay HostName relay.hostname.com RemoteForward 11111 localhost:22 IdentityFile ~/.ssh/tunnel-id
- Copy the public key to the relay server:
ssh-copy-id -i .ssh/tunnel-id relay.hostname.com
- To make it easy to start the tunnel, create a script in ~/bin/setup-tunnel.sh:
while true; do /usr/bin/ssh relay sleep 1 done < /dev/null & disown
Login to the relay computer.
- The ssh-copy-id appended the public key to the ~/.ssh/authorized_keys file. Because we want to limit the havoc that can be caused by the key, change the entry that was just created
ssh-rsa AAAAB3...= email@example.com
command="sleep 604800" ssh-rsa AAAAB3...= firstname.lastname@example.org
This will give the tunnel a livetime of a week, make it work without a local terminal on the server side (we used < /dev/null) and limit the commands that can be executed with this key.
Login to the client computer.
- To make it possible to transparently connect to the server through the tunnel, we use netcat on the relay computer and a ProxyCommand in the ~/.ssh/config file on the client. Add a new entry with
Host server HostName server.hostname.com ProxyCommand ssh relay.hostname.com bin/nc localhost 11111
Whenever you want to have the tunnel enabled, start ~/bin/setup-tunnel.sh on the server. Afterwards you will be able to connect from the client with ssh server. You will be asked for a password two times, once for the relay and once for the server.
The following diagram shows the general idea of the created tunnel:
+--------+ ( ) +----------------------+ | Client |------( Internet )---->| relay.hostname.com | +--------+ ( ) +----------------------+ : /|\ : | : [ X ] : | : Forwarded ssh connection +-------------------+ :..............................>| server.domain.com | over "server" ssh alias +-------------------+ --- real connection ... virtual connection [X] switch