远程访问 WSL2 Jupyter notebook

Access WSL2 Jupyter notebook remotely

所以我正在尝试从远程电脑连接到我的 jupyter notebook,因为我自己的电脑没有全局 IP,我必须先连接到本地网络(服务器 1)中的另一台电脑,然后通过 jupyter 运行 ssh 连接到我自己的电脑(服务器 2)所以它是这样的:

我的笔记本电脑 -> 服务器 1 -> 服务器 2

当两台服务器都像这样 Linux 时,我曾经这样做过:

在我的笔记本电脑上: ssh -NL 2323:localhost:2323 server1_username@golbalIp

在服务器 1 上: ssh -NL localhost:2323:localhost:8888 server2_username@localip

在服务器 2 上: python3 -m jupyterlab --NotebookApp.token='' --NotebookApp.password='' --port 8888

但现在我的服务器 2 是一台 windows 个人电脑,我的 jupyter 在 wls2 上,所以我想因为 windows' localhost:8888 运行 wsl2 的 jupyter 然后做同样的事情会工作但没有,我该如何解决这个问题?

一点也不明显,但解决方案是 Windows 10 不喜欢端口 8888,需要您使用 8889 才能使端口转发在 WSL2 上工作。我花了很长时间才解决这个问题。在本机 linux 下,端口完全没有问题,除非远程主机具有特定的自定义防火墙规则。

所以你必须使用:

 ssh -NL 8889:localhost:8888 remote-server-alias

其中 remote-server-alias 是您在 .ssh/config 中使用 JumpProxy 定义的内容,您还必须在 Windows 上的 Debian 上安装 apache2 服务器 运行 (WSL2 ) 安装(例如)。首先在浏览器中检查 localhost 以确保 apache 服务器 运行 正确。

开始之前,您必须执行以下操作:

ssh remote-server-alias
nohup jupyter notebook --no-browser --port 8888 &

令牌将存储在 nohup.out 中。

我发现了为什么我最初的尝试没有成功,所以,wsl 有它自己的本地 IP,通常每次启动它时都会改变。我试图将其设为静态 IP,但即使编辑配置文件也不起作用,所以每次在服务器 2 上启动 jupyter 时我们只需要检查 IP 和端口

在服务器 2 上:

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

你会得到这样的输出

[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).

所以172.28.20.187:8888是Jupyter使用的IP和端口,每次都在变化。其他步骤都是一样的。

在服务器 1 上:ssh -NL localhost:<SOME_PORT_U_LIKE, ex:2323>:<IP, ex:172.28.20.187>:<PORT, usually:8888> server2_username@localip

在我的笔记本电脑上:ssh -NL :localhost: server1_username@golbalIp