在Linux/Unix的shell中,通过sudo命令是可以从普通用户切换到超级用户的,这样便于我们的操作,但是有的时候这样做并不是很安全,下来我们来一起看看具体的操作,首先,sudolinux下常用的允许普通用户使用超级用户权限的工具,它的主要配置文件是sudoers,linux下通常在/etc目录下,如果是solaris,缺省不装sudo的,编译安装后通常在安装目录的 etc目录下,不过不管sudoers文件在哪儿,sudo都提供了一个编辑该文件的命令:visudo来对该文件进行修改。强烈推荐使用该命令修改 sudoers,因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。

言归正传,下面介绍如何配置sudoers

首先写sudoers的缺省配置:

  1. #############################################################  
  2. # sudoers file.  
  3. #  
  4. # This file MUST be edited with the 'visudo' command as root.  
  5. #  
  6. # See the sudoers man page for the details on how to write a sudoers file.  
  7. #  
  8.  
  9. # Host alias specification  
  10.  
  11. User alias specification  
  12.  
  13. # Cmnd alias specification  
  14.  
  15. # Defaults specification  
  16.  
  17. User privilege specification  
  18. root    ALL=(ALLALL 
  19.  
  20. # Uncomment to allow people in group wheel to run all commands  
  21. # %wheel        ALL=(ALL)       ALL 
  22.  
  23. # Same thing without a password 
  24. # %wheel        ALL=(ALL)       NOPASSWD: ALL 
  25.  
  26. # Samples  
  27. # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom  
  28. # %users  localhost=/sbin/shutdown -h now  
  29. ##################################################################  
  30.  

1. 最简单的配置,让普通用户support具有root的所有权限
执行visudo之后,可以看见缺省只有一条配置:
root    ALL=(ALL) ALL
那么你就在下边再加一条配置:
support ALL=(ALL) ALL

这样,普通用户support就能够执行root权限的所有命令

support用户登录之后,执行:
sudo su -
然后输入support用户自己的密码,就可以切换成root用户了

2. 让普通用户support只能在某几台服务器上,执行root能执行的某些命令
首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4
Host_Alias
Cmnd_Alias
User_Alias
Runas_Alias

1) 配置Host_Alias:就是主机的列表
Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
2)
配置Cmnd_Alias:就是允许执行的命令的列表
Cmnd_Alias      COMMAND_FLAG = command1, command2, command3
3)
配置User_Alias:就是具有sudo权限的用户的列表
User_Alias USER_FLAG = user1, user2, user3
4)
配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5)
配置权限
配置权限的格式如下:
USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密码验证的话,则按照这样的格式来配置
USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG

配置示例:

 

  1. ############################################################################  
  2. # sudoers file.  
  3. #  
  4. # This file MUST be edited with the 'visudo' command as root.  
  5. #  
  6. # See the sudoers man page for the details on how to write a sudoers file.  
  7. #  
  8.  
  9. # Host alias specification  
  10. Host_Alias      EPG = 192.168.1.1, 192.168.1.2  
  11.  
  12. User alias specification  
  13.  
  14. # Cmnd alias specification  
  15. Cmnd_Alias      SQUID = /opt/vtbin/squid_refresh, /sbin/service, /bin/rm  
  16.  
  17. # Defaults specification  
  18.  
  19. User privilege specification  
  20. root    ALL=(ALLALL 
  21. support EPG=(ALL) NOPASSWD: SQUID  
  22.  
  23. # Uncomment to allow people in group wheel to run all commands  
  24. # %wheel        ALL=(ALL)       ALL 
  25.  
  26. # Same thing without a password 
  27. # %wheel        ALL=(ALL)       NOPASSWD: ALL 
  28.  
  29. # Samples  
  30. # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom  
  31. # %users  localhost=/sbin/shutdown -h now  
  32. ###############################################################  
  33.