温馨提示×

centos怎样配置jupyter

小樊
45
2026-09-24 13:54:23
栏目: 智能运维

下面以 CentOS 7 / 8 为例,说明如何在 CentOS 上安装并配置 Jupyter Notebook / JupyterLab,包括远程访问配置。


一、环境准备

1. 安装 Python 和 pip

CentOS 一般自带 Python 2,需要 Python 3:

sudo yum install -y python3 python3-pip

验证:

python3 --version
pip3 --version

二、安装 Jupyter

方式一:直接安装 JupyterLab(推荐)

pip3 install jupyterlab

方式二:安装经典 Notebook

pip3 install notebook

三、生成 Jupyter 配置文件

jupyter notebook --generate-config

默认生成路径:

~/.jupyter/jupyter_notebook_config.py

四、设置密码(推荐)

jupyter notebook password

输入密码后,会生成:

~/.jupyter/jupyter_server_config.json

五、配置远程访问

编辑配置文件:

vi ~/.jupyter/jupyter_notebook_config.py

添加或修改以下内容:

c.ServerApp.ip = '0.0.0.0'        # 允许所有 IP 访问
c.ServerApp.open_browser = False  # 不自动打开浏览器
c.ServerApp.port = 8888           # 端口
c.ServerApp.root_dir = '/home/youruser/jupyter'  # 工作目录

如果是老版本(notebook < 6.0):

c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888

六、防火墙设置

sudo firewall-cmd --permanent --add-port=8888/tcp
sudo firewall-cmd --reload

如果是云服务器,还需在安全组放行 8888 端口。


七、启动 Jupyter

前台启动

jupyter lab
# 或
jupyter notebook

后台启动

nohup jupyter lab > jupyter.log 2>&1 &

八、使用 systemd 管理(推荐)

创建服务文件:

sudo vi /etc/systemd/system/jupyter.service

内容示例:

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

[Service]
User=youruser
WorkingDirectory=/home/youruser
ExecStart=/usr/local/bin/jupyter lab
Restart=always

[Install]
WantedBy=multi-user.target

启用并启动:

sudo systemctl daemon-reload
sudo systemctl enable jupyter
sudo systemctl start jupyter

九、使用 Nginx + 域名(可选)

如需通过 http://jupyter.example.com 访问,可配置 Nginx 反向代理(可后续补充)。


十、常见问题

  • 无法访问:检查防火墙、安全组、IP 配置
  • 权限问题:不要使用 root 直接运行
  • 版本问题:CentOS 默认 Python 较旧,建议使用 Python 3.8+

如果你告诉我:

  • CentOS 版本
  • 是否需要远程访问
  • 是否用 root / 普通用户

我可以给你一份完全适配的配置方案。

0 踩