SSH tunneling

You need to log in to a server on an internal network, but you can only access the internal network through a gateway machine. If both these machines are running SSH, it is convenient to set up an SSH tunnel.

Usage
Begin with creating a ssh tunneling session:

The -f argument instructs the ssh instance to go into the background, and -N instructs it to not launch a shell. Followd by:

The variables above represent:
 * GATEWAY = The hostname/IP address of the gateway machine.
 * GWUSERNAME = The username on the gateway (optional if this username is the same as on the client).
 * SERVER = The hostname/IP address of the server you wish to log into.
 * SUSERNAME = The username on the server (optional if this username is the same as on the client).
 * SPORT = The port number on which the server SSH daemon is listening, by default 22.
 * CPORT = The port number of your choosing on which the tunnel will be receiving connections on the client machine (should be greater than 1024 unless you are invoking as root).

You can scp files from the server as you would normally by specifying the tunnel port:

Similarly for sending files to the server:

Tips
In order to make this tunneling process less onerous in the future:
 * Set these commands as shell aliases (in Bash, usually in ).
 * To avoid typing passwords, copy the client key to the gateway, and the client and gateway keys to the server. This process is described in the official Gentoo documentation OpenSSH key management.
 * If you rely upon keeping an unattended connection alive which may become dropped due to timeouts, consider altering the various TCP keepalive settings in the client and server configurations. Perhaps the most robust solution is to install a connection watchdog such as net-misc/autossh which will babysit an ssh session and restart it if necessary.

X11 forwarding
To enable X11 forwarding, first the X11Forwarding and ForwardX11 options must be configured to yes for both the X client and server being connected to respectively. In your SSH client connection, add the -Y switch to the second invocation above, and optionally the -C switch to also enable compression i.e:

The following is required for the forwarding of X11 connections from the remote server to local client to work:


 * The SSH daemon on the gateway machine must have TCP forwarding must be enabled, otherwise X11 connections won't be forwarded:


 * The xauth tool must be present on the local X server. Install with the X USE flag set to pull it in or install.


 * X11 forwarding must be enabled in the remote server SSH daemon configuration: