如何创建sudo用户
三种方法:
- 编辑/etc/sudoer
找到root ALL=(ALL:ALL) ALL 这一行,并添加一行自己的
# User privilege specification
root ALL=(ALL:ALL) ALL
<username> ALL=(ALL) ALL
- 编辑/etc/sudoers.d/<username> 文件
第一种直接修改文件,并不是十分优雅
观察/etc/sudoer 这个文件最后,可以发现
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
让我们来添加一个文件
sudo tee /etc/sudoers.d/username <<< 'username ALL=(ALL) NOPASSWD: ALL'
然后赋予正确的权限
chmod 440 /etc/sudoers.d/username
来,瞅一眼:
image.png
- 使用命令
sudo usermod -a -G sudo <username>
sudo配置文件简单解释
# User privilege specification
root ALL=(ALL:ALL) ALL
# The syntax of lines in the /etc/sudoers file is users hosts=(user:group) commands.
这句配置的意思是root用户 登录任何主机 可以执行所有用户组的所有用户的全部命令
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
这句配置的意思是 admin用户组的用户可以得到root权限
# Allow members of group sudo to execute any command
%sudo ALL=(ALL) NOPASSWD: ALL
这句配置的意思是 sudo用户组的用户可以得到root权限,且不需要输入密码
如何查看一个用户是否在sudo组里?
id <username> 你会发现用户的groups中会有一个sudo组
uid=1003(username) gid=1003(username) groups=1003(username),27(sudo)
如何使用sudo时不使用密码?
根据上文、配置文件中的 NOPASSWD:ALL 会解决这个问题