在linux系统中我们经常要创建一些非root用户来完成一些特定的操作,有时我们需要执行一些root用户才能执行的命令,这时我们需要给该用户设置root的权限
下面将演示如何操作:
1 2 3 4 5 6 7 |
[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. |
当我们执行sudo命令时,会提示输test用户密码,输完后提示,test用户不在sudoer文件里,所以是无法执行sudo命令的。
1 2 3 4 5 |
[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. |
编辑 /etc/sudoers文件
1 2 3 |
[test@smiletian ~]$ su Password: [root@smiletian test]# vi /etc/sudoers |
找到 ## Allow root to run any commands anywhere 这栏,在root下添加 test用户,
1 2 3 |
## Allow root to run any commands anywhere root ALL=(ALL) ALL test ALL=(ALL) ALL |
找到## Same thing without a password一栏,将%wheel ALL=(ALL)前面的#去掉,
1 2 |
## Same thing without a password #%wheel ALL=(ALL) NOPASSWD: ALL |
1 2 |
## Same thing without a password %wheel ALL=(ALL) NOPASSWD: ALL |
wq!保存退出。
可以使用sudo命令,输入一次密码后,不再需要再每次都输入密码即可进行操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[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 |
配置用户具有root权限,方便后期加sudo执行root权限的命令
1 |
[root@master ~]# vim /etc/sudoers |
修改/etc/sudoers文件,在%wheel这行下面添加一行
如下所示:
1 2 3 4 5 6 |
## Allow root to run any commands anywhere root ALL=(ALL) ALL
## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL 用户名 ALL=(ALL) NOPASSWD:ALL |
注意:
用户名这一行不要直接放到root行下面,因为所有用户都属于wheel组,你先配置了用户名具有免密功能,但是程序执行到%wheel行时,该功能又被覆盖回需要密码。
所以用户名要放到%wheel这行下面。