Jupyter notebook中的sudo问题详解 docker jupyter 使用root 启动
在Jupyter notebook的容器镜像栈中,使用了官方的jovyan账号来进行默认登录,该账号没有root权限,不能执行apt之类的操作安装软件。这是为了保护本机宿主系统,但对于容器就很不方便,而且不是很必要(分使用场景哈)。
开启root权限有两种办法:
- 以root账户运行notebook。
- 设置jovyan的root权限。
-
以root账户运行notebook
Password authentication is disabled for the NB_USER (e.g., jovyan). This choice was made to avoid distributing images with a weak default password that users ~might~ will forget to change before running a container on a publicly accessible host.
You can grant the within-container NB_USER passwordless sudo access by adding -e GRANT_SUDO=yes and --user root to your Docker command line or appropriate container orchestrator config.
For example:docker run -it -e GRANT_SUDO=yes --user root jupyter/minimal-notebook
改变配置参数参考: https://jupyter-notebook.readthedocs.io/en/stable/config_overview.html
这个需要进容器去改变设置,然后重启。jupyter notebook --generate-config --allow-root
建议使用docker commit保存为新的镜像,然后重新运行docker服务。
-
设置jovyan的sudo权限
root进容器:docker exec -u 0 -it mycontainer bash
设置jovyan密码:
passwd jovyan
加入sudoers清单:
visudo
加入:
jovyan ALL=(ALL:ALL) ALL
然后到notebook界面,就可以执行sudo apt update之类的命令了。
-
docker不能使用1024一下端口的原因
Linux / Unix 系统默认规定,低端口号(1-1024),user组是不能访问的,需要root组才行
另一个可能原因:Linux下1024以下端口号,需要root权限所启动的程序才能绑定监听。gpasswd -a bob root //将用户cold加入到root组 gpasswd -d bob root //将用户cold从root组中移出
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。