假如我们要给系统添加一个用户,名字为azathoth,密码为Azathoth2021。同时该用户需要有sudo权限,可以使用下述方法:
[root@localhost ~]# useradd azathoth
[root@localhost ~]# passwd azathoth
Enter new UNIX password: //在这里输入要修改的密码,即 Azathoth2021 ,密码不会显示。
Retype new UNIX password: //在这里输入第二次密码,重复上边的
passwd: password updated successfully
[root@localhost ~]# ls -l /etc/sudoers
-r–r—–. 1 root root 1145 Oct 6 02:57 /etc/sudoers //这里的权限很重要,是440
[root@localhost ~]# chmod 777 /etc/sudoers
[root@localhost ~]# vi /etc/sudoers
## Sudoers allows particular users to run various commands as
……
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
azathoth ALL=(ALL) ALL //添加这一行并保存
……
[root@localhost ~]# chmod 440 /etc/sudoers
[root@localhost ~]# su azathoth
[azathoth@localhost root]# sudo ls
[sudo] password for azathoth:
[azathoth@localhost root]#
至此,已经成功添加了一个用户名为azathoth,密码为指定密码且可以执行sudo命令的用户。值得一提的是这样的用户可以在不知道root密码的情况下使用sudo su切换到root用户,并且更改其他用户的密码。