问题
最新在协助同事在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。