Linux下配置sudo权限

sudo为用户提供了一种机制, 使得普通用户在不知道root密码的情况下, 可以执行root用户才能执行的命令. 在执行sudo时需要输入当前用户的密码, 认证通过后就可以执行命令, 但是每次需要sudo时, 都需要输入密码, 很麻烦.

为新用户配置sudo权限

1.以root登入系统:
2.添加新用户useradd , centos和rhel下也可以用adduser, 相同adduser是useradd的符号链接.

# useradd test
# 给新用户设置密码
# passwd test
更改用户 centos 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

3.编辑/etc/sudoers文件, 可以用vim直接编辑但是不安全.
# visudo
找到下面两行注释掉(移除第二行前面的#), 然后保存退出:
## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
如果想运行sudo时不需要输入密码, 找到下面的两行, 同样将第二行注释掉:
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
修改后
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL

4.添加test用户到wheel组(group)
usermod -aG wheel test
5.测试sudo
切换到test用户

su - test
[test@localhost ~]$ sudo whoami

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for centos: 
root

结果显示是root

Note: - 表示完全切换到test用户下, 包括环境变量等.
第一次运行sudo时, 会出现下面的警告, 以后再执行就不会了.
sudo在运行时, 就好像root自己在执行一样.

6.查看当前用户组.

[test@localhost ~]$ sudo id -u
0
[test@localhost ~]$ sudo id -g
0

给现有的用户添加sudo权限

如果/etc/sudoers文件已经修改过的话, 直接将用户添加到wheel组中就可以了.
usermod -aG wheel 用户名
或者在/etc/sudoers后面加上下面一行
echo "test ALL=(ALL) NOPASSWD: ALL">>/etc/sudoers或者echo "test ALL=(ALL) NOPASSWD: ALL">/etc/sudoers.d/test

posted @ 2017-06-15 18:43  一忘如故  阅读(1646)  评论(0编辑  收藏  举报