Awhile ago, I had AWS set up to provide me a unique URL that I could navigate to and use jupyter notebooks. I admired the convenience and the ability to just start a computation and close my laptop knowing full well my computations continued working away. However, using an AWS P2 instance can get very costly depending on your usage, which for me would be around $600 per month. So, I figured I could just build a computer with that kind of money which could serve as a deep learning rig along with the occasional video gaming :).

This post describes the configuration setup once you have already built your computer and installed a flavor of Linux like Ubuntu. Turns out that the following configuration was way easier for me to setup than AWS, and with the help of aliases, connecting to my server is easier than ever. Let’s get started!

Installation

So first you need to install the following on your Ubuntu server:

  • Anaconda, which will provide a lot of the default Python packages you’d need
  • openssh-server, which can be installed with the following: sudo apt-get install openssh-server -y
  • tmux, which can be installed with sudo apt-get install tmux -y

If you ever need to see the status of openssh-server or restart it, type the below into your terminal:

systemctl status ssh
sudo service ssh restart

Connecting Locally To Your Server

To make sure most things are set up correcly, we first need to verify that you can connect to your server on your local network.

Ok! So on your server, open your sshd_config file located at /etc/ssh/sshd_config. To make changes to it, you’ll need sudo privileges. Once the file is open, you’ll need to specify what port you’ll want to use when connecting in. Whatever you choose, I highly advise not using the default port 22. Let’s say you decide to use port 22222 instead. There’s an entry in your sshd_config file called Port and you should edit it as such:

Port 22222

Under AllowUsers, put the username you use when logging into your server.

AllowUsers your_username

Next, set PasswordAuthentication to yes.

Lastly, to make sure Ubuntu won’t block incoming web traffic on port 8888, we need to adjust its iptables:

sudo iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 8888 -j ACCEPT
sudo iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 443 -j ACCEPT
sudo netfilter-persistent save

Now, take note of your server’s IP address. This can be found by typing ifconfig into your terminal and looking for something like: inet 192.168.1.112. Once you’ve identified your server’s IP address on your local network, it’s time to pick up your laptop and try to log into it:

ssh your_username@192.168.1.112 -p 22222

If you get a terminal prompt, you’re in!

Connecting Remotely To Your Server

Now the whole point of setting up remote computing is so that you can leave your house and remote into your server while on someone else’s network. To do this only requires a few changes (which require you to still be on your network):

  • if you don’t already have a set of public and private keys on your laptop, generate them
  • copy your public key to your server: ssh-copy-id your_username@192.168.1.112 -p 22222
  • identify your server’s WAN IP address: curl 'https://api.ipify.org' (note that your ISP changes this address frequently which is why I use Google Wifi which allows me to check my WAN address from anywhere)

Now try to remote into your server using the WAN address you found!

ssh your_username@server_wan_ip -p 22222

If you see a prompt, well done! One last thing is to set PasswordAuthentication to no in your sshd_config file since now you’re logging in with a ssh key; that way no one can try brute-forcing your password.

You can now access your server from outside your network, go grab yourself that Stabucks coffee :).

Starting a Remote Jupyter Notebook

Now that all the hard work is done, you can easily use a remote jupyter notebook with the following steps:

  1. ssh into your server: ssh your_username@server_wan_ip -p 22222
  2. start a new tmux session that you can easily detch from later: tmux new -s session-name
  3. start jupyter-notebook without a browser: jupyter-notebook --no-browser --port=8889
  4. now in a new terminal on your laptop, forward your server’s port traffic to your laptop’s local port: ssh -N -L localhost:8888:localhost:8889 your_username@server_wan_ip -p 22222
  5. In your web browser, navigate to localhost:8888/tree and you should see your jupyter notebooks!

Now you can easily use jupyter notebooks on your laptop, but instead using your server’s beefy resources to do the computing.

One last thing, after having figured out the steps above, I thought I’d make the process more simple by using aliases and functions. The below are the relavent lines I added to my laptop’s .bashrc file:

server-connect() {
    ssh your_username@$1 -p 22222
}

jn-connect() {
    ssh -N -L localhost:8888:localhost:8889 your_username@$1 -p 22222
}

And the lines I added to my server’s .bashrc file:

alias jn-remote="jupyter-notebook --ip='*' --no-browser --port=8889"

Now the 5 steps above become:

  1. server-connect server_wan_ip
  2. tmux new -s session-name
  3. jn-remote
  4. open new terminal and type jn-connect server_wan_ip
  5. navigate to localhost:8888/tree

And that’s it!

If this was useful or if you have any questions, let me know in the comments!

tmux references