您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
马哥linux运维学习笔记-sudo详解
发布时间:2018-11-17 14:37:01编辑:雪饮阅读()
以管理员身份添加tom用户
[xy@localhost ~]$ sudo /usr/sbin/useradd tom
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 xy:
xy is not in the sudoers file. This incident will be reported.
解析:
(1)管理员身份
这里sudo命令后直接执行了useradd命令,并没有指定是以管理员的身份去执行,但确实是以管理员身份去执行的,sudo命令当没有指定所属身份时默认就是管理员身份。
(2)关于认证
可以看到这里要求输入密码,而且密码就是你当前用户密码而不是你的目标身份密码。sudo在你没有输入过密码时会让你输入密码,密码认证有效期默认为5分钟,在这5分钟内一切sudo操作都不用密码,超过后则又需要输入密码。
(3)关于sudoers
我们可以看到上面命令执行报错,上面的错误是说useradd以管理员身份添加用户的这项操作并没有授权给当前用户,若要授权该操作就需要在sudoers中进行配置。
为xy用户配置权限
root用户执行visudo,核心部分如下
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
xy ALL=(root) /usr/sbin/useradd,/usr/sbin/usermod
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
然后我们重新用xy用户执行之前没有执行成功的命令
[xy@localhost ~]$ sudo /usr/sbin/useradd tom
[sudo] password for xy:
解析:
(1)visudo:visudo其实就是打开了sudoers配置文件,但是不建议直接用vi进行编辑配置,因为vi编辑配置后若有visudo语法错误则在保存时无法给予提示,而visudo则可以。
(2)本例中新增sudoers配置条目
xy:你要给什么用户赋权则就是该用户的用户名
ALL:主机,这里是ALL,则表示主机不限
root:给xy用户授予本条目所包含的操作的身份
/usr/sbin/useradd,/usr/sbin/usermod:被条目所要执行的操作,需要绝对路径,多个则以逗号分隔。
sudo -k
sudo -k命令用于清除sudo的认证信息,执行该命令后则会在新的sudo操作时又需要再次输入密码了。
sudo -l
sudo -l命令用于列出当前用户所有可用sudo命令列表
sudo免密码
在visudo中增加一条目如
tom ALL=(root) NOPASSWD:/usr/sbin/useradd,/usr/sbin/usermod
这里是给tom用户添加了一条和上面xy用户一样的操作命令权限,不同的是该条目权限对于tom用户是不需要认证的。
sudo局部免密码
在visudo中增加一条目如
tom2 ALL=(root) NOPASSWD:/usr/sbin/useradd,PASSWD:/usr/sbin/usermod
和上一条目差不多,不同的是在操作命令时对于是否要认证更细化了,比如这里是useradd命令不需要认证,而usermod命令则需要认证。
sudo操作日志
使用sudo操作命令时会产生sudo操作日志,管理员使用命令如“tail /var/log/secure”就可以查看到sudo操作日志
关键字词:sudo,linux