Remote Computing with Jupyter Notebooks
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, remote computing with Jupyter Notebooks 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 correctly, 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 [email protected] -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 [email protected] -p 22222
- Identify your server’s WAN IP address by typing in your server’s terminal:
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) - On your router, turn on port forwarding. Using our example, you need to forward port 22222 to port 22222.
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 Starbucks 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:
- ssh into your server:
ssh your_username@server_wan_ip -p 22222
- Start a new tmux session that you can easily detch from later:
tmux new -s session-name
- Start jupyter-notebook without a browser:
jupyter-notebook --no-browser --port=8889
- 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
- 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 relevant 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:
server-connect server_wan_ip
tmux new -s session-name
jn-remote
- open new terminal and type
jn-connect server_wan_ip
- navigate to
localhost:8888/tree
(Photo by Lukas)