Search code examples
windowsjupyter-notebookwindows-subsystem-for-linuxopenssh

Access WSL2 Jupyter notebook remotely


So I'm trying to connect to my jupyter notebook from a remote pc, since my own pc doesn't have a global IP I have to first connect to another pc in the local network(server 1) and then ssh to my own pc with jupyter running on it(server 2) so its something like this:

my laptop -> server 1 -> server 2

I used to do this when both servers were Linux like this:

On my laptop: ssh -NL 2323:localhost:2323 server1_username@golbalIp

On server 1: ssh -NL localhost:2323:localhost:8888 server2_username@localip

On server 2: python3 -m jupyterlab --NotebookApp.token='' --NotebookApp.password='' --port 8888

but now my server 2 is a windows pc and my jupyter is on wls2, so I figured since on windows' localhost:8888 runs wsl2's jupyter then doing the same thing would work but it doesn't, How can I fix this?


Solution

  • I found out why my initial attempt did not work, So, wsl has its own local IP which usually changes every time you start it. I tried to make it a static IP but even editing the config files didn't work so instead we just have to check the IP and the port every time you start jupyter on Server 2

    On server 2:

    python3 -m jupyterlab --NotebookApp.token='' --NotebookApp.password='' \
     --ip $(python3 -c "import subprocess; subprocess>
    

    the you will get an output like this

    [I 2022-05-17 12:25:06.476 ServerApp] nbclassic | extension was successfully loaded.
    [I 2022-05-17 12:25:06.476 ServerApp] Serving notebooks from local directory: /mnt/d
    [I 2022-05-17 12:25:06.476 ServerApp] Jupyter Server 1.4.1 is running at:
    [I 2022-05-17 12:25:06.476 ServerApp] http://172.28.20.187:8888/lab
    [I 2022-05-17 12:25:06.476 ServerApp]  or http://127.0.0.1:8888/lab
    [I 2022-05-17 12:25:06.476 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
    

    So 172.28.20.187:8888 is the IP and the Port used by Jupyter which changes every time. every other step is the same.

    On server 1: ssh -NL localhost:<SOME_PORT_U_LIKE, ex:2323>:<IP, ex:172.28.20.187>:<PORT, usually:8888> server2_username@localip

    On my laptop: ssh -NL <SOME_PORT_U_LIKE, ex:2323>:localhost:<SOME_PORT_U_LIKE, ex:2323> server1_username@golbalIp

    also to avoid finding a the port every time I forwarded my wsl 8888 port to some other port on my windows so now If I ssh to that port on my windows I can directly connect to my jupyter notebook.

    I first allow the port 2322 through windows firewall and after that enabled ssh on my wsl and run this commands every time I restart my pc:

    @echo off
    setlocal
    
    C:\Windows\System32\bash.exe  -c "sudo /usr/sbin/service ssh start"
    
    C:\Windows\System32\netsh.exe interface portproxy delete v4tov4 listenport=2322 listenaddress=0.0.0.0 protocol=tcp
    
    for /f %%i in ('wsl hostname -I') do set IP=%%i
    C:\Windows\System32\netsh.exe interface portproxy add v4tov4 listenport=2322 listenaddress=0.0.0.0 connectport=2322 connectaddress=%IP%
    
    endlocal