admin管理员组文章数量:1666723
通过su命令我们可以非常方便地切换到另一个用户,去行使其他用户的权力(比如root用户),但前提是我们要知道其他用户的密码,这在很多实际环境中是不被允许的。
那我们就有一个折中的办法,使用sudo命令来提升权限,不过需要事先由管理员授权,指定允许哪些用户以root用户的身份来执行哪些命令。
先做个测试,我们使用test1用户来用yum安装apache,可以看到test用户是没有权限使用命令的。
[test1@promote ~]$ sudo yum -y install httpd # 安装apache
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 test1:
test1 is not in the sudoers file. This incident will be reported.
下面我们来通过sudo命令来使test用户获得使用yum命令的权限
-
修改配置文件/etc/sudoers
该文件的授权记录的基本配置格式为:
user MACHINE=COMMANDS
user为用户名,也可以是“%组名”的形式给组用户授权
MACHINE为主机名称,当多个主机共用一个配置文件时,主机名填ALL比较方便
COMMANDS为命令,注意需要填绝对路径,多个命令之间要用逗号隔开
[root@promote ~]# vim /etc/sudoers # 在文件结尾加上: test1 ALL=/usr/bin/yum # 退出vim编辑器时,需要用“wq!”,因为该文件的默认权限为440
-
使用test1用户使用yum命令安装apache
[test1@promote ~]$ sudo yum -y install httpd # 再次尝试安装apache
[sudo] password for test1:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution
……省略部分信息……
Installed:
httpd.x86_64 0:2.4.6-90.el7.centos
Complete! # 成功安装apache
补充:
在/etc/sudoers文件中还可以这样写:
User_Alias OPERATORS=test1,test2,test3 # 将test[1-3]用户设置为集合
Host_Alias MAILSVRS=smtp,pop # 将主机名设置为集合
Cmd_Alias PKGTOOLS=/bin/rpm,/usr/bin/yum # 将命令设置为集合
OPERATORS MAILSVRS=PKGTOOLS # test[1-3]在smtp、pop主机上有rpm和yum命令
可以执行sudo -l命令来查看用户获得了哪些权限
[test1@promote ~]$ sudo -l
Matching Defaults entries for test1 on promote:
!visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR
LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT
LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User test1 may run the following commands on promote:
(root) /usr/bin/yum
版权声明:本文标题:sudo命令的授权 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1730076209a1221807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论