SSH tunnels and IPython

We have an OS X server blade at the lab that’s been suped up with some extra RAM (I think 24GB) and is very useful for our bioinformatics processing, as there are a number of bioinformatics tools installed on it. Additionally, running sequence analysis jobs on the server keeps my personal computer free to do other tasks instead of being swamped by these computationally expensive processes.

With that being the case, it’s also much more convenient to log in to the server remotely instead of having to sit directly at the computer to initiate jobs.

I’ve previously written how I’ve managed to set up SSH and the SSH config file to simplify my life for remotely logging in to the server. That part is pretty straightforward. However, we also use IPython Notebooks to document how we’ve processed data. IPython Notebooks run through a web browser, so how can we still use them when we’ve SSH’d into the remote server via the command line?

It turns out, you can set up a SSH tunnel and then connect to the server through your browser. This does require that you have access to the server, as well as available ports on both your client and server to connect through.

Here’s how I create a tunnel into our server:

ssh -L client_port_Number_over8000:localhost:server_port_Number_over8000 user_name@server_ip_address

That’s a lot to type, especially if you’re logging into the server multiple times a week or even multiple times a day. To get around this, we can edit the SSH config file (found in ~/.ssh/config)

Host easy_to_remember_nickname
HostName IP_address_of_remote_server
User remote_server_login_username
ControlMaster auto
LocalForward localhost:client_portNumber_over8000 localhost:server_portNumber_over8000
ServerAliveInterval 30

With this info in the config file, recreating the tunnel is as easy as typing the following on the command line:

$ssh easy_to_remember_nickname

Once the tunnel is established, open a new Terminal window and SSH into the server. Once the SSH connection is established, change to the directory containing your IPython notebook(s) (Note: this is not needed if you’ve set up an IPython config file that sets the default IPython directory), then launch IPython on the remote machine:

$ipython notebook --no-browser --port=first_port_number_listed_in_tunnel_command

Now, on the machine you’re using, start your browser and enter the following into the URL of your browser:

localhost:first_port_number_listed_in_tunnel_command

This should connect through the tunnel and you’ll have the normal web-based view of your notebooks, but those notebooks are actually housed on a remote machine!

Leave a Comment