天天看點

sudo日志檔案跟蹤

    由于管理的伺服器不多,而且都是我一個人管,但公司發展越來越快,伺服器也越來越多,人手也添加了,但不能一直都用root賬号人手一份,為了安全和管理控制,決定部署sudo。sudo預設安裝在系統裡,配置日志檔案跟蹤的時候遇到問題,無論怎麼操作,就是不能寫入sudo日志。參考了網上的N多文章 ,都不能解決問題,其中的奧秘就在于他們都省略了一步。是以部署東西,最好還是先看官方文檔為先,遇到小部份難題在google為好。

一、實驗環境

  1. # cat /etc/redhat-release  
  2. CentOS release 5.5 (Final)
  3. # rpm -qa | grep sudo
  4. sudo-1.7.2p1-5.el5
  5. [root@CentOS-A log]# rpm -ql  sudo
  6. /usr/share/doc/sudo-1.7.2p1/sample.sudoers
  7. /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. #
  3. # Sample /etc/sudoers file.
  4. # This file MUST be edited with the 'visudo' command as root.
  5. # See the sudoers man page for the details on how to write a sudoers file.
  6. # $Sudo: sample.sudoers,v 1.29 2008/10/03 19:55:57 millert Exp $
  7. ##
  8. # Override built-in defaults
  9. Defaults        syslog=auth
  10. Defaults>root       !set_logname
  11. Defaults:FULLTIMERS !lecture
  12. Defaults:millert    !authenticate
  13. Defaults@SERVERS    log_year, logfile=/var/log/sudo.log
  14. Defaults!PAGERS     noexec
  15. # User alias specification
  16. User_Alias  FULLTIMERS = millert, mikef, dowdy
  17. User_Alias  PARTTIMERS = bostley, jwfox, crawl
  18. User_Alias  WEBMASTERS = will, wendy, wim
  19. # Runas alias specification
  20. Runas_Alias OP = root, operator
  21. Runas_Alias DB = oracle, sybase
  22. # Host alias specification
  23. Host_Alias  SPARC = bigtime, eclipse, moet, anchor:\
  24. SGI = grolsch, dandelion, black:\
  25. ALPHA = widget, thalamus, foobar:\
  26. HPPA = boa, nag, python
  27. Host_Alias  CUNETS = 128.138.0.0/255.255.0.0
  28. Host_Alias  CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
  29. Host_Alias  SERVERS = master, mail, www, ns
  30. Host_Alias  CDROM = orion, perseus, hercules
  31. # Cmnd alias specification
  32. Cmnd_Alias  DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \
  33.            /usr/sbin/rrestore, /usr/bin/mt
  34. Cmnd_Alias  KILL = /usr/bin/kill
  35. Cmnd_Alias  PRINTING = /usr/sbin/lpc, /usr/bin/lprm
  36. Cmnd_Alias  SHUTDOWN = /usr/sbin/shutdown
  37. Cmnd_Alias  HALT = /usr/sbin/halt
  38. Cmnd_Alias  REBOOT = /usr/sbin/reboot
  39. Cmnd_Alias  SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \
  40.             /usr/local/bin/tcsh, /usr/bin/rsh, \
  41.             /usr/local/bin/zsh
  42. Cmnd_Alias  SU = /usr/bin/su
  43. Cmnd_Alias  VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \
  44.               /usr/bin/chfn
  45. Cmnd_Alias  PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less
  46. # User specification
  47. # root and users in group wheel can run anything on any machine as any user
  48. root        ALL = (ALL) ALL
  49. %wheel      ALL = (ALL) ALL
  50. # full time sysadmins can run anything on any machine without a password
  51. FULLTIMERS  ALL = NOPASSWD: ALL
  52. # part time sysadmins may run anything but need a password
  53. PARTTIMERS  ALLALL = ALL
  54. # jack may run anything on machines in CSNETS
  55. jack        CSNETS = ALL
  56. # lisa may run any command on any host in CUNETS (a class B network)
  57. lisa        CUNETS = ALL
  58. # operator may run maintenance commands and anything in /usr/oper/bin/
  59. operator    ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\
  60.        sudoedit /etc/printcap, /usr/oper/bin/
  61. # joe may su only to operator
  62. joe     ALL = /usr/bin/su operator
  63. # pete may change passwords for anyone but root on the hp snakes
  64. pete        HPPA = /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root
  65. # bob may run anything on the sparc and sgi machines as any user
  66. # listed in the Runas_Alias "OP" (ie: root and operator)
  67. bob     SPARC = (OP) ALL : SGI = (OP) ALL
  68. # jim may run anything on machines in the biglab netgroup
  69. jim     +biglab = ALL
  70. # users in the secretaries netgroup need to help manage the printers
  71. # as well as add and remove users
  72. +secretaries    ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser
  73. # fred can run commands as oracle or sybase without a password
  74. fred        ALL = (DB) NOPASSWD: ALL
  75. # on the alphas, john may su to anyone but root and flags are not allowed
  76. john        ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root*
  77. # jen can run anything on all machines except the ones
  78. # in the "SERVERS" Host_Alias
  79. jen     ALL, !SERVERS = ALL
  80. # jill can run any commands in the directory /usr/bin/, except for
  81. # those in the SU and SHELLS aliases.
  82. jill        SERVERS = /usr/bin/, !SU, !SHELLS
  83. # steve can run any command in the directory /usr/local/op_commands/
  84. # as user operator.
  85. steve       CSNETS = (operator) /usr/local/op_commands/
  86. # matt needs to be able to kill things on his workstation when
  87. # they get hung.
  88. matt        valkyrie = KILL
  89. # users in the WEBMASTERS User_Alias (will, wendy, and wim)
  90. # may run any command as user www (which owns the web pages)
  91. # or simply su to www.
  92. WEBMASTERS  www = (www) ALL, (root) /usr/bin/su www
  93. # anyone can mount/unmount a cd-rom on the machines in the CDROM alias
  94. ALL     CDROM = NOPASSWD: /sbin/umount /CDROM,\
  95.        /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. # This logs successful and failed sudo attempts to the file /var/log/sudo
  18. local2.debug                    /var/log/sudo
  19. # To log to a remote machine, use something like the following,
  20. # where "loghost" is the name of the remote machine.
  21. 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. Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
  3. root     10367  0.0  0.1   1728   612 ?        Ss   23:10   0:00 syslogd -m 0
  4. root     10437  0.0  0.1   3920   688 pts/1    R+   23:41   0:00 grep syslog
  5. # kill -HUP 10367
  6. #/etc/init.d/syslog restart

四、成功測試

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

繼續閱讀