Debian如何使用Jupyter Notebook

在 Debian 服务器上安装并运行 Jupyter Notebook 并配置本地远程访问,以下是完整的步骤:

1. 安装 Jupyter Notebook

既然你已经安装了 Anaconda,Jupyter Notebook 通常会与 Anaconda 一起安装。如果没有安装,可以通过以下命令安装:

conda install jupyter

2. 配置 Jupyter Notebook 以便进行远程访问

为了让 Jupyter Notebook 能够通过网络进行远程访问,你需要做一些配置。

2.1 生成 Jupyter 配置文件

运行以下命令生成 Jupyter 配置文件:

jupyter notebook --generate-config

此命令会在 ~/.jupyter/ 目录下生成 jupyter_notebook_config.py 文件。

2.2 设置密码

为确保远程访问安全,你应该为 Jupyter Notebook 设置密码。你可以通过以下命令来设置:

jupyter notebook password

这会要求你输入并确认一个密码。

2.3 修改配置文件

打开 jupyter_notebook_config.py 配置文件:

nano ~/.jupyter/jupyter_notebook_config.py

在文件中找到并修改或取消注释以下配置项:

# 配置为允许所有IP地址访问
c.NotebookApp.ip = '0.0.0.0'

# 设置端口,默认是 8888
c.NotebookApp.port = 8888

# 不启动浏览器
c.NotebookApp.open_browser = False

# 设置为开启 Notebook 密码
c.NotebookApp.password_required = True

# 设置 Jupyter Notebook 的访问地址
c.NotebookApp.allow_remote_access = True

2.4 配置防火墙

确保服务器的防火墙允许外部访问 Jupyter Notebook 默认的 8888 端口。使用 ufw 或其他防火墙工具来开放端口:

sudo ufw allow 8888

3. 启动 Jupyter Notebook

启动 Jupyter Notebook 服务器:

jupyter notebook

此时,Jupyter Notebook 会启动并监听 0.0.0.0:8888(所有 IP 地址的 8888 端口)。你会在终端中看到类似以下内容:

    To access the notebook, open this file in a browser:
        file:///home/username/.local/share/jupyter/runtime/nbserver-XXXX-open.html
    Or copy and paste one of these URLs:
        http://127.0.0.1:8888/?token=XXXXXXXXXXXXXXXXXXXX
     or http://localhost:8888/?token=XXXXXXXXXXXXXXXXXXXX

4. 访问 Jupyter Notebook

现在你可以从任何机器上使用浏览器访问你的服务器。在浏览器中输入服务器的 IP 地址和端口,例如:

http://<server-ip>:8888

例如,如果你的服务器 IP 是 192.168.1.100,那么你应该输入:

http://192.168.1.100:8888

你会看到一个要求输入密码的页面,输入你设置的密码后,就可以开始使用 Jupyter Notebook 了。

5. (可选) 使用 SSH 隧道访问 Jupyter Notebook

如果你不想直接暴露 Jupyter Notebook 到外网,也可以通过 SSH 隧道进行远程访问:

在本地机器上运行以下命令:

ssh -L 8888:localhost:8888 your_user@<server-ip>

这将把本地的 8888 端口转发到远程服务器的 8888 端口。然后,在本地浏览器中访问:

http://localhost:8888

这样你就可以通过 SSH 隧道安全地访问 Jupyter Notebook。

总结

现在,你已经成功地配置并启动了 Jupyter Notebook,可以通过浏览器在本地或远程访问它。如果你需要进一步的帮助,随时告诉我!

将Jupyter notebook封装为服务

你可以通过以下几个步骤将 Jupyter Notebook 封装为服务并使其在后台运行:

1. 创建一个 systemd 服务单元文件

在 Debian 系统中,systemd 是管理系统和服务的工具。你可以通过创建一个 systemd 服务单元文件来启动和管理 Jupyter Notebook。

创建服务文件

首先,使用管理员权限创建一个新的 systemd 服务文件。例如,创建名为 jupyter-notebook.service 的文件:

sudo nano /etc/systemd/system/jupyter-notebook.service

编辑服务文件

将以下内容复制粘贴到服务文件中:

[Unit]
Description=Jupyter Notebook
After=network.target

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/usr/bin/jupyter-notebook --config=/home/your_username/.jupyter/jupyter_notebook_config.py --no-browser --port=8888
WorkingDirectory=/home/your_username
User=your_username
Group=your_username
Restart=always

[Install]
WantedBy=multi-user.target

请注意:

  • ExecStart 路径中的 /usr/bin/jupyter-notebook 是你安装 Jupyter Notebook 的路径,可能需要根据你的 Anaconda 安装位置调整。
  • --config 参数指向的是 Jupyter 配置文件的位置(例如:~/.jupyter/jupyter_notebook_config.py)。
  • User 和 Group 应该设置为你当前的用户名和用户组。

2. 重新加载 systemd 并启动服务

保存文件并退出编辑器后,你需要重新加载 systemd 配置,并启动 Jupyter Notebook 服务。

sudo systemctl daemon-reload
sudo systemctl enable jupyter-notebook.service
sudo systemctl start jupyter-notebook.service
  • daemon-reload 会重新加载服务文件。
  • enable 会使服务在系统启动时自动启动。
  • start 会立即启动服务。

3. 检查服务状态

你可以通过以下命令查看服务的状态:

sudo systemctl status jupyter-notebook.service

4. 停止或重启服务

如果你需要停止或重启 Jupyter Notebook 服务,可以使用以下命令:

sudo systemctl stop jupyter-notebook.service
sudo systemctl restart jupyter-notebook.service

5. 访问 Jupyter Notebook

现在你可以在浏览器中通过指定的 IP 地址和端口(例如,http://<your_server_ip>:8888)访问远程的 Jupyter Notebook。如果你需要在配置文件中设置密码或认证,记得在 jupyter_notebook_config.py 配置文件中进行相应设置。

这样,Jupyter Notebook 就可以作为后台服务运行,不会随着会话的结束而中止。

posted on 2025-04-03 22:15  朝朝暮Mu  阅读(133)  评论(0)    收藏  举报