CentOS7下sudo的配置說明

一、權限簡單回顧

1.1、基本權限

關於基礎命令,可以參考:
http://www.gnu.org/software/coreutils/manual/coreutils.html
在這裏插入圖片描述

http://www.gnu.org/software/coreutils/manual/coreutils.html#index-long-ls-format
這裏ls -l輸出的長格式進行說明。先回顧一下ls -l輸出文件的含義。

-rw-r--r-- 1 root root  784 Dec 22 14:58 z.sh
第一部分:文件類型(the file type)
第二部分:文件權限位(file mode bits)
第三部分:文件硬鏈接數(number of hard links)
第四部分:文件屬主(owner name)
第五部分:文件屬組(group name)
第六部分:文件大小
第七部分:時間戳,默認是修改時間(即modify time,mtime)
第八部分:文件名(the name of file)

第一部分說明:(文件類型,用以下介紹的字符中的一個)
註解:下面每個符號都以引用引用表示,實際表現形式並沒有引號。引號這裏純粹做字符串引用作用;
‘-’:常規文件,普通文件(regular file);
'b':塊設備特殊文件(block file);
'c':字符設備文件(character file);
‘C’:高性能(連續數據的)文件(不常見,不太理解),這裏做字面音譯。原文:high performance (“contiguous data”) file
'd':目錄(文件) (directory (file));
'D':出口 (沒見過,只做音譯).原文:door ,(Solaris 2.5 and up)。Solaris 2.5以及以上系統纔有這種類型的文件;
'l':符號鏈接(文件),軟鏈接(文件)。symbolic link (file)
'M':離線文件,沒見過,只做音譯。原文內容:off-line (“migrated”) file (Cray DMF)
'n':網絡特殊文件(network special file),HP-UX上的文件類型;
'p':有名管道(文件),先進先出(文件).FIFO(first input first output),named fle;
'P':端口(port),Solaris 10 和以上版本纔有;
's':套接字文件(socket file);
'?':其他文件類型;

第二部分:符號權限表示(符號權限表示含義如下)
分爲三段,以上面的rw-rw----爲了,可以看作默認符號'-'表示佔位,其他非‘-’都有其特殊含義。我們就說一個佔位表示一位,一共有9位,
前三位用來標識文件屬主對該文件的權限,中間三位用來標識文件屬組對該文件的權限,最後三位表示其他者(不是文件屬主,也不是
文件屬組的其他用戶都是其他者)對該文件的權限。
r:表示對文件有可讀的權限;
w:表示對文件有可寫的權限;
x:表示對文件有可執行的權限;
-:表示符號佔位,除了讀寫執行之外的權限;
還有可能出現特殊字符:'s','S','t','T'   #這些是特殊權限爲,後邊小結說明。

修改基本權限用到最多的兩個命令就是:chmod和chown。這兩個配合符號權限格式([ugoa…][-+=]perms…[,…])以及
配合數字權限格式(7,6,5,4,3,2,1,0)。默認的數字權限爲有八進制用3位表示(111是八進制的最大數的二進制表示形式,7.所以
可以理解,每3位二進制數可以表示一位八進制的權限位。一般基礎權限位有9位,如果要加上特殊權限位,還要用一位八進制數表示
所以完整意義上的權限位的個數,二進制數量一共有12位)。加上用戶權限模型,進程運行上下文模型等,權限可以算是非常基礎而又
複雜的知識點。具體設置方式可以參考:
http://www.gnu.org/software/coreutils/manual/coreutils.html#Directory-Setuid-and-Setgid

還有一個可以改變GNU或GNU/LINUX文件屬性的命令是:chattr  (change file attributes on a Linux file system)
設置特殊權限,比如涉及到acl,Suid,Sgid,Sticky,Selinux(這個算一種權限機制)等,下面只會簡單提及。

1.2、acl權限

原文:(因爲總結的很概要簡練,所以貼出原文以示尊重,謝謝。)
Following the file mode bits is a single character that specifies whether an alternate access method such as an access control list 
applies to the file. When the character following the file mode bits is a space, there is no alternate access method. When it is a printing
 character, then there is such a method.
GNU ls uses a ‘.’ character to indicate a file with a security context, but no other alternate access method.
A file with any other combination of alternate access methods is marked with a ‘+’ character.

緊跟在文件(權限)模式位後可能會有一個單一的符號用來指明知否文件有使用一個可選擇的諸如訪問控制列表(acl)的訪問方法。如果
這個字符位是一個空格,表示文件並沒有選擇訪問方法。如果有使用,其表示含義說明如下:
如果文件有Selinux權限,GNU的ls程序會顯示一個點號"."來標識,Selinux又叫安全上下文(Security Context);
如果有訪問控制列表權限,會標記一個加號"+";

Selinux不常見,我這裏不舉例子。對acl做舉例說明:
[root@www tmp]# ls -l file 
-rw-r--r-- 1 root root 4 Dec 22 21:53 file
這裏的文件file,文件屬主是root用戶,文件屬組是root組。這裏其他者比如(yanhui這個普通用戶),如果要讓yanhui對這個用戶有寫的權
限,除了修改權限位的權限,以及修改文件屬主或屬組。還可以設置acl權限。設置如下:
setfacl -m u:yanhui:w file

修改前yanhui普通用戶寫入(要使用管理員權限修改):
[root@www tmp]# su - yanhui
[yanhui@www ~]$ cd /tmp/
[yanhui@www tmp]$ ls -l file
-rw-r--r-- 1 root root 4 Dec 22 21:53 file
[yanhui@www tmp]$ cat file 
123
[yanhui@www tmp]$ echo "234" >> file 
-bash: file: Permission denied

修改後yanhui普通用戶正常寫入(要使用管理員權限修改):
說明,如果有加入acl訪問控制權限,如果權限爲有的權限,在acl訪問控制列表沒有,會以訪問控制列表爲準。比如:
第一次修改,只給yanhui訪問控制file文件的權限爲-w-,即只有可寫權限。
[root@www tmp]# setfacl -m u:yanhui:w file
[root@www tmp]# getfacl file 
# file: file
# owner: root
# group: root
user::rw-
user:yanhui:-w-
group::r--
mask::rw-
other::r--
[yanhui@www tmp]$ ls -l file 
-rw-rw-r--+ 1 root root 4 Dec 22 21:53 file    #文件權限模型位後面多了一個加號,請注意。
[yanhui@www tmp]$ echo "234" >> file 
[yanhui@www tmp]$ cat file    #其他者有可讀權限,發現加上acl權限後,這個權限不會生效。
cat: file: Permission denied

第二次修改:
[root@www tmp]# setfacl -m u:yanhui:rw file
[yanhui@www tmp]$ ls -l file 
-rw-rw-r--+ 1 root root 8 Dec 22 21:59 file
[yanhui@www tmp]$ cat file   #acl權限給yanhui對file有讀權限後,纔可以查看。
123
234

1.3、特殊權限

特殊權限爲,有Suid,Sgid以及Sticky。分別含義是:設置uid權限,設置gid權限以及設置粘貼位權限。

功用:當目錄屬組有寫權限,且有SGID權限時,那麼所有屬於此目錄的屬組,且以屬組身份在此目錄中新建文件或目錄時,新文件的屬組不是用戶的基本組,而是此目錄的屬組;

管理文件的SGID權限:
chmod g+|-s FILE…
展示位置:屬組的執行權限位
如果屬組原本有執行權限,顯示爲小寫s;
否則,顯示爲大寫S;

SUID權限用的比較廣一點。比如su和passwd這兩個命令本身就有設置SUID權限:

[yanhui@www ~]$ which su
/bin/su
[yanhui@www ~]$ which passwd
/bin/passwd
[yanhui@www ~]$ ls -l /bin/su /bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd
-rwsr-xr-x. 1 root root 32064 Mar  6  2015 /bin/su

普通用戶可以使用su命令來切換到管理員。可以使用passwd命令來修改自己的密碼。passwd命令如果要修改密碼,就涉及到寫
/etc/passwd和/etc/shadow文件:
-rw-r--r-- 1 root root 1226 Dec 22 21:57 /etc/passwd
---------- 1 root root 1146 Dec 22 21:57 /etc/shadow
默認普通用戶是無法直接寫這兩個文件的。

功用:對於屬組或全局可寫的目錄,組內的所有用戶或系統上的所有用戶對在此目錄中都能創建新文件或刪除所有的已有文件;如果爲此類目錄設置Sticky權限,則每個用戶能創建新文件,且只能刪除自己的文件;
管理文件的Sticky權限:
chmod o+|-t FILE…
展示位置:其它用戶的執行權限位
如果其它用戶原本有執行權限,顯示爲小寫t;
否則,顯示爲大寫T;
應用:/tmp目錄以及/var/tmp目錄就有設置粘貼位。

[yanhui@www ~]$ ls -ld /var/tmp/
drwxrwxrwt. 6 root root 4096 Dec 22 17:55 /var/tmp/
[yanhui@www ~]$ ls -ld /tmp/
drwxrwxrwt. 9 root root 4096 Dec 22 22:05 /tmp/
每個普通用戶都可以在上面這兩個目錄下創建文件,以及刪除自己的文件。普通用戶A正常不能刪除普通用戶B的文件,反之亦然。

1.4、進程安全上下文說明

進程安全上下文說明:
1、進程以某用戶的身份運行; 進程是發起此進程用戶的代理,因此以此用戶的身份和權限完成所有操作;
2、權限匹配模型:
(1) 判斷進程的屬主,是否爲被訪問的文件屬主;如果是,則應用屬主的權限;否則進入第2步;
(2) 判斷進程的屬主,是否屬於被訪問的文件屬組;如果是,則應用屬組的權限;否則進入第3步;
(3) 應用other的權限;

權限:
    r:readable,讀
    w:writable,寫
    x:excutable,執行
    
基本權限相對於文件:
	r:可獲取文件的數據;
	w:可修改文件的數據;
	x:可將此文件發起運行爲進程;

基本權限相對於目錄:
	r:可使用ls命令獲取其下的所有文件列表;
 	w:可修改此目錄下的文件列表;即創建或刪除文件;
	x:可cd至此目錄中,且可使用ls -l來獲取所有文件的詳細屬性信息;

權限模型(基本的9位,ls -l可以看見的屬性)以及從屬關係:
    mode:rwxrwxrwx
    ownership:user,group


權限組合機制:八進制表示0~7,最大爲7,二進制可以爲111,最小爲0,二進制可以爲000:
    --- 二進制表示:000 轉換成八進制:0
    r-- 二進制表示:100 轉換成八進制:4
    rw- 二進制表示:110 轉換成八進制:6
    r-x 二進制表示:101 轉換成八進制:5
    -w- 二進制表示:010 轉換成八進制:2
    -wx 二進制表示:011 轉換成八進制:3
    --x 二進制表示:001 轉換成八進制:1
    rwx 二進制表示:111 轉換成八進制:7

附加特殊權限:
管理特殊權限的另一方式:
    suid sgid sticy     八進制權限
            0 0 0   0
            0 0 1   1
            0 1 0   2
            0 1 1   3
            1 0 0   4
            1 0 1   5
            1 1 0   6
            1 1 1   7
基於八進制方式賦權時,可於默認的三位八進制數字左側再加一位八進制數字
例如:chmod 1777 

二、實驗環境說明

[root@www ~]# cat /etc/redhat-release   #以CentOS 7.1發行版做實驗
CentOS Linux release 7.1.1503 (Core) 
[root@www ~]# uname -a   #內核版本保持默認,沒有升級
Linux www.yanhui.com 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@www ~]# ip addr show|sed -rn '/inet[[:space:]]+127/! s/^[[:space:]]+inet[[:space:]]+(.*)\/[[:digit:]]+[[:space:]]+.*$/\1/p'
172.16.0.77
[root@www ~]# getenforce   #Selinux機制關閉,沒有啓用。
Disabled
[root@www ~]# systemctl status firewalld.service #防火牆有啓動
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Sat 2018-12-22 03:11:34 CST; 19h ago
 Main PID: 859 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─859 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Dec 22 03:11:34 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Dec 22 12:06:21 www.yanhui.com firewalld[859]: 2018-12-22 12:06:21 ERROR: INVALID_SERVICE: php-fpm
Dec 22 12:06:26 www.yanhui.com firewalld[859]: 2018-12-22 12:06:26 ERROR: INVALID_SERVICE: php

三、管理員用戶和普通用戶做切換之su

su命令可以切換用戶,可以切換用戶並以指定用戶的身份去運行命令。其中涉及到完全切換環境和不完全切換環境的問題。
下面兩句話很關鍵,所以貼出原文:
When called without arguments su defaults to running an interactive shell as root.
For  backward  compatibility su defaults to not change the current directory and to only set the environment variables HOME and SHELL 
(plus USER and LOGNAME if the target user is not root).  It is recommended to always use the --login option (instead it's shortcut  -)  to  
avoid side effects caused by mixing environments.

如果調用su不加任何參數(表示su USERNAME),這種還是會以root環境去運行一個交互式的shell;
爲了向後兼容考慮,su默認(不加任何參數)不會去改變當前工作目錄,會改變HOME和SHELL環境的值(如果切換的用戶是非root用戶,
還會改變USER和LOGNAME環境變量的值)。切換的時候建議完全切換,使用--login選項或簡寫符號"-"去完全切換,即su - USERNAME,
因爲不完全切換可能會因爲環境混合造成副作用。

[root@www tmp]# pwd
/tmp
[root@www tmp]# su yanhui
[yanhui@www tmp]$ pwd
/tmp    #半切換,工作目錄沒有改變
[yanhui@www tmp]$ echo $HOME
/home/yanhui
[yanhui@www tmp]$ echo $SHELL
/bin/bash
[yanhui@www tmp]$ echo $USER
yanhui
[yanhui@www tmp]$ echo $LOGNAME
yanhui
[yanhui@www tmp]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@www tmp]# su - yanhui
Last login: Sat Dec 22 22:22:56 CST 2018 on pts/0
[yanhui@www ~]$ pwd
/home/yanhui
[yanhui@www ~]$ echo $HOME
/home/yanhui
[yanhui@www ~]$ echo $SHELL
/bin/bash
[yanhui@www ~]$ echo $USER
yanhui
[yanhui@www ~]$ echo $LOGNAME
yanhui
[yanhui@www ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/yanhui/.local/bin:/home/yanhui/bin #看到這裏的區別了嗎,完全切換是與不完全切換不同的。
[yanhui@www ~]$ 


su命令使用語法結構:
su [options] [-] [USER [arg]...]

只要涉及用戶登錄都與pam有關係,這裏的su也是如此,也會調用pam的功能,這裏不深入。su常見的兩個語法形式:
su  -l  user (或su --login user 或 su - user) #完全切換用戶(和用戶的環境)
su  -l  user  -c   'COMMAND'  #以另外一個用戶的身份(以及環境)去執行指定的命令;

四、sudo概述

sudo允許根據指定的安全策略,運行一個用戶以其他用戶身份或管理員身份去執行某個命令;
對於安全的策略設定和登入和登出,sudo支持插件的結構管理。爲了無縫的與sudo的前端一起更好的工作,第三方機構可以自己開發和
發佈自己的安全策略設定以及登入登出插件。默認的安全策略是sudoers,通過配置文件/etc/sudoers來配置,並且支持LDAP協議。

如果有的話,安全策略檢測用戶有什麼權限去運行。這個sudo策略可能需要用戶使用其他認證機制或密碼認證機制去認證他們自己的
密碼。如果認證需要的時候,等待用戶輸入用戶密碼超出了配置文件的時間限制,默認sudo會退出。這個限制是由sudo策略指定,
默認的交互式密碼輸入的超時時間安全策略是5分鐘。

安全策略可以支持保存憑據,這種緩存機制能夠讓用戶在一小段時間內反覆調用sudo去執行命令而不需要反覆多次確認密碼。默認
sudoers策略緩存憑據時間是5分鐘。(如果sudoers被重寫,緩存憑據要重新計算,使用sudo的-v選項也可以在不運行命令的情況下更新
這個平均緩存時間)。-k會在運行命令的時候是的sudo的緩存憑據失效,即每一次使用sudo去調用命令,配合-k,如果要認證密碼,默認
情況下不管時間間隔有多短,都需要用戶去確認密碼。

    

五、sudo配置

5.1、sudo軟件包和sudoers的配置文件/etc/sudoers

sudo軟件包:

[root@www tmp]# rpm -qi sudo
Name        : sudo
Version     : 1.8.6p7
Release     : 13.el7
Architecture: x86_64
Install Date: Mon 03 Dec 2018 11:26:46 PM CST
Group       : Applications/System
Size        : 2520314
License     : ISC
Signature   : RSA/SHA256, Sat 14 Mar 2015 04:37:15 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : sudo-1.8.6p7-13.el7.src.rpm
Build Date  : Fri 06 Mar 2015 01:42:19 PM CST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.courtesan.com/sudo/
Summary     : Allows restricted root access for specified users
Description :
Sudo (superuser do) allows a system administrator to give certain
users (or groups of users) the ability to run some (or all) commands
as root while logging all commands and arguments. Sudo operates on a
per-command basis.  It is not a replacement for the shell.  Features
include: the ability to restrict what commands a user may run on a
per-host basis, copious logging of each command (providing a clear
audit trail of who did what), a configurable timeout of the sudo
command, and the ability to use the same configuration file (sudoers)
on many different machines.

[root@www tmp]# rpm -ql sudo
/etc/pam.d/sudo  #sudo程序的pam的配置文件
/etc/pam.d/sudo-i
/etc/sudo-ldap.conf  #sudo配合LADP協議的配置文件
/etc/sudo.conf #sudo前端的配置文件
/etc/sudoers #默認sudo安全策略模塊的配置文件(我們要在這個配置文件中配置實現相關功能)
/etc/sudoers.d #sudo安全策略模塊的子配置文件
/usr/bin/sudo #執行程序
/usr/bin/sudoedit #執行程序
/usr/bin/sudoreplay #重放sudo會話的日誌的程序
/usr/libexec/sesh
/usr/libexec/sudo_noexec.so #模塊文件
/usr/libexec/sudoers.so #默認塊文件
/usr/sbin/visudo #編輯sudoers 文件的工具,可以檢測語法,建議使用工具調用編輯配置文件
......
......省略了很多安裝的文檔文件。
/var/db/sudo #sudo配置的用戶數據庫數據文件目錄
sudoers的模板配置文件:
[root@www ~]# cat /etc/sudoers
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
### 添加翻譯:sudoers可以讓特定用戶以root身份去運行各種各樣的命令而不需要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 (perhaps using 
## wildcards for entire domains) or IP addresses instead.
###在主機組別名組內。你可以使用主機名(或許可以使用整個域的通配表示)和IP地址。

# Host_Alias     FILESERVERS = fs1, fs2
# Host_Alias     MAILSERVERS = smtp, smtp2
###Host_Alias這個是關鍵字,要按照這個來寫。表示定義主別名。後邊跟着一個空格,然後跟自定義的全大寫(必須全大寫)的英文,建議
使用特定含義的單詞或縮寫,後邊加入對應的主機ip或名稱,不同的主機地址以逗號隔開。上面示例定義了兩個主機別名,一個是
FILESERVERS,含義表示文件服務器,有主機fs1和fs2。


## 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
### 示例定義了一個用戶別名,叫ADMINS,這個用戶別名中包含了用戶jsmith和mikem兩個用戶。

## Command Aliases  ##命令別名
## These are groups of related commands...
###這些通常是一組相關的命令(組合在一起)

## Networking  #和網絡相關的命令的別名
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool
##上面定義了一個命令別名,叫NETWORKING,其含義表示網絡。比如route,ifconfig,ping等。注意這些命令一定要是文件的絕對路徑,
否則會有語法錯誤。

## Installation and management of software  ###軟件包安裝和管理的命令別名
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
##上面定義了一個命令別名,叫SOFTWARE,其含義表示軟件包。比如rpm,yum包管理器工具和包管理器前端工具。


## Services  ###服務相關的前端命令別名
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig
### 上面定義了一個命令別名叫SERVICES,中間包含的命令有service和chkconfig


## Updating the locate database   #和更新locate數據庫相關的別名組。
# Cmnd_Alias LOCATE = /usr/bin/updatedb
定義一個命令別名,叫LOCATE,目前有updatedb命令加入。

## Storage #和存儲管理相關的命令別名
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
###上面定義了一個命令別名,叫STORAGE,加入的命令有fdisk,parted,mount,umount等。

## Delegating permissions  #和權限委派相關的命令別名
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp
### 上面定義了一個命令別名,叫DELEGATING,加入的命令有visudo,chown,chmod,chgrp等

## Processes ##和進程處理相關的命令別名
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
###上面定義了一個命令別名,叫PROCESSES,加入的命令有nice,kill,killall。

## Drivers #和驅動模塊管理相關的命令別名。
# Cmnd_Alias DRIVERS = /sbin/modprobe
### 上面定義了一個命令別名,叫DRIVERS,加入的命令有modprobe。

# Defaults specification  #默認指定和默認設定

#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. 
#         You have to run "ssh -t hostname sudo <cmd>".
#
### 關閉"ssh hostname sudo <cmd>",因爲這種用法會清楚的顯示出密碼。可以使用
"ssh -t hostname sudo <cmd>"的形式。
Defaults    requiretty

#
# Refuse to run if unable to disable echo on the tty. This setting should also be
# changed in order to be able to use sudo without a tty. See requiretty above.
#
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).

### 接下來的部分纔是主要設置部分:什麼用戶可以在什麼機器上運行什麼軟件或命令。(sudoers文件可以支持在多個系統之間共享)
## Syntax:
##
## 	user	MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere 
### 允許root用戶在任意地點運行任意命令。
root	ALL=(ALL) 	ALL

## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
### 允許sys組的成員去運行後邊命令別名中的命令。相關命令涉及的命令請往上翻看示例定義。

## Allows people in group wheel to run all commands
%wheel ALL=(ALL)	ALL 
### 允許以wheel爲主要組的用戶以任何用戶身份去運行所有的命令。(要驗證用戶本身的密碼)

## Same thing without a password
# %wheel	ALL=(ALL)	NOPASSWD: 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
## 允許以users爲主要組的用戶可以以管理員的身份去執行掛載和卸載關盤的命令

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now
### 允許以users爲主要組的用戶執行shutdown的立即關機命令。

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
這裏表示包含子配置。命令includedir前邊的#符號不表示註釋,千萬要注意。

總結:

授權項:
	who  	where=(whom)  	commands
	users 	hosts=(runas)	 	commands
允許指定用戶(users或who),在指定主機上(where或hosts,ALL表示任意主機)=以誰的身份(whom或runas,root表示以管理員身份,
如果爲ALL表示以所有用戶身份)  可以運行的命令列表(commands) 

其中users可以設置爲:
username:單個用戶名;
#uid:特定用戶uid;
%groupname:以groupname爲主要組的用戶;
##gid:以gid對應的組爲主要組的用戶;
User_Alias:用戶別名中定義的用戶;

hosts可以設置爲:
ip:單個ip地址
hostname:單個主機名;
NetAddr:網絡地址,網段地址;
Host_Alias:主機地址別名中指定的主機;

runas可以設置爲:

Runas_Alias:定義可以被作爲運行用戶的別名。

commands可以設置爲:
commands:指定運行的命令;
directory
sudoedit:特殊權限,可用於向其它用戶授予sudo權限;
Cmnd_Alias:命令別名中指定的命令;

定義別名的方法:
ALIAS_TYPE  NAME=item1, item2, item3, ...
ALIAS_TYPE表示別名的類型,要使用特定的字符串表示,系統預先有設定不同的分類。
NAME表示別名的別稱,定義好後可以使用這個名稱來引用。必須使用全大寫字符。
別名類型有:
User_Alias   這個是定義用戶的別名
Host_Alias  這個是定義主機的別名
Runas_Alias  這個是定義以何種身份用戶運行的用戶的別名
Cmnd_Alias  這個是命令的別名

5.2、配置一個普通用戶,不需要切換密碼就能以管理員身份執行管理命令

先創建普通用戶yanhui:
[root@www ~]# id yanhui
id: yanhui: no such user
[root@www ~]# useradd yanhui
[root@www ~]# echo "yanhui" | passwd --stdin yanhui
Changing password for user yanhui.
passwd: all authentication tokens updated successfully.
[root@www ~]# id yanhui
uid=1003(yanhui) gid=1003(yanhui) groups=1003(yanhui)

使用visudo來編輯sudo的配置文件:
加入一項:yanhui  ALL=(ALL)       NOPASSWD: ALL
含義爲:yanhui這個用戶,可以通過任意主機(ALL),以任何人的身份(小括號中的ALL,也可以定義爲root,一般管理員的管理任務,
其他用戶是沒有權限做的,所以定義成ALL和root區別不是很大),不需要驗證密碼(NOPASSWD)的執行所有命令(最後的ALL)。

登錄測試:
(1) 添加用戶centos
[yanhui@www ~]$ id centos
id: centos: no such user
[yanhui@www ~]$ useradd centos   #這種用法不是sudo的提權用法
-bash: /usr/sbin/useradd: Permission denied
[yanhui@www ~]$ sudo useradd centos  #每次要以管理員身份運行某個命令的時候,要使用sudo
[yanhui@www ~]$ sudo passwd centos
Changing password for user centos.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

(2) 掛載光盤
[yanhui@www ~]$ mount /dev/sr0 /mnt/
mount: only root can do that
[yanhui@www ~]$ sudo mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[yanhui@www ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  1.3G   49G   3% /
devtmpfs                 985M     0  985M   0% /dev
tmpfs                    994M     0  994M   0% /dev/shm
tmpfs                    994M  8.6M  986M   1% /run
tmpfs                    994M     0  994M   0% /sys/fs/cgroup
/dev/mapper/centos-home   48G   33M   48G   1% /home
/dev/sda1                497M  102M  395M  21% /boot
/dev/sr0                 4.1G  4.1G     0 100% /mnt

好了,以後就不要使用root用戶直連了,使用yanhui提權就行了。

5.3、定義別名並簡單驗證

Cmnd_Alias NETADMINCMD = /usr/sbin/ip,/usr/sbin/ifconfig,/usr/sbin/route
Cmnd_Alias USERADMINCMD = /usr/sbin/useradd,/user/sbin/userdel,/usr/sbin/passwd,! /usr/sbin/passwd root
fedora ALL=(ALL)        NETADMINCMD
centos ALL=(ALL)        NETADMINCMD,USERADMINCMD

定義了兩個命令別名,一個是NETADMINCMD表示管理ip地址網絡相關的命令,另一個是USERADMINCMD表示可以做用戶管理的。
現在定義兩個提權用戶,fedora用戶可以在任意主機以任意人的身份去執行NETADMINCMD別名中定義的命令,而centos用戶可以在
任意主機以任意人的身份去執行NETADMINCMD和USERADMINCMD別名中定義的命令。

簡單來說,就是創建兩個普通用戶,分別是fedora和centos,然後讓fedora用戶只能做簡單的管理管理,而centos則可以做簡單的網絡
管理之外,還要能做用戶的管理。爲了擴展性,所以可以定義一個網絡管理需要的命令的命令別名,以及定義一個用戶管理需要的命令
的命令別名,後續如果有新增命令可以直接加入對應的別名即可。

(1) 創建普通用戶centos和fedora
[yanhui@www ~]$ sudo useradd centos
[yanhui@www ~]$ sudo useradd fedora
[yanhui@www ~]$ sudo echo "fedora" | passwd --stdin fedora  #可以看到這種提權用戶的設定這種用法還不行
Only root can do that.
[yanhui@www ~]$ sudo passwd fedora
Changing password for user fedora.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[yanhui@www ~]$ sudo passwd centos
Changing password for user centos.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[yanhui@www ~]$ id fedora
uid=1005(fedora) gid=1005(fedora) groups=1005(fedora)
[yanhui@www ~]$ id centos
uid=1004(centos) gid=1004(centos) groups=1004(centos)

(2 ) visudo編輯
加入以下內容:
Cmnd_Alias NETADMINCMD = /usr/sbin/ip,/usr/sbin/ifconfig,/usr/sbin/route
Cmnd_Alias USERADMINCMD = /usr/sbin/useradd,/user/sbin/userdel,/usr/sbin/passwd,! /usr/sbin/passwd root
fedora ALL=(ALL)        NETADMINCMD
centos ALL=(ALL)        NETADMINCMD,USERADMINCMD
##本次內容都只是測試的,生產中這樣設定一不符合特定需求,二可能存在很多可以利用的漏洞點(具體可以反覆推敲,特別要注意涉及
到修改管理員密碼的方式等)

(3) 登錄fedora進行測試
[yanhui@www ~]$ sudo su - fedora
[fedora@www ~]$ clear
[fedora@www ~]$ sudo -l   #查看自己可以使用的命令有哪些
[sudo] password for fedora: 
Matching Defaults entries for fedora on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC 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 fedora may run the following commands on this host:
    (ALL) /usr/sbin/ip, /usr/sbin/ifconfig, /usr/sbin/route

[fedora@www ~]$ ifconfig eno16777736:1 192.168.56.119 netmask 255.255.255.0 up #不加sudo默認不允許
SIOCSIFADDR: Operation not permitted
SIOCSIFFLAGS: Operation not permitted
SIOCSIFNETMASK: Operation not permitted
SIOCSIFFLAGS: Operation not permitted

[fedora@www ~]$ sudo ifconfig eno16777736:1 192.168.56.119 netmask 255.255.255.0 up  #sudo可以新增ip
[fedora@www ~]$ ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.0.77  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::20c:29ff:fe3d:65a2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:3d:65:a2  txqueuelen 1000  (Ethernet)
        RX packets 115792  bytes 60836731 (58.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 75994  bytes 15843178 (15.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.119  netmask 255.255.255.0  broadcast 192.168.56.255
        ether 00:0c:29:3d:65:a2  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 5383  bytes 2888858 (2.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5383  bytes 2888858 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[fedora@www ~]$ sudo useradd user1 #嘗試使用不能提權執行的命令
Sorry, user fedora is not allowed to execute '/sbin/useradd user1' as root on www.yanhui.com.

(4) 登錄centos用戶進行測試
[yanhui@www ~]$ sudo su - centos
[centos@www ~]$ sudo -l -k  #-k表示不使用緩存的密碼。每次都要輸入密碼。-l查看有哪些權限
[sudo] password for centos: 
Matching Defaults entries for centos on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC 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 centos may run the following commands on this host:
    (ALL) /usr/sbin/ip, /usr/sbin/ifconfig, /usr/sbin/route, (ALL) /usr/sbin/useradd, /user/sbin/userdel, /usr/sbin/passwd, !/usr/sbin/passwd root
[centos@www ~]$ sudo passwd root  #嘗試做壞事,修改管理員密碼,不允許。
[sudo] password for centos: 
Sorry, user centos is not allowed to execute '/bin/passwd root' as root on www.yanhui.com.
[centos@www ~]$ sudo useradd user1 #可以新增用戶
[sudo] password for centos: 
[centos@www ~]$ sudo userdel -r user1  #這裏不能刪除用戶是我沒有定義好,默認有個/sbin/userdel沒有加入
Sorry, user centos is not allowed to execute '/sbin/userdel -r user1' as root on www.yanhui.com.
[centos@www ~]$ sudo ifconfig eno16777736:1 down #可以做網絡接口的管理
[centos@www ~]$ ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.0.77  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::20c:29ff:fe3d:65a2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:3d:65:a2  txqueuelen 1000  (Ethernet)
        RX packets 116168  bytes 60871684 (58.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 76307  bytes 15878012 (15.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 5383  bytes 2888858 (2.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5383  bytes 2888858 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0       
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章