利用frp内网穿透远程访问Jupyter Notebook

一、远程登陆 jupyter notebook

  1. 登陆远程服务器

  2. 生成配置文件

     jupyter notebook --generate-config
    

    执行后报错:
    在这里插入图片描述
    根据错误提示没有权限,执行下面的命令给文件或文件夹加上所有权限

     sudo chmod 777 ~/.jupyter
    
  3. 生成密码

    打开ipython,创建一个密文的密码:

     In [1]: from notebook.auth import passwd
     In [2]: passwd()
     Enter password:
     Verify password:
     Out[2]: 'sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274'
    

    把生成的密文‘sha1:ce…’复制下来

  4. 修改默认配置文件

     vim ~/.jupyter/jupyter_notebook_config.py
    

    进行如下修改:

     c.NotebookApp.ip='*'
     c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
     c.NotebookApp.open_browser = False
     c.NotebookApp.port =8888 #随便指定一个端口
     c.NotebookApp.allow_remote_access = True
     c.NotebookApp.notebook_dir = '*'  #默认打开路径
    
  5. 启动jupyter notebook

       jupyter notebook
    

    此时应该可以在局域网内直接访问ip:8888就可以看到jupyter的登陆界面。
    如果登陆失败,则有可能是服务器防火墙设置的问题,此时最简单的方法是在本地建立一个ssh通道:
    在本地终端中输入ssh username@address_of_remote -L127.0.0.1:1234:127.0.0.1:8888
    便可以在localhost:1234直接访问远程的jupyter了。

二、远程访问

利用frp将8888端口映射出去,详情见frp内网穿透

vim frpc.ini

frpc配置如下:

[common]
server_addr=***.***.***.*** #公网ip
server_port=7000
token=**** #密码

[jupyter]
type = tcp
local_ip = 127.0.0.1
local_port = 8888
remote_port = *****

配置完成后直接 ./frpc -c ./frpc.ini

三、添加开机启动服务

首先 sudo vim /lib/systemd/system/jupyter.service

jupyter.service里写入以下内容

[Unit]
Description=jupyter
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=simple
User=username  #若在root权限下改为 root
ExecStart=/home/username/anaconda3/bin/jupyter-lab --allow-root

[Install]
WantedBy=multi-user.target

然后启动 jupyter

sudo systemctl start jupyter.service

再打开自启动

sudo systemctl enable jupyter.service

同时

停止 sudo systemctl stop jupyter.service
重启 sudo systemctl restart jupyter.service
查看状态 sudo systemctl status jupyter.service

最后重启frp服务器 sudo systemctl restart frpc

此时可通过公网ip:remote_port/lab?在本地浏览器打开jupyter lab

新建、删除文件时因为权限问题出现报错:

解决方案如下:
sudo chmod -R g+w path(jupyter默认打开路径),意思是给文件夹path所属的用户组加上写文件的权限

发布了2 篇原创文章 · 获赞 2 · 访问量 92

猜你喜欢

转载自blog.csdn.net/weixin_42783784/article/details/105540340