CentOS 普通用戶提升root權限


1、sudo命令可以使普通用戶具備root用戶的權限,使用前,需要先配置/etc/sudoers文件。

#sudoers文件是只讀,一般情況下都是用visudo來修改,visudo也一定程度上可以保證修改sudoers文件是安全的,避免同時修改的沖突情況
[root@bigdata-senior01 ~]# ll /etc/sudoers
-r--r----- 1 root root 4093 11月 28 22:35 /etc/sudoers


[root@bigdata-senior01 ~]# visudo

#找到這行
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL 

然后可以添加其他用戶
hadoop  ALL=(root) NOPASSWD:ALL
xu.dm   ALL=(root)      ALL     
es      ALL=(ALL)       ALL    
amy    ALL=(root) /usr/sbin/useradd        
#配置字段說明
root ALL=(ALL) ALL
第一個字段:root為能使用sudo命令的用戶;
第二個字段:第一個ALL為允許使用sudo的主機,第二個括號里的ALL為使用sudo后以什么身份來執行命令;
第三個字:ALL為以sudo命令允許執行的命令;

解釋: amy ALL
=(root) /usr/sbin/useradd,/usr/sbin/userdel 表示允許amy用戶從任何主機登錄,以root的身份執行/usr/sbin/useradd和userdel命令。

NOPASSWD:表示使用sudo提升權限的時候不用輸入密碼

注意:命令必須是全路徑

2、sudoers的其他配置

2.1 非同組的多個用戶配置

#對於不同需求的用戶:可以按照上面的方法依次增加多行,每行對應一個用戶。
#對於相同需求的多個用戶,可以定義別名,統一用戶
User_Alias UUU=user1,user2……             #定義用戶別名;

## 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
User_Alisa s1=user1,user2,user3,user4 //s1代表四個用戶

... ...

#像上面的配置一樣加入,用戶列用別名就可以
s1    ALL=(root)    /usr/sbin/useradd 

2.2 類似的別名情況

可以有主機別名,命令別名等等,在/etc/sudoers文件里都有例子

2.3 對於組的配置,多了一個百分號%

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

允許wheel組的用戶執行所有命令。


## 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

 3、其他辦法

修改 /etc/passwd 文件,把用戶UID修改成0就可以。UID=0,代表着root權限,而不是用戶名root代表root權限。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM