目录
一、之前的版本修改方法
1、生成配置文件
jupyter notebook --generate-config
2、设置密码、获取秘钥
让输入,直接回车。然后复制代码最后产生的秘钥
from notebook.auth import passwd
passwd()
3、修改默认配置文件
vim ~/.jupyter/jupyter_notebook_config.py
- 修改如下4处:
- 行号分别对应:203,264,273,284
c.NotebookApp.ip=’*’
c.NotebookApp.password = 'sha:ce…刚才复制的那个密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8889 #随便指定一个端口
注:自动化脚本
python版本 >= 3.6
from notebook.auth import passwd
import os
home_path = os.path.expanduser('~')
cfg_path = f"{home_path}/.jupyter/jupyter_notebook_config.py"
if not os.path.exists(cfg_path):
os.system("jupyter notebook --generate-config")
key = passwd()
port = input("port(default:8888): ") or "8889"
with open(cfg_path, "r") as f:
data = f.readlines()
modify = {
"c.NotebookApp.ip": "c.NotebookApp.ip= '*'\n", # 所有ip可访问
"c.NotebookApp.password": f"c.NotebookApp.password = '{key}'\n",
"c.NotebookApp.open_browser": "c.NotebookApp.open_browser = False\n",
"c.NotebookApp.port": f"c.NotebookApp.port = {port}\n"
}
for i, j in enumerate(data):
for k in list(modify.keys()):
if k in j:
data[i] = modify[k]
modify.pop(k)
with open(cfg_path, "w") as f:
f.writelines(data)
二、新版本
jupyter-server 最近发布了新的版本升级,随之,配置文件中的部分设置也有所改变(Jupyter Notebook 7.0 更新远程访问配置)。
旧配置:
c.NotebookApp.ip = '*' 或'0.0.0.0' 所有绑定服务器所有网络接口。
c.NotebookApp.password = ''
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8889
c.NotebookApp.allow_remote_access = True
新配置:
c.ServerApp.ip = '*'
c.ServerApp.password = ''
c.ServerApp.open_browser = False
c.ServerApp.port = 8889
c.ServerApp.allow_remote_access = True
简单看下来,就是 NotebookApp
被替换为了 ServerApp
。
但是生成密码的时候就出现了问题:
旧方法:
from notebook.auth import passwd
passwd()
Enter password:
Verify password:
Out[2]: 'sha1:...'
现在提示:
>>> from notebook.auth import passwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'notebook.auth'
解决方法:
from jupyter_server.auth import passwd;
passwd()
如果你想生成 SHA1
:
from jupyter_server.auth import passwd;
passwd(algorithm='sha1')
注:自动化脚本
from jupyter_server.auth import passwd
import os
home_path = os.path.expanduser('~')
cfg_path = f"{home_path}/.jupyter/jupyter_notebook_config.py"
if not os.path.exists(cfg_path):
os.system("jupyter notebook --generate-config")
key = passwd()
port = input("port(default:8888): ") or "8889"
with open(cfg_path, "r") as f:
data = f.readlines()
modify = {
"c.ServerApp.ip": "c.ServerApp.ip= '*'\n", # 所有ip可访问
"c.ServerApp.password": f"c.ServerApp.password = '{key}'\n",
"c.ServerApp.open_browser": "c.ServerApp.open_browser = False\n",
"c.ServerApp.port": f"c.ServerApp.port = {port}\n"
}
for i, j in enumerate(data):
for k in list(modify.keys()):
if k in j:
data[i] = modify[k]
modify.pop(k)
with open(cfg_path, "w") as f:
f.writelines(data)
三、访问
启动jupyter:
jupyter notebook
访问:
linux服务器的ip:888
如192.168.3.210:888
PS:8888是刚在配置文件设置的端口
另外一种访问:
1、启动jupyter
## 启动notebook
jupyter notebook --no-browser --port=8889
## 启动lab
jupyter lab --no-browser --port=8889
2、在本地启动ssh
## name@IP替换成具体的服务器用户名及IP地址,之后输入服务器登录密码即可。
ssh -N -f -L localhost:8888:localhost:8889 name@IP
3、远程访问
本地浏览器输入http://localhost:8888/
,输入jupyter登录密码即可使用。
四、ip查询
1、win
ipconfig
2、linux
下面三个任意一个:
hostname -i
ll
ifconfig