在linux系统中我们经常要创建一些非root用户来完成一些特定的操作,有时我们需要执行一些root用户才能执行的命令,这时我们需要给该用户设置root的权限,下面将演示如何操作:
1、首先创建一个test用户,
[root@smiletian ~]# groupadd -g 1234 tian
[root@smiletian ~]# useradd -u 1234 -g tian test
[root@smiletian ~]# passwd test
Changing password for user test.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
2、切换到test用户下,当我们执行sudo命令时,会提示输test用户密码,输完后提示,test用户不在sudoer文件里,所以是无法执行sudo命令的。
[root@smiletian test]# su - test
Last login: Sun Sep 18 23:35:57 PDT 2022 on pts/2
[test@smiletian ~]$ sudo mkdir abc
[sudo] password for test:
test is not in the sudoers file. This incident will be reported.
3、切换到root用户,编辑 /etc/sudoers文件
[test@smiletian ~]$ su
Password:
[root@smiletian test]# vi /etc/sudoers
找到 ## Allow root to run any commands anywhere 这栏,在root下添加 test用户,
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
test ALL=(ALL) ALL
找到## Same thing without a password一栏,将%wheel ALL=(ALL)前面的#去掉,
更改前
## Same thing without a password
#%wheel ALL=(ALL) NOPASSWD: ALL
更改后:
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
wq!保存退出。
4、切换到test用户下,可以使用sudo命令,输入一次密码后,不再需要再每次都输入密码即可进行操作。
[root@smiletian test]# su - test
Last login: Sun Sep 18 23:54:07 PDT 2022 on pts/2
[test@smiletian ~]$ su
Password:
[root@smiletian test]# vi /etc/sudoers
[root@smiletian test]# su - test
Last login: Mon Sep 19 00:08:24 PDT 2022 on pts/2
[test@smiletian ~]$ sudo mkdir abc
[sudo] password for test:
[test@smiletian ~]$ sudo mkdir abd
[test@smiletian ~]$ sudo mkdir abf
[test@smiletian ~]$ ll
total 0
drwxr-xr-x. 2 root root 6 Sep 19 00:20 abc
drwxr-xr-x. 2 root root 6 Sep 19 00:20 abd
drwxr-xr-x. 2 root root 6 Sep 19 00:21 abf