Use SSH to connect the virtual machine on cloud and run jupyter notebook on it

In this blog, I will introduce how to create a virtual machine on openstack (it's a cloud platform), and then using SSH to connect the virtual machine with local computer. After the connection, I will show how to run jupyter notebook on this remote virtual machine.

(1) Create a SSH-keypair on openstack

The only method allowed to access the cloud instances is via ssh-keypairs. Username/Password is disabled by default on all cloud instances (according to best practice) and should never be enabled for security reasons.

The OpenStack software helps us create/import keys, and will make sure that the public keys are injected in the instances you create. The private key should be private and is for us to safekeep on our clients.

In the OpenStack dashboard: Compute -> Key Pairs -> Create Key Pair

Save the downloaded .pem file in a secure location on computer. We would need it when we want to access the virtual machine instance but we don't need to create a new ssh-keypair each time.

(2) Launch an instance of virtual machine on openstack

In the OpenStack dashboard: Compute -> Instances -> Launch instance

Then we can choose the configuration we want for the virtual machine. For my instance, I choose ubuntu 20.04 as the image, 1 VCPUS, 512 MB RAM and 20 GB disk. We also have to give this instance a name, and it would auto select to use the key-pair we create. As for the rest of the configuration, I chose the default for them.

(3) Use SSH to access the virtual machine from local computer

Before the SSH connection, we need to associate a floating IP to the instance, which represents the IP address of this virtual machine. After the IP setting, we should change the permission of the downloaded-keypair file and SSH file so that the IP can access the key and connect with local computer. Run the following commands in terminal to change permission. "/..." is the path of this file on your computer.

1
2
3
chmod 400 /.../liang_cheng.pem
chmod 700 /.../.ssh
chmod 600 /.../.ssh/authorized_keys

After the permission-change, we are able to use SSH to access the virtual machine by running the commands below.

1
2
3
4
# if the instance uses old volume or snapshot but not a new image as the boot source, 
# we need to run the next command first before run ssh -i
ssh-keygen -R floating IP
ssh -i /.../liang_cheng.pem ubuntu@floating IP

(4) Install jupyter notebook and run it on the virtual machine

Firstly, we need to install pip and jupyter notebook on the virtual machine.

1
2
3
4
5
6
7
8
# switch to root user first
sudo su
apt install python3-pip
pip install jupyter notebook
# if it shows error 'E: Package <packagename> has no installation candidate'
# run the following commands and then install pip and jupyter notebook
apt-get update
apt-get upgrade

Then we edit the config file of jupyter notebook so that we can remotely log in it on the local browser.

1
2
3
4
5
6
7
8
jupyter notebook --generate-config
vim /.../jupyter_notebook_config.py
# after entering the file, press button O to get into edit mode
# add the following three codes into the config file
c.NotebookApp.ip='*'
c.NotebookApp.open_browser=False
c.NotebookApp.port=8888
# press button esc to turn off edit mode, and type :wq! to save the changes in file

After everything is set up, we can run jupyter notebook.

1
jupyter notebook --allow-root


All articles in this blog adopt the CC BY-SA 4.0 agreement except for special statements. Please indicate the source for reprinting!