Linux服務器權限管理之sudo高級應用

Linux服務器權限管理之sudo高級應用

Linux服務器權限管理之sudo高級應用

前面提到Linux系統修改權限與默認權限,它都是徵對用戶對於目錄或文件的一些權限控制,那麼其實真正從安全性角度上來考慮的話,是要控制用戶一定執行命令的權限,也就是哪些用戶可以執行哪些命令,不可以執行哪些命令,因此也就有了sudo這個應用

對於sudo提權,也就是修改/etc/sudoers的配置文件
[root@Centos ~]# ls -ll /etc/sudoers
-r--r-----. 1 root root 5870 Aug 19 16:53 /etc/sudoers
可以看出/etc/sudoers默認的權限是440(也是系統比較安全的權限設置),當然了超級管理員肯定是有權限修改其文件內容的,不然還不哦哦了...........。


一:直接修改/etc/sudoers文件的注意事項

1、操作時最好用echo >>追加,不過cat sed同樣也可以實現(不常用)
2、修改完成後一定記得檢查語法visudo -c
3、確保/etc/sudoers默認的權限是440(防止權限誤用)
4、及時驗證修改的配置是否正確
5、確保知道root密碼,以便普通用戶可以通過sudo su -命令切換



二:sudo的配置文件/etc/sudoers

[root@Centos ~]# cat /etc/sudoers
Sudoers allows particular users to run various commands as

the root user, without needing the root password.

##

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.

Host Aliases

Groups of machines. You may prefer to use hostnames (perhaps using

wildcards for entire domains) or IP addresses instead.

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

修改時儘量複製系統的格式進行相關修改,防止配置錯誤,難以改正
修改授權某用戶權限成功後,切換到用戶下面,用sudo -l 來查看自己擁有哪些權限
[yuw001@Centos ~]$ sudo -l
[sudo] password for yuw001:
Matching Defaults entries for yuw001 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, logfile=/var/log/sudo.log










User yuw001 may run the following commands on this host:
(root) /bin/ping, /bin/hostname, /usr/bin/free, /sbin/route,
/bin/netstat
使用命令時記得加上sudo
[yuw001@Centos ~]$ hostname linux
hostname: you must be root to change the host name
[yuw001@Centos ~]$ /bin/hostname linux
hostname: you must be root to change the host name
[yuw001@Centos ~]$ sudo hostname linux
[root@linux ~]# 退出重新登陸後發現主機名修改成功
配置文件一行是一個規則,前面都會用#進行註釋,用‘\’續行(換行)









三:配置文件中規則的分類

1、別名類型
別名類型分爲以下幾類
a、Host_Alias(主機別名)
生產環境中一般不會設置主機別名,一般主機別名不太常用
root ALL=(ALL) ALL 第一個ALL就是主機別名的應用位置



b、User_Alias(用戶別名)
如果是表示用戶組那麼前面要加%
root ALL=(ALL) ALL root就是用戶別名的應用位置
User_Alias ADMINS = jsmith, mikem


c、Runas_Alias別名
此別名是指定“用戶身份”,即 sudo允許切換到的用戶
root ALL=(ALL) ALL 第二個(ALL)就是用戶別名的應用位置
Runas_Alias OP = root


d、Cmnd_Alias(命令別名)
就是定義一個別名,它可以包含一堆命令的內容(一組相關命令的集合)
root ALL=(ALL) ALL 第三個ALL就是用戶別名的應用位置
Cmnd_Alias DRIVERS = /sbin/modprobe


說明
用戶別名中的用戶必須是系統真實存在的,書寫時注意空格,用戶別名具有特殊意義,用戶別名必須使用大寫
命令別下的成員必須使用絕對路徑,可以用‘\’換行

2、授權規則
授權規則就是執行的規則,授權中的所有ALL必須大寫

Allow root to run any commands anywhere

root ALL=(ALL) ALL
yumw ALL=(ALL) /usr/sbin/useradd,/usr/sbin/userdel

###user group sa allow to run commands anywhere
yuw ALL=/usr/sbin,/sbin
sa ALL= /usr/sbin,/sbin,!/sbin/fdisk
!表示禁止執行這個命令
[sa@linux ~]$ sudo -l
User sa may run the following commands on this host:
(root) /usr/bin, (root) /sbin, (root) !/sbin/fdisk
[sa@linux ~]$ sudo fdisk
Sorry, user sa is not allowed to execute '/sbin/fdisk' as root on linux.
如果將配置做下修改
###user group sa allow to run commands anywhere
yuw ALL=/usr/sbin,/sbin
sa ALL= !/sbin/fdisk ,/usr/sbin,/sbin
[sa@linux ~]$ sudo -l
User sa may run the following commands on this host:
(root) /usr/bin, (root) /sbin, (root) !/sbin/fdisk
[root@linux ~]# su - sa
[sa@linux ~]$ sudo fdisk
[sudo] password for sa:
Usage:
fdisk [options] <disk> change partition table
fdisk [options] -l <disk> list partition table(s)
fdisk -s <partition> give partition size(s) in blocks
Options:
-b <size> sector size (512, 1024, 2048 or 4096)
-c switch off DOS-compatible mode
-h print help
-u <size> give sizes in sectors instead of cylinders
-v print version
-C <number> specify the number of cylinders
-H <number> specify the number of heads
-S <number> specify the number of sectors per track
所以經測試結果表明,sa ALL= !/sbin/fdisk ,/usr/sbin,/sbin命令執行的匹配規則是從後到前的,所以後面執行sudo fdisk不會提示權限不足的現像
Linux服務器權限管理之sudo高級應用
































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章