Linux系统sudo权限管理和Docker权限管理

引言

本博客主要讲解如何在Linux系统里进行用户和用户组的sudo权限管理。sudo权限的重要性,此处不赘述。

配置文件

配置sudo权限的文件一般位于/etc/sudoers,root模式下,使用

cat /etc/sudoers

就能查看它的内容:
一般为

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults	use_pty

# This preserves proxy settings from user environments of root
# equivalent users (group sudo)
#Defaults:%sudo env_keep += "http_proxy https_proxy ftp_proxy all_proxy no_proxy"

# This allows running arbitrary commands, but so does ALL, and it means
# different sudoers have their choice of editor respected.
#Defaults:%sudo env_keep += "EDITOR"

# Completely harmless preservation of a user preference.
#Defaults:%sudo env_keep += "GREP_COLOR"

# While you shouldn't normally run git as root, you need to with etckeeper
#Defaults:%sudo env_keep += "GIT_AUTHOR_* GIT_COMMITTER_*"

# Per-user preferences; root won't have sensible values for them.
#Defaults:%sudo env_keep += "EMAIL DEBEMAIL DEBFULLNAME"

# "sudo scp" or "sudo rsync" should be able to use your SSH agent.
#Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"

# Ditto for GPG agent
#Defaults:%sudo env_keep += "GPG_AGENT_INFO"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root	ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "@include" directives:

@includedir /etc/sudoers.d

该文件通常为只读文件,一般需要在读写前后进行权限管理;
在写之前,执行以下命令授予写权限

chmod u+w /etc/sudoers

然后以vim编辑为例,执行

vim /etc/sudoers

在编辑完配置文件以后,再撤销写权限

chmod u-w /etc/sudoers

用户权限管理

如果只是授予某一用户sudo权限,则修改

# User privilege specification
root	ALL=(ALL:ALL) ALL

即可,再其后追加

# User privilege specification
root	ALL=(ALL:ALL) ALL
user_name	ALL=(ALL:ALL) ALL

在这种情况下,用户user_name获得sudo权限,但执行sudo命令需要输入密码确认,因此如果需要免除密码输入,可写成

# User privilege specification
root	ALL=(ALL:ALL) ALL
user_name	ALL=(ALL:ALL) NOPASSWD:ALL

用户组权限管理

授予组sudo权限

如果需要授予大量用户sudo权限,则逐个添加sudo权限,不仅麻烦繁琐,给管理造成不便,因此引入用户组权限管理;
修改用户组sudo权限管理
首先,新建一个用户组,或者复用现有的用户组
新建命令如下:

addgroup group_name

或者

groupadd group_name

修改

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

即可,再其后追加

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL
%group_name  ALL=(ALL:ALL) ALL

在这种情况下,用户组group_name的成员获得sudo权限,但执行sudo命令需要输入密码确认,因此如果需要免除密码输入,可写成

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL
%group_name   ALL=(ALL:ALL) NOPASSWD:ALL

添加用户

当用户组group_name获得sudo权限以后,将指定用户user_name添加到组group_name中,即可授予该用户sudo权限,执行命令

usermod -a -G group_name user_name 

这条命令会将user_name 添加到组group_name,而不会改变user_name 之前的所属组关系,换句话说添加组的操作,可以理解为

group_name.append(user_name)

测试

查看

当sudo权限授予完毕后,查看用户组group_name组内有哪些成员,可以执行

cat /etc/group | grep group_name 

查看用户user_name属于哪些组,可以执行

cat /etc/group | grep user_name 

执行

当sudo权限授予完毕后,测试用户是否获得了用户权限,可执行软件更新进行测试:
Ubuntu系统下执行

sudo apt update

或者

sudo apt-get update

Centos系统下执行

sudo yum update