問題:某些場景下,需要修改一些root權限的配置檔案,然後執行一些root權限的指令,不想老是使用root賬号;
解決方案:
第一步:将配置檔案歸到一個特殊的組,如confgp,對組成員開放寫權限,同時在userA的附加組裡面加上confgp;
這一步也可以使用setfacl工具來進行權限控制,參考:https://blog.csdn.net/qq_40809549/article/details/83339570
第二步:為userA配置sudo,使其可使用root權限執行某些指令。
第一步:關于使用者組管理的相關指令
參考:https://www.cnblogs.com/jxhd1/p/6528574.html
所有的使用者帳戶及相關資訊都存儲在
/etc/passwd
檔案中,
/etc/shadow
和
/etc/group
檔案存儲了使用者資訊。
useradd [-g userA -G groupA,groupB...] userA
-g userA userA的主要使用者組,預設建立
-G groupA,groupB... userA的附加使用者組,多個使用者組直接用英文逗号隔離
usermod -a -G groupC userA 将已有使用者userA加到groupC
usermod -g groupD userA 将已有使用者userA的主使用者組修改為groupD
gpasswd -d userA groupD 将userA從groupD中删除,groupD不能是userA的主使用者組
第二步:配置sudo
參考:https://www.linuxidc.com/Linux/2016-08/134451.htm
使用visudo來修改sudo配置檔案:/etc/sudoers
配置基礎規則:授權使用者 主機=指令動作,如:userA ALL=/bin/chown
配置詳解:授權使用者 主機=[(切換到哪些使用者或使用者組)] [是否需要密碼驗證] 指令1,[(切換到哪些使用者或使用者組)] [是否需要密碼驗證] [指令2],[(切換到哪些使用者或使用者組)] [是否需要密碼驗證] [指令3]......
别名定義參考:
Host_Alias FILESERVERS = server1, server2
User_Alias ADMINS = userA,userB
Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig
ADMINS FILESERVERS=(root) NOPASSWD:SERVICES
sudo配置示例:
visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
ooxx ALL=(ALL) NOPASSWD:SERVICES
配置檔案添加:/etc/sudoers.d/下新增檔案ooxx
ooxx ALL=(root) NOPASSWD:/sbin/service nginx start,PASSWD:/sbin/service nginx stop
ooxx ALL=(root) NOPASSWD:/sbin/service nginx reload
如此,即可在ooxx使用者下使用sudo service nginx reload等指令