WSL2中VS Code无法连接已启动的Jupyter Server问题求助
解决WSL2中VS Code连接Jupyter Server 403 Forbidden问题
1. 补全Jupyter启动参数
你当前的启动命令缺少关键的凭证允许配置,修改为以下命令重启Jupyter:
jupyter notebook --allow-root --NotebookApp.allow_origin='*' --NotebookApp.allow_credentials=True --no-browser
allow_credentials=True会允许VS Code携带连接所需的凭证,是解决403的常见关键项。
2. 清理VS Code中已保存的服务器记录
虽然提示URL已存在,但旧记录可能存在异常:
- 打开VS Code命令面板(
Ctrl+Shift+P) - 输入
Jupyter: Remove Jupyter Server - 选中之前添加的带token的URL删除,之后重新尝试添加连接
3. 调整VS Code的Jupyter扩展配置
- 打开VS Code设置(
Ctrl+,),搜索Jupyter: Server Connection Timeout,将数值调至60000毫秒(60秒),避免连接超时被误判为失败 - 搜索
Jupyter: Allow Unauthorized SSL,如果你的Jupyter用的是默认http协议,确保该选项处于关闭状态
4. 修改Jupyter配置文件彻底放开权限
如果临时参数无效,直接修改配置文件:
- 在WSL终端运行
jupyter notebook --generate-config生成配置文件(若已存在可跳过) - 打开
~/.jupyter/jupyter_notebook_config.py,找到并修改以下配置项:c.NotebookApp.allow_root = True c.NotebookApp.allow_origin = '*' c.NotebookApp.allow_credentials = True c.NotebookApp.disable_check_xsrf = True # 临时关闭XSRF检查,解决403核心原因 - 重启Jupyter Server后再尝试连接
5. 确认WSL2端口转发与防火墙状态
虽然浏览器能访问,但VS Code的请求可能被防火墙或端口转发规则拦截:
- 在WSL终端运行
hostname -I获取WSL2的IP地址 - 在Windows命令提示符运行
netsh interface portproxy show all,确认存在0.0.0.0:8888转发到WSL2 IP:8888的记录,若没有则手动添加:netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8888 connectaddress=你的WSL2IP connectport=8888 - 临时关闭Windows Defender防火墙测试,或添加8888端口的入站允许规则
6. 确认VS Code的运行环境一致性
如果你是在WSL2中直接启动VS Code(code .),确保VS Code终端的which jupyter输出路径,和启动Jupyter Server的终端路径完全一致,避免环境不一致导致的连接失败
内容的提问来源于stack exchange,提问作者Маруф Башарходжаев
相关产品推荐
相关产品推荐

