Debian、CentOS、Rocky等Linux系统配置普通用户sudo权限

配置 linux 权限是熟悉 linux 所必须的。各大linux发行版的默认软件仓库对各软件包配置了详细而严格的权限限制。如果不能熟悉 linux 的权限控制,就会因此出现很多问题。
直接使用 root 用户管理系统是非常方便的,但也是非常危险的。标准的做法是使用具有 sudo 权限的普通用户管理系统。这里记录一下在 Debian 10 系统下手动配置一个具有 sudo 权限的linux 普通用户的过程。
sudo 是较为通用的软件包,其他的 linux 发行版与 Debian 基本相同,甚至没有任何区别。可以直接参考本文

原文链接:http://blog.ryjer.com/posts/a158220239.html

1、准备工作——安装sudo

sudo 是 super user do 的意思,表示临时借用超级用户(super user)的权限执行(do)一次命令。

但官方的 debian 系统可能默认没有安装 sudo 软件,因而也没有sudo命令的配置文件 /etc/sudoers。其他发行版一般都会默认安装sudo。

Debian 需要切换到 root 用户后用apt-get手动安装 sudo 软件。

apt install sudo

RHEL 系的发行版(CentOS Rocky)则需要切换到 root 用户后用dnf手动安装 sudo ,但通常 RHEL系会预装 sudo

# centos 8 和 rocky 8使用 dnf
dnf install sudo
# centos 7 使用 yum
yum install sudo

接下来,其他用户就可以使用 sudo 命令了。

2、原理

2.1 sudo配置文件—debian10

sudo 命令使用 /etc/sudoers 配置文件管理 用户用户组 的sudo权限。Debian 10 中 sudo 默认的配置文件内容如下:

#
# 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"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification # 用户权限
root    ALL=(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

其中,第20行开始为 用户sudo权限 ,这里为root用户提供了sudo权限(虽然root用户并不需要)。第23行以 % 开始的为 用户组权限,这里创建了 sudo 用户组,该组中的用户将拥有sudo权限。

为了方便管理,我们可以将普通用户添加 sudo 附加用户组,便可以使其拥有 sudo 权限。如果单独为一个个用户添加 sudo 权限会非常难以管理。

2.2 sudo配置文件—Rocky8

rocky8 的sudo是默认安装的,其配置文件与 debian10 有所不同,我刚装完一个rocky8后的配置文件如下

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
## 
## This file must be edited with the 'visudo' command.

## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using 
## wildcards for entire domains) or IP addresses instead.
# Host_Alias     FILESERVERS = fs1, fs2
# Host_Alias     MAILSERVERS = smtp, smtp2

## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname 
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem


## Command Aliases
## These are groups of related commands...

## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

## Services
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

## Updating the locate database
# Cmnd_Alias LOCATE = /usr/bin/updatedb

## Storage
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount

## Delegating permissions
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp 

## Processes
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

## Drivers
# Cmnd_Alias DRIVERS = /sbin/modprobe

# Defaults specification

#
# Refuse to run if unable to disable echo on the tty.
#
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home
Defaults    match_group_by_gid

# Prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. Starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# We enable always_query_group_plugin to restore old behavior.
# Disable this option for new behavior.
Defaults    always_query_group_plugin

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## 	user	MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## 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

## Same thing without a password
# %wheel	ALL=(ALL)	NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

从 107 行可看出,其默认sudo用户组为wheel 而不是 debian 10中的 sudo,而且格式也不相同

查看一下拥有sudo权限的用户 me 的群组看看

~# groups me
me : me wheel

可见确实是 wheel 群组授予了其 sudo 权限

3、添加用户—useradd命令

可以使用 useradd 命令创建新用户,系统会自动创建一个同名组(me用户组)。其格式如下:

useradd 选项 用户名
# 多个选项,用户名要在最后
useradd 选项1 选项2 选项3 选项4 用户名

常用参数:

  • -s 指定用户登录 shell,这里指定为 bash
  • -d 指定一个已经存在的目录为主目录
  • -m 指定并创建主目录(main),这里指定为 /home/me
  • -g 指定用户所属用户组(group)。
  • -G 指定附加用户组
# 添加新用户 me,指定登录shell为 /bin/bash,主目录为 /home/me
useradd -s /bin/bash -m /home/me me

如果你想同时指定不同命的用户组和附加组,可以这样。

useradd -s /bin/bash -g we –G adm,root me

该命令创建了用户 me 。使用 -s 参数指定了其登录 shell 为 /bin/bash;使用 -g 参数指定属于不同名的 we 用户组;并用 -G 参数指定了两个附加组 admroot,指定多个附加组时用逗号 “,” 分割各个附加组名。

如果你想偷下懒,可以使用adduser命令,该命令是useradd的简化版(但可以满足90%的需求)。该命令会自动配置 /home 下的用户同名主目录和默认 shell

adduser me

执行完该命令后,会在配置文件 /etc/passwd 中添加me用户的记录。并在 /etc/group 配置文件中添加一个me用户组记录。在 /etc/shadow 配置文件中记录加密密码(说是密码不太准确)。

4、 修改密码—passwd命令

刚刚创建的新用户没有密码,不太安全。可以使用passwd 命令为其修改密码。以下为更改用户 me 的密码:

passwd me

然后就会提示你输入密码进行修改了

5、 修改账号—usermod命令

添加附加用户组,授予 sudo 权限

修改用户账号就是更改用户的有关属性记录,如用户号、主目录、用户组、登录Shell等。这些记录信息分散在各个配置文件中(/etc/passwd 用户信息, /etc/shadow 加密密码信息, /etc/group 用户组信息),使用usermod 可以统一管理修改这几个配置文件。usermod 命令格式如下:

usermod 选项 用户名

其常用选项参数与 useradd 相同,有些版本的usermod命令还会提供额外功能选项(如修改用户名等):

  • -s 指定用户登录 shell,这里指定为 bash
  • -d 指定一个已经存在的目录为主目录
  • -m 指定并创建主目录(main),这里指定为 /home/me
  • -g 指定用户所属用户组(group)。
  • -a 把用户追加(append)到某些组中,仅与-G选项一起使用
  • -G 指定附加用户组(Group)

注意,usermod 修改用户账户前。需保证该用户下没有任何进程执行,特别注意shell进程(这意味着对应用户需要退出所有登录)。

为普通用户添加 sudo 权限可以通过将对应用户添加到拥有sudo权限的用户组中实现,比如在debian10 中的 sudo 用户组:

# 将me用户 添加到 sudo 用户组
usermod -a -G sudo me
# 你要是比较闲的话,再添加一个 adm 伪用户组,注意这不是必须的
usermod -aG adm me

或者说 rocky8 中的 wheel 群组

# 将me用户 添加到 wheel 用户组
usermod -a -G wheel me 

执行完后,使用 groups命令检查

groups me

该命令会显示 me 用户所属的多个用户组(可能有很多),debian10中应该有 sudo 和 adm,rocky8中应该有 wheel

6、删除用户——userdel命令

与 useradd 命令向对应,有一个 userdel 命令用于删除用户。其使用格式如下:

userdel 选项 用户名

常用的选项参数是 -r ,这是递归的意思。表示将该用户的所有的配置信息(主要是/etc/passwd, /etc/shadow, /etc/group等)以及用户主目录全部删除。这基本上已经是算是清除干净了。

7、 参考资料

菜鸟教程用户管理

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值