一、前言

    在 Linux 系统中,sudo 是一个不可或缺的工具,它通过允许授权用户以超级用户或其他用户身份执行特定命令,有效解决了权限不足、多用户权限分配以及系统安全风险等问题。它不仅提升了系统的安全性,还优化了用户管理效率,同时通过日志记录功能,为系统管理员提供了审计和追踪的便利

二、普通用户配置sudo权限

1.创建普通用户msa

useradd  msa   

2.编辑sudouser的配置文件

vim  /etc/sudoers

在文件中仿照root部分,粘贴出如下


##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
msa     ALL=(ALL)       ALL       ##仿照上面root

## 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
Defaults logfile=/var/log/sudo.log

3.注释/etc/sudoers 中的关于默认权限认证的配置,不然最后配置完sudo会让你用root密码验证

Defaults targetpw   或  Defaults rootpw

4.使用wq!强制保存,否则无法保存,该文件是只读的

5.验证使用

su  msa   切换到普通用msa

sudo  cd /root/   #就会提示需要msa的密码执行

 三.结尾

  1. 总结:sudo 解决了普通用户权限不足和频繁切换 root 的困扰,通过临时提权执行命令,提供灵活安全的权限管理,避免直接使用 root 账户的风险。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐