/etc/sudoers文件详解

了解 sudoers 文件

[root@localhost ~]# cat /etc/sudoers
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##该文件允许特定用户像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 (perhap using 
## wildcards for entire domains) or IP addresses instead.
## 对于一组服务器,你可能会更喜欢使用主机名(可能是全域名的通配符)
## 或IP地址代替,这时可以配置主机别名

# 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 {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}groupname 
## rather than USERALIAS
## 这并不很常用,因为你可以通过使用组来代替一组用户的别名  
# User_Alias ADMINS = jsmith, mikem

## Command Aliases
## These are groups of related commands...
## 指定一系列相互关联的命令(当然可以是一个)的别名,通过赋予该别名sudo权限,  
## 可以通过sudo调用所有别名包含的命令,下面是一些示例

## 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
## Installation and management of software
#软件安装管理相关命令别名  
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
## Services
#服务相关命令别名 
Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig
## Updating the locate database
#本地数据库升级命令别名  
Cmnd_Alias LOCATE = /usr/sbin/updatedb
## Storage
#磁盘操作相关命令别名
Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
## Delegating permissions
#代理权限相关命令别名 
Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp
## Processes
#进程相关命令别名
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
## Drivers
#驱动命令别名
Cmnd_Alias DRIVERS = /sbin/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>".
#
Defaults    requiretty
Defaults    env_reset
Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY"
## 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.
# {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## 允许sys中户组中的用户使用NETWORKING等所有别名中配置的命令

## Allows people in group wheel to run all commands
# {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}wheel        ALL=(ALL)       ALL
## 允许wheel用户组中的用户执行所有命令  
## Same thing without a password
## 允许wheel用户组中的用户在不输入该用户的密码的情况下使用所有命令
# {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}wheel        ALL=(ALL)       NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
## 允许users用户组中的用户像root用户一样使用mount、unmount、chrom命令 
# {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# {f7f18c9fd6018b57a8e5310208018ee39180fe76b298cb0846ec99113e86d05f}users  localhost=/sbin/shutdown -h now
## 允许users用户组中的用户像root用户一样使用shutdown命令

了解什么是 Wheel组?

Wheel组是Unix系统一个遗留物。当服务器需要做比日常例行维护更高级的工作的时候,就经常需要用到root权限了。而这个wheel组就是建立用来归纳一些特殊的系统用户用的,这其中的用户都或多或少地拥有root的部分功能和权限。也就是说如果你不是wheel组成员,那就没有root身上任何的特权。也因为这样,使用wheel组成员用户的话,会尽量减少对系统“摧毁性”破坏的概率和风险。如今大多数的Linux发行版本中,仍然保留了wheel这个组,虽然它已经不像当初设计出来的那样必要了,但是有些老玩家还是忠于这种旧式经典风格的,所以他们经常还是依旧让wheel组发挥着以往的作用。他们是这样做的:在建立他们自己的用户时,将其添加入wheel组中(用wheel组作为用户的主组),或者使用vigr来编辑/etc/group文件,将他们的用户名追加到wheel组那行的末尾。

为什么需要wheel组?

通常在UNIX下,即使我们是系统的管理员,也不推荐用 root 用户登录来进行系统管理。一般情况下用普通用户登录,在需要 root 权限执行一些操作时,再 su 登录成为 root 用户。但是,任何人只要知道了 root 的密码,就都可以通过 su 命令来登录为 root 用户——这无疑为系统带来了安全隐患。所以,将普通用户加入到 wheel 组,被加入的这个普通用户就成了管理员组内的用户,但如果不对一些相关的配置文件进行配置,这个管理员组内的用户与普通用户也没什么区别。

上一篇
下一篇