linux sudoers - sudo权限配置

【基本介绍】
The sudoers policy module determines a user's sudo privileges. It is the default sudo policy plugin. The policy is driven by the /etc/sudoers file or, optionally in LDAP.
/etc/sudoers 用来配置管理用户sudo权限

【基本配置】
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

## Allows people in group wheel to run all commands without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

## Allows members of the users group to mount and unmount the
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

讲解:root    ALL=(ALL) ALL
root表示被授权的用户,这里是根用户;
第一个ALL表示所有计算机;
第二个ALL表示所有用户;
第三个ALL表示所有命令;

通用格式
/etc/sudoers的通用格式为:
user  host run_as  command
user:一位或几位用户,在/etc/group中可以用一个%代替它,组对象的名称一定要用百分号%开头。
host:一个或几个主机名;
run_as:作为哪个用户运行,常见选项是root和ALL
command:想让用户或组运行的一个或几个根级别命令。

例如:
hans   ALL=(root)   useradd,userdel
授权hans用户在所有计算机上以root身份运行useradd,userdel命令。
%smith  ALL=(ALL)  NOPASSWD:useradd,userdel
授权smith组全部成员在所有计算机上以所有用户的身份运行useradd,userdel命令;且运行时不必输入密码。

--注意--
添加的命令最好要用全路径
如果命令有参数的话必须严格按照配置文件里面来
nagios  ALL= NOPASSWD: /bin/cp /etc/shadow /home/nagios/haha
那么复制shadow也只能复制到名字为haha的文件,否则要输入密码

【异常】
提示sudo: sorry, you must have a tty to run sudo解决方法
方法1.注释#Defaults    requiretty
方法2.添加Defaults:user !requiretty (指定user不需要tty)

【参考】
http://www.sudo.ws/sudoers.man.html
http://blog.chinaunix.net/uid-26642180-id-3962245.html

猜你喜欢

转载自runpanda.iteye.com/blog/2089667