1. 生成一个 notebook 配置文件。
[atguigu@hadoop101 桌面]$ jupyter notebook --generate-config
Writing default config to: /home/atguigu/.jupyter/jupyter_notebook_config.py (这是一个隐藏文件)
Out[3]: 'sha1:62e626059736:7788c2a6ad5ba4343c0e18ddd66b4f0e2ddf74f0'
2、生成密码。
在terminal中输入ipython。
In [2]: from notebook.auth import passwd
In [3]: passwd()
Enter password:
Verify password:
Out[3]: 'sha1:62e626059736:7788c2a6ad5ba4343c0e18ddd66b4f0e2ddf74f0'
3. 修改配置文件
sudo vim /home/atguigu/.jupyter/jupyter_notebook_config.py
在 jupyter_notebook_config.py 中找到下面的行,取消注释并修改
c.NotebookApp.allow_remote_access = True #如果是比较老的jupyter notebook版本才有这一项
# 允许远程连接
c.NotebookApp.ip='*'
#设置访问notebook的ip,*表示所有IP,这里设置ip为都可访问。补充:报错 No address associated with hostname可设置为:'0.0.0.0'
c.NotebookApp.notebook_dir = '/home/share'
#共享目录
c.NotebookApp.password = u'sha1:62e626059736:7788c2a6ad5ba4343c0e18ddd66b4f0e2ddf74f0'
#填写刚刚生成的密文
c.NotebookApp.open_browser = False
# 禁止notebook启动时自动打开浏览器(在linux服务器一般都是ssh命令行访问,没有图形界面的。所以,启动也没啥用)
c.NotebookApp.port =8888
#指定访问的端口,默认是8888
以上设置完以后就可以在服务器上启动 jupyter notebook,
root 用户使用 jupyter notebook --allow-root。
在远程电脑上,打开浏览器,输入 IP:指定的端口, 输入密码就可以访问了。
需要注意的是不能在隐藏目录 (以 . 开头的目录)下启动 jupyter notebook, 否则无法正常访问文件。
但是我的服务器直接 用jupyter notebook命令启动始终还是在localhost运行,导致远程登录失败
可能是因为我在外网连接服务器的原因。
具体解决:
$ jupyter notebook --no-browser --port=8989 --ip=服务器的ip
以上端口号可以任意指定,ip必须是服务器的ip
在本地浏览器网址输入:192.168.126.132:8989,然后你就可以看到熟悉的jupyter-notebook界面了。
但是这样连接有如下二个缺点:
1、只能访问 命令“ jupyter notebook --ip=服务器的ip ” 执行时所在的目录及子目录(所以最好在根目录下执行该命令)。
2、命令“ jupyter notebook --ip=服务器的ip ”起来的服务不能关闭,否则无法访问。
(也可以直接nohup jupyter notebook --ip=10.*.*.* )
弥补第二个缺点:直接后台运行
在想要jupyter访问的目录下:执行vim start_jupyter.sh,写如下内容:nohup jupyter notebook --ip=10.*.*.* &
让刚才生成的文件具有执行权限:chmod +x start_jupyter.sh,(此时权限又【-rw-rw-r--】变成【-rwxrwxr-x】,文件名变成绿色,后面有【*】)
执行刚才的文件:./start_jupyter.sh,(该进程在后台运行,同时会自动生成-rw------- nohup.out文件)
这样,即便关闭了命令窗口,同样可以访问。
后来我发现,我的环境下根本不需要做之前的密文,和配置文件设置,只需要最后一步就可以远程登录,不知道原因。
另外,按照上边的设置如果服务器shell关闭后就无法访问了,可以在服务器后台运行jupyter notebook来避免这一问题
启动jupyter后台运行
nohup jupyter notebook --allow-root > jupyter.log 2>&1 &
这样shell关闭也不会有问题啦。



