(root) PASSWD: /bin/id, !/bin/w
sudo -u <用户名> <命令>
, 将允许当前用户,提权到<用户名>的身份,再执行后面的<命令>
[root@yxb ~]# sudo -u user1 id
uid=1000(user1) gid=1000(user1) 组=1000(user1),10(wheel) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
通过useradd
添加的用户,并不具备sudo
权限。在ubuntu/centos
等系统下, 需要将用户加入admin
组或者wheel
组或者sudo
组。
[test@yxb ~]$ sudo vim /etc/passwd
[sudo] password for test:
test is not in the sudoers file. This incident will be reported.
以root
用户身份执行如下命令, 将用户加入wheel/admin/sudo
组:
usermod -a -G wheel <用户名>
sudoer文件
sudoers
文件主要有三部分组成:
-
sudoers的默认配置(default),主要设置sudo的一些缺省值
-
alias(别名),主要有Host_Alias|Runas_Alias|User_Alias|Cmnd_Alias。
-
安全策略(规则定义)。
安全策略
root ALL=(ALL) ALL
我们来说一下这一行的配置的意思
“不以%
号开头的表示"将要授权的用户”,比如例子中的root;以%
号开头的表示"将要授权的组", 比如例子中的%wheel
组 和 %sudo
组
root
表示该用户root
可以使用sudo
命令,第一个ALL
指的是网络中的主机(可以是主机名也可以是ip地址),它指明用户可以在此主机上执行后面命令;第二个括号里的ALL
是指目标用户,也就是以谁的身份去执行命令。最后一个ALL
是指命令路径。
user1 localhost=(root) /bin/kill,/bin/id
表示user1用户可以在本地以root的身份去执行kill命令,多条命令用逗号分隔
注意: 命令必须是完整的路径
NOPASSWD
后面带有冒号:。表示执行sudo
时可以不需要输入密码。
test ALL=(ALL) NOPASSWD: /bin/useradd
用户可以执行passwd
程序,但是不能修改root密码,在命令前面加上叹号表示不能执行该程序。
user01 localhost=(root) NOPASSWD:/usr/bin/passwd,!/usr/bin/passwd root
用户可以在本机上无密码执行/usr/local/bin/python,有密码执行kill命令
user01 localhost=(root) NOPASSWD:/bin/more PASSWD:/bin/less
用户别名允许root将多个用户整理成一组中,并按组来分配目标用户的权限。这部分是可选的,定义的时候是User_Alias group1=user1,user2
使用的时候是%group1
。我们也可以直接使用/etc/groups
中定义的组而不用自己设置的别名。
User_Alias group1 = user1, user2
命令别名就是将一部分命令进行归类,方便系统管理员有效分配权限。
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
实例
–
针对MySQL
数据库的设置,让test组中的test用户具备/etc/init.d/mysqld
的权限
[root@test ~]# groupadd test
[root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test
[root@test ~]# passwd test
[root@test ~]# visudo
test ALL=(ALL) NOPASSWD: /etc/init.d/mysqld
test ALL=(ALL) /etc/init.d/mysqld
#start mysql
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld start
#stop mysql
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld stop
针对tomcat
的设置,让test组中的test用户具备tomcat操作的权限
[root@test ~]# groupadd test
[root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test
[root@test ~]# passwd test
sh
[root@test ~]# visudo
test ALL=(ALL) /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
test ALL=(ALL) NOPASSWD: /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
[root@test ~]# vim /usr/local/tomcat/bin/catalina.sh
JDK
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
#start tomcat
[root@test ~]# su test
[test@test ~]$ sudo /usr/local/tomcat/bin/startup.sh
[test@test ~]$ ss -ntlup | grep Java
[test@test ~]$ curl -I http://localhost:8080
#stop tomcat
[root@test ~]# su test
[test@test ~]$ sudo /usr/local/tomcat/bin/shutdown.sh
/etc/sudoers配置文件详解
[root@test ~]# cat /etc/sudoers
Sudoers allows particular users to run various commands as
the root user, without needing the root password.
##该文件允许特定用户像root用户一样使用各种各样的命令,而不需要root用户的密码
Examples are provided at the bottom of the file for collections
of related commands, which can then be delegated out to particular
users or groups.
在文件的底部提供了很多相关命令的示例以供选择,这些示例都可以被特定用户或
## 用户组所使用
This file must be edited with the ‘visudo’ command.
该文件必须使用"visudo"命令编辑
Host Aliases
#主机别名
Groups of machines. You may prefer to use hostnames (perhap using
wildcards for entire domains) or IP addresses instead.
对于一组服务器,你可能会更喜欢使用主机名(可能是全域名的通配符)
或IP地址代替,这时可以配置主机别名
Host_Alias FILESERVERS = fs1, fs2
Host_Alias MAILSERVERS = smtp, smtp2
User Aliases
#用户别名
These aren’t often necessary, as you can use regular groups
(ie, from files, LDAP, NIS, etc) in this file - just use %groupname
rather than USERALIAS
这并不很常用,因为你可以通过使用组来代替一组用户的别名
User_Alias ADMINS = jsmith, mikem
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
(ie, from files, LDAP, NIS, etc) in this file - just use %groupname
rather than USERALIAS
这并不很常用,因为你可以通过使用组来代替一组用户的别名
User_Alias ADMINS = jsmith, mikem
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
[外链图片转存中…(img-trwXY28r-1725898090993)]
[外链图片转存中…(img-h2F37o9x-1725898090994)]
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新