由于管理的服务器不多,而且都是我一个人管,但公司发展越来越快,服务器也越来越多,人手也添加了,但不能一直都用root账号人手一份,为了安全和管理控制,决定部署sudo。sudo默认安装在系统里,配置日志文件跟踪的时候遇到问题,无论怎么操作,就是不能写入sudo日志。参考了网上的N多文章 ,都不能解决问题,其中的奥秘就在于他们都省略了一步。所以部署东西,最好还是先看官方文档为先,遇到小部份难题在google为好。

一、实验环境

 
  
  1. # cat /etc/redhat-release  
  2.  
  3. CentOS release 5.5 (Final) 
  4.  
  5. # rpm -qa | grep sudo 
  6.  
  7. sudo-1.7.2p1-5.el5 
  8.  
  9. [root@CentOS-A log]# rpm -ql  sudo 
  10.  
  11. /usr/share/doc/sudo-1.7.2p1/sample.sudoers 
  12.  
  13. /usr/share/doc/sudo-1.7.2p1/sample.syslog.conf 

二、参考资料

注:留意我标明红色的地方,这些是成功配置所在。

 
  
  1. [root@CentOS-A ~]# cat /usr/share/doc/sudo-1.7.2p1/sample.sudoers  
  2. # Sample /etc/sudoers file. 
  3. # This file MUST be edited with the 'visudo' command as root. 
  4. # See the sudoers man page for the details on how to write a sudoers file. 
  5. # $Sudo: sample.sudoers,v 1.29 2008/10/03 19:55:57 millert Exp $ 
  6.  
  7. ## 
  8. # Override built-in defaults 
  9. ## 
  10. Defaults        syslog=auth 
  11. Defaults>root       !set_logname 
  12. Defaults:FULLTIMERS !lecture 
  13. Defaults:millert    !authenticate 
  14. Defaults@SERVERS    log_year, logfile=/var/log/sudo.log 
  15. Defaults!PAGERS     noexec 
  16.  
  17. ## 
  18. # User alias specification 
  19. ## 
  20. User_Alias  FULLTIMERS = millert, mikef, dowdy 
  21. User_Alias  PARTTIMERS = bostley, jwfox, crawl 
  22. User_Alias  WEBMASTERS = will, wendy, wim 
  23.  
  24. ## 
  25. # Runas alias specification 
  26. ## 
  27. Runas_Alias OP = root, operator 
  28. Runas_Alias DB = oracle, sybase 
  29.  
  30. ## 
  31. # Host alias specification 
  32. ## 
  33. Host_Alias  SPARC = bigtime, eclipse, moet, anchor:\ 
  34.         SGI = grolsch, dandelion, black:\ 
  35.         ALPHA = widget, thalamus, foobar:\ 
  36.         HPPA = boa, nag, python 
  37. Host_Alias  CUNETS = 128.138.0.0/255.255.0.0 
  38. Host_Alias  CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0 
  39. Host_Alias  SERVERS = master, mail, www, ns 
  40. Host_Alias  CDROM = orion, perseus, hercules 
  41.  
  42. ## 
  43. # Cmnd alias specification 
  44. ## 
  45. Cmnd_Alias  DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \ 
  46.             /usr/sbin/rrestore, /usr/bin/mt 
  47. Cmnd_Alias  KILL = /usr/bin/kill 
  48. Cmnd_Alias  PRINTING = /usr/sbin/lpc, /usr/bin/lprm 
  49. Cmnd_Alias  SHUTDOWN = /usr/sbin/shutdown 
  50. Cmnd_Alias  HALT = /usr/sbin/halt 
  51. Cmnd_Alias  REBOOT = /usr/sbin/reboot 
  52. Cmnd_Alias  SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \ 
  53.              /usr/local/bin/tcsh, /usr/bin/rsh, \ 
  54.              /usr/local/bin/zsh 
  55. Cmnd_Alias  SU = /usr/bin/su 
  56. Cmnd_Alias  VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \ 
  57.                /usr/bin/chfn 
  58. Cmnd_Alias  PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less 
  59.  
  60. ## 
  61. # User specification 
  62. ## 
  63.  
  64. # root and users in group wheel can run anything on any machine as any user 
  65. root        ALL = (ALL) ALL 
  66. %wheel      ALL = (ALL) ALL 
  67.  
  68. # full time sysadmins can run anything on any machine without a password 
  69. FULLTIMERS  ALL = NOPASSWD: ALL 
  70.  
  71. # part time sysadmins may run anything but need a password 
  72. PARTTIMERS  ALLALL = ALL 
  73.  
  74. # jack may run anything on machines in CSNETS 
  75. jack        CSNETS = ALL 
  76.  
  77. # lisa may run any command on any host in CUNETS (a class B network) 
  78. lisa        CUNETS = ALL 
  79.  
  80. # operator may run maintenance commands and anything in /usr/oper/bin/ 
  81. operator    ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\ 
  82.         sudoedit /etc/printcap, /usr/oper/bin/ 
  83.  
  84. # joe may su only to operator 
  85. joe     ALL = /usr/bin/su operator 
  86.  
  87. # pete may change passwords for anyone but root on the hp snakes 
  88. pete        HPPA = /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root 
  89.  
  90. # bob may run anything on the sparc and sgi machines as any user 
  91. # listed in the Runas_Alias "OP" (ie: root and operator) 
  92. bob     SPARC = (OP) ALL : SGI = (OP) ALL 
  93.  
  94. # jim may run anything on machines in the biglab netgroup 
  95. jim     +biglab = ALL 
  96.  
  97. # users in the secretaries netgroup need to help manage the printers 
  98. # as well as add and remove users 
  99. +secretaries    ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser 
  100.  
  101. # fred can run commands as oracle or sybase without a password 
  102. fred        ALL = (DB) NOPASSWD: ALL 
  103.  
  104. # on the alphas, john may su to anyone but root and flags are not allowed 
  105. john        ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root* 
  106.  
  107. # jen can run anything on all machines except the ones 
  108. # in the "SERVERS" Host_Alias 
  109. jen     ALL, !SERVERS = ALL 
  110.  
  111. # jill can run any commands in the directory /usr/bin/, except for 
  112. # those in the SU and SHELLS aliases. 
  113. jill        SERVERS = /usr/bin/, !SU, !SHELLS 
  114.  
  115. # steve can run any command in the directory /usr/local/op_commands/ 
  116. # as user operator. 
  117. steve       CSNETS = (operator) /usr/local/op_commands/ 
  118.  
  119. # matt needs to be able to kill things on his workstation when 
  120. # they get hung. 
  121. matt        valkyrie = KILL 
  122.  
  123. # users in the WEBMASTERS User_Alias (will, wendy, and wim) 
  124. # may run any command as user www (which owns the web pages) 
  125. # or simply su to www. 
  126. WEBMASTERS  www = (www) ALL, (root) /usr/bin/su www 
  127.  
  128. # anyone can mount/unmount a cd-rom on the machines in the CDROM alias 
  129. ALL     CDROM = NOPASSWD: /sbin/umount /CDROM,\ 
  130.         /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM 
 
  
  1. [root@CentOS-A ~]# cat /usr/share/doc/sudo-1.7.2p1/sample.syslog.conf  
  2. # This is a sample syslog.conf fragment for use with Sudo. 
  3. # Sudo logs to local2 by default, but this is changable via the 
  4. # --with-logfac configure option.  To see what syslog facility 
  5. # a sudo binary uses, run `sudo -V' as *root*.  You may have 
  6. # to check /usr/include/syslog.h to map the facility number to 
  7. # a name. 
  8. # NOTES: 
  9. #   The whitespace in the following line is made up of <TAB> 
  10. #       characters, *not* spaces.  You cannot just cut and paste! 
  11. #   If you edit syslog.conf you need to send syslogd a HUP signal. 
  12. #   Ie: kill -HUP process_id 
  13. #   Syslogd will not create new log files for you, you must first 
  14. #   create the file before syslogd will log to it.  Eg. 
  15.   'touch /var/log/sudo' 
  16. # $Sudo: sample.syslog.conf,v 1.3 2004/10/01 14:58:15 millert Exp $ 
  17.  
  18. # This logs successful and failed sudo attempts to the file /var/log/sudo 
  19. local2.debug                    /var/log/sudo 
  20.  
  21. # To log to a remote machine, use something like the following, 
  22. # where "loghost" is the name of the remote machine. 
  23. local2.debug                    @loghost 

三、完整配置

3.1.配置sudo日志文件

 
  
  1. #touch /var/log/sudo.log 

3.2.修改/etc/syslog.conf配置文件 

local2.debug                                  /var/log/sudo.log                     #空白处不能用空格键,必需用tab键

3.3、修改/etc/sudoers配置文件

注:不要手动修改配置文件 ,用visudo命令修改,好处是修改出错,保存会弹出错误提示信息,方便排错。

 
  
  1. #visudo 
  2. efaults              logfile=/var/log/sudo.log  #添加这一行

3.4、重启syslog服务

 
  
  1. # ps -aux | grep syslog 
  2.  
  3. Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ 
  4.  
  5. root     10367  0.0  0.1   1728   612 ?        Ss   23:10   0:00 syslogd -m 0 
  6.  
  7. root     10437  0.0  0.1   3920   688 pts/1    R+   23:41   0:00 grep syslog 
  8.  
  9. # kill -HUP 10367 
  10. #/etc/init.d/syslog restart 

四、成功测试

 
  
  1. # cat /var/log/sudo.log 
  2.  
  3. Dec 14 23:07:39 : firerat : TTY=pts/0 ; PWD=/home/firerat ; USER=root ; 
  4.  
  5.     COMMAND=/bin/cat /etc/passwd 

临高人社区:http://www.lingaoren.com