18.8. sudo, sudoedit - execute a command as another user

简介:
debian:~# apt-get install sudo		
		

18.8.1. /etc/sudoers

sudo的配置文件是/etc/sudoers,visudo修改时会锁住sudoers文件,保存修改到临时文件,然后检查文件格式,确保正确后才会覆盖sudoers文件. 必须保证sudoers格式正确,否则sudo将无法运行.

/etc/sudoers

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=NOPASSWD: ALL
		
			

sudo group

neo@debian:/etc/mysql$ cat /etc/group | grep 'sudo'
sudo:x:27:neo		
			

18.8.2. /etc/sudoers

visudo调用的默认编辑器是vi,如果要临时使用其他编辑器,在该命令前加上EDITOR环境变量即可.

		
[root@netkiller ~]# EDITOR=vim visudo
			

18.8.3. 设置示例

			
>1 允许neo用户从任何主机登录,以root的身份执行/usr/sbin/useradd命令

neo   ALL=(root) /usr/sbin/useradd 

>2 允许jam用户从任何主机登录,以root的身份无密码使用sudo执行/sbin/iptables -n -t filter -L 

jam   ALL=(ALL) NOPASSWD: /sbin/iptables -n -t filter -L 

>3 neo用户从任何主机登录,以root的身份执行自定义命令链里面的命令

Cmnd_Alias USERCOMMAND = /sbin/route,/sbin/ifconfig,/bin/ping,/sbin/dhclient,/usr/bin/net,/sbin/iptables,/usr/bin/rfcomm,/usr/bin/wvdial,/sbin/iwconfig
neo   ALL=(root)    USERCOMMAND
			
			

18.8.4. NOPASSWD

ubuntu NOPASSWD sudo的时候不需要输入密码

%admin ALL=(ALL)ALL
改为
%admin ALL=(ALL) NOPASSWD: NOPASSWD: ALL	
			

用户

www localhost=NOPASSWD: /bin/cat, /bin/ls			
			

18.8.5. 允许或禁止命令

命令前面加‘!’可以禁止用户运行该命令

neo ALL = (root) /bin/mount, /bin/umount, !/bin/mount /data0
dba ALL = /bin/mount /u0[1-5], /bin/umount /u0[1-5]
			

18.8.6. Cmnd_Alias 用法

Cmnd_Alias 定义命令别名

Cmnd_Alias WEBMASTER = /srv/nginx/sbin/nginx, /srv/php/sbin/php-fpm, !/srv/mysql/bin/mysql
www localhost = NETWORKING, SERVICES, DELEGATING, PROCESSES, WEBMASTER
			

自定义用户组(以所有的身份)执行自定义的命令链里的命令

Cmnd_Alias USERCOMMAND = /sbin/route,/sbin/ifconfig,/bin/ping,/usr/sbin/mtr,/bin/traceroute,/usr/bin/top,/bin/df,/usr/bin/free,/usr/bin/du,/bin/ls,/bin/date,/usr/bin/less
User_Alias    ADMINS = user1, user2
ADMINS	ALL=(ALL)	USERCOMMAND			
			

18.8.7. wheel 组

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
neo     ALL=(ALL)       ALL
%wheel  ALL=(ALL)       ALL
			

18.8.8. 注意事项

1 修改sudo记录密码的时间


Defaults:用户名 timestamp_timeout=20
eg:
Defaults:redhat timestamp_timeout=20

2 默认sudo命令只能在tty上执行,注释掉下面选项可以使程序调用sudo命令

Defaults    requiretty			
		




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
1月前
|
关系型数据库 MySQL 数据安全/隐私保护
已解决:mysql: [Warning] Using a password on the command line interface can be insecure.
已解决:mysql: [Warning] Using a password on the command line interface can be insecure.
56 0
|
2月前
|
Ubuntu Docker 容器
sudo apt-get update失败已经解决 报错 The update command takes no arguments
sudo apt-get update失败已经解决 报错 The update command takes no arguments
62 0
问题解决:Try to run this command from the system terminal. Make sure that you use the...
问题解决:Try to run this command from the system terminal. Make sure that you use the...
执行 mysqld --install 报错:Install Remove of the Service Denied
执行 mysqld --install 报错:Install Remove of the Service Denied
79 0
执行 mysqld --install 报错:Install Remove of the Service Denied
执行 mysqld --install 报错:The service already exists
执行 mysqld --install 报错:The service already exists
94 0
执行 mysqld --install 报错:The service already exists
check_user_createdate.sh
在前面这篇文章Linux如何找出用户的创建时间里面讨论了查看用户创建时间的方法,后面自己尝试弄了一个脚本来检查所有用户创建时间脚本,当然更合理的应该叫检查所有用户的密码修改时间比较准确(因为这种方法有条件限制),期间和夕照讨论了一下如何用shell脚本实现,获益良多。
632 0

热门文章

最新文章