解决docker镜像(centos系统)中无sudo命令问题
问题
最新在协助同事在docker中安装app时,提供的container使用了centos基础镜像(某些功能阉割版):
[root@server111-111 admin]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 75835a67d134 6 months ago 200 MB
经检查发现普通用户需要执行一些yum 安装命令,提示权限不足,如下:
[Errno 13] Permission denied: ‘/var/lib/rpm/__db.002’
You need to be root to perform this command.
解决方案步骤
1. 查看docker container os版本
给docker容器安装lsb_release后,检查centos版本如下:
yum -y install net-tools vim redhat-release
[root@container /]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.5.1804 (Core) Release: 7.5.1804 Codename: Core
原生centos、ubuntu中提升普通用户为root权限,自然能想到是通过sudo方式,然而输入sudo却报命令不存在,看来docker中官方centos镜像已是精简到不能在精简的核心基础OS。
2. 安装sudo服务
安装sudo服务:
yum -y install sudo
3. sudoers list中添加普通用户
添加普通用户到sudoers列表中:
切换到root下:
Step ①、为/etc/sudoers文件增加写权限(原为440)
chmod 640 /etc/sudoers
Step ②、添加用户到sudoers list中(最后一行),同时修改属性:
vim /etc/sudoers 或使用 visudo命令修改:
## Allow root to run any commands anywhere root ALL=(ALL) ALL ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL %docker_backuper ALL=(ALL) NOPASSWD: ALL
Step ③、/etc/sudoers文件改回原权限(440)
chmod 440 /etc/sudoers
4. 校验
功能可行性检查:
在普通用户docker_backuper下,执行
sudo su
命令,看能否切换回root。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
docker安装Jenkins配置Gitee SSH密钥踩坑解决
这篇文章主要为大家介绍了docker安装Jenkins配置Gitee SSH密钥踩坑解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08
Docker安装Nginx并修改Nginx配置文件的方法详解
这篇文章主要给大家介绍了关于Docker安装Nginx并修改Nginx配置文件的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2023-02-02
Docker-compose搭建Redis集群(Sentinel)的实现
本文主要介绍了Docker-compose搭建Redis集群(Sentinel)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-07-07
docker安装OpenWebUI报错500的原因及解决方法
Open WebUI是一个可扩展、功能丰富且用户友好的自托管WebUI,旨在完全离线操作,这篇文章主要介绍了docker安装OpenWebUI报错500的原因及解决方法,需要的朋友可以参考下2025-07-07


最新评论