ubuntu16.04 正确编辑 /etc/sudoers 文件,禁止普通用户使用超级权限
/etc/sudoers
编辑权限问题
请先切换到超级用户(root),将 /etc/sudoers
修改为可编辑。
ls -lah /etc/sudoers
chmod 777 /etc/sudoers
将 /etc/sudoers
修改回只读模式。
chmod 440 /etc/sudoers
ls -lah /etc/sudoers
如何编辑 /etc/sudoers
User Privilege Lines
root
ALL=(ALL:ALL) ALL
The first field indicates the username that the rule will apply to (root).- demo
ALL
=(ALL:ALL) ALL
The first “ALL” indicates that this rule applies to all hosts. - demo ALL=(
ALL
:ALL) ALL
This “ALL” indicates that the root user can run commands as all users. - demo ALL=(ALL:
ALL
) ALL
This “ALL” indicates that the root user can run commands as all groups. - demo ALL=(ALL:ALL)
ALL
The last “ALL” indicates these rules apply to all commands.
This means that our root user can run any command using sudo, as long as they provide their password.
禁止普通用户使用 sudo
, su
命令
这里约定普通用户的用户名是 www-data
,其用户组只有 www-data
。
那么在 /etc/sudoers
中添加如下的规则,可以禁止用户使用 sudo
命令
www-data ALL=(www-data:www-data) /bin/*,/usr/bin/*,/sbin/*, /usr/sbin/*,!/usr/bin/sudo,!/bin/su,!/usr/bin/passwd,!/usr/bin/chattr -* /etc/sudoers
普通用户禁用 su 的话。这里需要注意的是 ubuntu 16.04 禁止普通用户使用 su
命令,需要修改 /etc/pam.d/su
,找到类似 #auth required pam_wheel.so
,取消其注释,并在其后添加允许使用 su
的用户组 group=sudo
。
最终的修改如下,修改后会立刻生效:
auth required pam_wheel.so group=sudo
可以不用在 /etc/login.defs
追加 SU_WHEEL_ONLY yes
,如果追加,可能会报 SU Module Unknown Error
。可能是由于 ubuntu 上没有 wheel 组,有可能会出现不兼容的情况,例如这位老哥 pam.d/su to restrict su access. SU Module Unknown Error。
允许普通用户使用 sudo 命令
有如下几种方法,可以搜索其他博客进行参考:
- 把普通用户添加到
sudo
组别 - 在
/etc/sudoers
添加规则,例如:www-data ALL=(ALL:ALL) ALL