二: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就是主机别名的应用位置
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不会提示权限不足的现像