Jupyter and VScode Remote on a server behind a gateway
28 Aug 2019The goal is to be able to work on the remote linux server, which sits behind an ssh gateway or proxy, using GUI apps running natively on the local windows machine.
The first step is to set up the tunnels in PuTTY:
- As the host name use the gateway that you have to use to access your server
- Go into Connection/SSH/Tunnels
- In the "Destination" field put the hostname or ip and port of the service that
you want to use. Let's start with the ssh port 22, so it would be similar to:
192.168.0.1:22
- In source port give the local port that is going to be tunneled from your local host to the destination port. I used 10022 for the ssh port.
- Click "add".
- For a jupyter setup do the same with the remote 8888 port (I tunneled my local 18888 there).
- For convenience go back to "Session", in the "Saved sessions" field give a name for the configuration and click "Save". You have to do it again every time you want to modify the configuration (load, modify, save).
- Click open.
Now you should be able to directly ssh onto the remote server:
$ ssh -p 10022 remote_username@localhost
I find it helpful to configure the connection in the .ssh/config
file:
Host my_tunnel
HostName localhost
Port 10022
User remote_username
IdentityFile ~/.ssh/id_rsa
Now you can simply do $ ssh my_tunnel
.
With the configuration stored in the .ssh/config
file you can also use the
Remote-SSH feature of VScode. However, I found that in order for that to work I
need two files. .ssh/config
is used by the regular ssh client, which requires
it to have permissions only to my Windows user (and not SYSTEM and
Administrators). Then I have a second .ssh/config_vscode
, which I told VScode
to use, as it requires that SYSTEM and Administrators have access to it (as is by
default on Windows).
To run jupyter first install it on the remote server. I like to use conda for that. I create a dedicated environment for jupyter, activate it and install the basic packages:
$ conda create --name jupyter
$ conda activate jupyter
$ conda install jupyterlab numpy scipy matplotlib
Then I run command on the remote server that starts a deamon screen session in which jupyter runs.
$ screen -dmS jupyter bash -lc "conda activate jupyter; jupyter lab --ip='*' --no-browser"
By default jupyter will only allow connections from localhost, hence the --ip
option. You can always check on this session by connecting to it:
$ screen -r jupyter
And then doing ^a^d to detach again. At first you need to do that to check the login token.
Then on your local machine you can open the browser and go to localhost:18888
and you will see the jupyter lab environment running on the remote server. You
will need to give the token for security reasons. You can also immediately on
the screen configure a permanent password, so that you don't need to copy the
token each time you start jupyter new.