一,檢查服務器是否已安裝sudo的rpm包?
1,查詢rpm包列表
[root@yjweb ~]# rpm -qa | grep sudo libsss_sudo-2.0.0-43.el8_0.3.x86_64 sudo-1.8.25p1-4.el8.x86_64
2,如未安裝,執行下面的命令安裝:
[root@yjweb ~]# yum install sudo
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,在centos8上面添加sudoer用戶的兩種方法:
1,把用戶賬號添加到wheel組 2, 把用戶賬號添加到sudoers文件
三,新建用戶webop
1,添加用戶webop
[root@yjweb ~]# groupadd webop [root@yjweb ~]# useradd -g webop webop [root@yjweb ~]# ls /home/webop/ [root@yjweb ~]# grep webop /etc/passwd webop:x:1000:1000::/home/webop:/bin/bash [root@yjweb ~]# passwd webop
2,用webop通過ssh登錄后,測試sudo
[webop@yjweb ~]$ sudo -i 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 webop: webop is not in the sudoers file. This incident will be reported.
注意:因為webop沒有被添加成為sudoer,所以系統給出報錯
四,把用戶webop添加wheel組,再次重新嘗試sudo
1,把webop用戶添加到wheel組
[root@yjweb ~]# usermod -aG wheel webop
關於參數-aG
-a, --append
Add the user to the supplementary group(s). Use only with the -G option.
可以看到-a參數作用是:添加用戶到基本的組,僅和 -G選項一起使用
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]] A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma,
with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. If the user is currently a member of a group which is not listed, the user will be removed from the group.
This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.
-G參數指定用戶所屬的group
注意它指定group列表時是用逗號隔開
如果用戶當前屬於未被列出的組的成員,則用戶會被從所屬的那個group中移出。
如果搭配 -a選項,則僅會被添加,不會有移除的情況
2,查看當前用戶所屬的group
注意:修改用戶所屬的組之后,需要logout后再重新登錄,才能看到效果
[webop@yjweb ~]$ groups webop wheel
說明:可以看到 webop被添加到了wheel組
3,測試sudo
[webop@yjweb ~]$ sudo -i [sudo] password for webop: [root@yjweb ~]#
說明:成功的sudo到了root賬戶
五,為什么用戶添加到wheel組后,就成為了sudoer?
[root@yjweb ~]# grep wheel /etc/sudoers ## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL
說明:可以看到 wheel用戶組是被配置為運行所有命令的sudoer
六, 把用戶webop2添加/etc/sudoers文件,再次重新嘗試sudo
說明;新建一個用戶webop2,然后把用戶添加到/etc/sudoers文件
1,用戶添加到/etc/sudoers文件
[root@database2 ~]# visudo
添加一行:
webop2 ALL=(ALL) ALL
說明:為什么要用 visudo?
查看/etc/sudoers的用戶權限:
[root@database2 ~]# ll /etc/sudoers -r--r----- 1 root root 4003 Mar 26 2015 /etc/sudoers
可以看到用戶的權限是440,帶來的問題就是它是一個只讀的文件,
編輯它時需要先添加寫權限,編輯完成后再改為只讀,
這個過程很不方便 ,
而使用visudo則不存在這個問題
2, /etc/sudoers 文件中的命令格式說明:
<user> ALL=(ALL:ALL) NOPASSWD:ALL
說明:
<user> 用戶名,如果前面加%則表示是一個group ALL=(ALL:ALL) 三個ALL分別是: host 用戶 組 NOPASSWD:ALL 執行的命令,ALL表示所有命令 NOPASSWD 表示系統不詢問密碼
七,看一下只允許指定用戶執行指定命令的sudo例子:
[root@webserver1 cron]# grep mkdirchmod /etc/sudoers laoliu ALL=(ALL) NOPASSWD:/usr/local/cmd/tools/mkdirchmod.sh
說明:允許laoliu這個用戶sudo執行mkdirchmod.sh這個腳本,系統不詢問密碼
八,查看centos的版本:
[root@yjweb ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)