Tutorial on basic SSH and Jupyter Notebook
Contents
Setup your SSH access
Please go through the following procedure to set up your SSH access to your group servers.
-
Generate your SSH key-pair by
ssh-keygen -t rsa -b 4096
. For more, refer to https://docs.gitlab.com/ee/ssh/. -
Contact your server admin and send him/her your SSH pub key
~/.ssh/id_rsa.pub
, to set up your SSH pub key for authentication. (WARNING: DO NOT send your private key~/.ssh/id_rsa
and keep it safe!) -
Edit
~/.ssh/config
to configure your SSH connection for convenience (not necessary, but help you to avoid typing the long IPs every time). For other servers, refer to Section Available servers.1 2 3 4
Host YOUR_SERVER_ID Hostname YOUR_SERVER_IP User YOUR_SERVER_USER Port YOUR_SERVER_PORT
where
YOUR_SERVER_ID
is any name you choose, e.g.mylab
;YOUR_SERVER_IP
is the global IP or domain name your server;YOUR_SERVER_USER
is the login user on your server; andYOUR_SERVER_PORT
is the port number your server set to offer SSH (or TCP) service. -
Once you got confirmed with key setup from your admin, you now can access SSH server by
ssh YOUR_SERVER_ID
.
Warning: The insecure login via password to SSH servers will be completely invalidated soon!
How to run and connect to Jupyter Notebook on a remote SSH server
-
SSH connect to remote server by
ssh YOUR_SERVER_ID
and insert your password (if you don’t use your SSH key authentication). -
On remote server, run the jupyter notebook web sever by
jupyter notebook --no-browser
. -
On your local machine, run
ssh -NL 8888:localhost:8888 YOUR_SERVER_ID
to build up a SSH tunneling to remote server. -
Use the URL
localhost:8888
in your web browser (local machine) to access the Jupyter Notebook on the remote server.
Note: Variable
YOUR_SERVER_ID
can be the specific URLs of SSH servers (in next section), or any identifier names specified by your~/.ssh/config
.
Tips: You may add one more
&
at the end of the above commands inStep 2,3
to bring them to the background. It allows the command to keep running even when you log out or kill your terminal. To bring them to foreground (i.e. the normal running), usefg
; to list the commands running in background, usebg
.
Note: If you have altered the port number in your Jupyter config file (in
~/.jupyter/
), e.g.,1224
, you choose replace the 1st8888
in Step 3 with1224
.
A demo to use an SSH server
Suppose that ~/.ssh/config
has configured mylab
. In terminals,
- (local machine)
ssh mylab
- (remote server)
jupyter notebook --no-browser &
- (local machine)
ssh -NL 8888:localhost:8888 mylab &
- (local machine) web browser to open the URL:
localhost:8888
- done! enjoy your Jupyter notebook :)
Tips: You may set aliases in your
~/.bashrc
to accelerate your start every time. For example, in remote server, addalias jp="jupyter notebook --no-browser"
; in local machine, addalias jp="ssh -NL 8888:localhost:8888 mylab &"
. Now you can just typejp
twice (one in ssh-ed terminal, one in local**, and you have Jupyter Notebook in your local web browser to work with.
Alternative using 花生壳
The operations to setup ssh and access servers (step 1, 2) will be the
same. The difference is that step 3,4
can be replaced by an automatic
HTTP redirection service by 花生壳. In the console of 花生壳, let the
HTTP map to 127.0.0.1:8888
, i.e. localhost:8888
the default IP and
port used by Jupyter notebook. Now once you run the Jupyter notebook on
your server, you can open it via you local web browser using the URL
provided by 花生壳.
Procedure:
- ssh your server (
ssh mylab
) and start Jupyter notebook (jp
); - open in web browser using the domain name from 花生壳.
Available servers
Private. Please contact your admin.
Author oracleyue
LastMod 2021-10-15