由于管理的伺服器不多,而且都是我一個人管,但公司發展越來越快,伺服器也越來越多,人手也添加了,但不能一直都用root賬号人手一份,為了安全和管理控制,決定部署sudo。sudo預設安裝在系統裡,配置日志檔案跟蹤的時候遇到問題,無論怎麼操作,就是不能寫入sudo日志。參考了網上的N多文章 ,都不能解決問題,其中的奧秘就在于他們都省略了一步。是以部署東西,最好還是先看官方文檔為先,遇到小部份難題在google為好。
一、實驗環境
- # cat /etc/redhat-release
- CentOS release 5.5 (Final)
- # rpm -qa | grep sudo
- sudo-1.7.2p1-5.el5
- [root@CentOS-A log]# rpm -ql sudo
- /usr/share/doc/sudo-1.7.2p1/sample.sudoers
- /usr/share/doc/sudo-1.7.2p1/sample.syslog.conf
二、參考資料
注:留意我标明紅色的地方,這些是成功配置所在。
- [root@CentOS-A ~]# cat /usr/share/doc/sudo-1.7.2p1/sample.sudoers
- #
- # Sample /etc/sudoers file.
- # This file MUST be edited with the 'visudo' command as root.
- # See the sudoers man page for the details on how to write a sudoers file.
- # $Sudo: sample.sudoers,v 1.29 2008/10/03 19:55:57 millert Exp $
- ##
- # Override built-in defaults
- Defaults syslog=auth
- Defaults>root !set_logname
- Defaults:FULLTIMERS !lecture
- Defaults:millert !authenticate
- Defaults@SERVERS log_year, logfile=/var/log/sudo.log
- Defaults!PAGERS noexec
- # User alias specification
- User_Alias FULLTIMERS = millert, mikef, dowdy
- User_Alias PARTTIMERS = bostley, jwfox, crawl
- User_Alias WEBMASTERS = will, wendy, wim
- # Runas alias specification
- Runas_Alias OP = root, operator
- Runas_Alias DB = oracle, sybase
- # Host alias specification
- Host_Alias SPARC = bigtime, eclipse, moet, anchor:\
- SGI = grolsch, dandelion, black:\
- ALPHA = widget, thalamus, foobar:\
- HPPA = boa, nag, python
- Host_Alias CUNETS = 128.138.0.0/255.255.0.0
- Host_Alias CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
- Host_Alias SERVERS = master, mail, www, ns
- Host_Alias CDROM = orion, perseus, hercules
- # Cmnd alias specification
- Cmnd_Alias DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \
- /usr/sbin/rrestore, /usr/bin/mt
- Cmnd_Alias KILL = /usr/bin/kill
- Cmnd_Alias PRINTING = /usr/sbin/lpc, /usr/bin/lprm
- Cmnd_Alias SHUTDOWN = /usr/sbin/shutdown
- Cmnd_Alias HALT = /usr/sbin/halt
- Cmnd_Alias REBOOT = /usr/sbin/reboot
- Cmnd_Alias SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \
- /usr/local/bin/tcsh, /usr/bin/rsh, \
- /usr/local/bin/zsh
- Cmnd_Alias SU = /usr/bin/su
- Cmnd_Alias VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \
- /usr/bin/chfn
- Cmnd_Alias PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less
- # User specification
- # root and users in group wheel can run anything on any machine as any user
- root ALL = (ALL) ALL
- %wheel ALL = (ALL) ALL
- # full time sysadmins can run anything on any machine without a password
- FULLTIMERS ALL = NOPASSWD: ALL
- # part time sysadmins may run anything but need a password
- PARTTIMERS ALLALL = ALL
- # jack may run anything on machines in CSNETS
- jack CSNETS = ALL
- # lisa may run any command on any host in CUNETS (a class B network)
- lisa CUNETS = ALL
- # operator may run maintenance commands and anything in /usr/oper/bin/
- operator ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\
- sudoedit /etc/printcap, /usr/oper/bin/
- # joe may su only to operator
- joe ALL = /usr/bin/su operator
- # pete may change passwords for anyone but root on the hp snakes
- pete HPPA = /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root
- # bob may run anything on the sparc and sgi machines as any user
- # listed in the Runas_Alias "OP" (ie: root and operator)
- bob SPARC = (OP) ALL : SGI = (OP) ALL
- # jim may run anything on machines in the biglab netgroup
- jim +biglab = ALL
- # users in the secretaries netgroup need to help manage the printers
- # as well as add and remove users
- +secretaries ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser
- # fred can run commands as oracle or sybase without a password
- fred ALL = (DB) NOPASSWD: ALL
- # on the alphas, john may su to anyone but root and flags are not allowed
- john ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root*
- # jen can run anything on all machines except the ones
- # in the "SERVERS" Host_Alias
- jen ALL, !SERVERS = ALL
- # jill can run any commands in the directory /usr/bin/, except for
- # those in the SU and SHELLS aliases.
- jill SERVERS = /usr/bin/, !SU, !SHELLS
- # steve can run any command in the directory /usr/local/op_commands/
- # as user operator.
- steve CSNETS = (operator) /usr/local/op_commands/
- # matt needs to be able to kill things on his workstation when
- # they get hung.
- matt valkyrie = KILL
- # users in the WEBMASTERS User_Alias (will, wendy, and wim)
- # may run any command as user www (which owns the web pages)
- # or simply su to www.
- WEBMASTERS www = (www) ALL, (root) /usr/bin/su www
- # anyone can mount/unmount a cd-rom on the machines in the CDROM alias
- ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\
- /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM
- [root@CentOS-A ~]# cat /usr/share/doc/sudo-1.7.2p1/sample.syslog.conf
- # This is a sample syslog.conf fragment for use with Sudo.
- # Sudo logs to local2 by default, but this is changable via the
- # --with-logfac configure option. To see what syslog facility
- # a sudo binary uses, run `sudo -V' as *root*. You may have
- # to check /usr/include/syslog.h to map the facility number to
- # a name.
- # NOTES:
- # The whitespace in the following line is made up of <TAB>
- # characters, *not* spaces. You cannot just cut and paste!
- # If you edit syslog.conf you need to send syslogd a HUP signal.
- # Ie: kill -HUP process_id
- # Syslogd will not create new log files for you, you must first
- # create the file before syslogd will log to it. Eg.
- # 'touch /var/log/sudo'
- # $Sudo: sample.syslog.conf,v 1.3 2004/10/01 14:58:15 millert Exp $
- # This logs successful and failed sudo attempts to the file /var/log/sudo
- local2.debug /var/log/sudo
- # To log to a remote machine, use something like the following,
- # where "loghost" is the name of the remote machine.
- local2.debug @loghost
三、完整配置
3.1.配置sudo日志檔案
- #touch /var/log/sudo.log
3.2.修改/etc/syslog.conf配置檔案
local2.debug /var/log/sudo.log #空白處不能用空格鍵,必需用tab鍵
3.3、修改/etc/sudoers配置檔案
注:不要手動修改配置檔案 ,用visudo指令修改,好處是修改出錯,儲存會彈出錯誤提示資訊,友善排錯。
- #visudo
- efaults logfile=/var/log/sudo.log #添加這一行
3.4、重新開機syslog服務
- # ps -aux | grep syslog
- Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
- root 10367 0.0 0.1 1728 612 ? Ss 23:10 0:00 syslogd -m 0
- root 10437 0.0 0.1 3920 688 pts/1 R+ 23:41 0:00 grep syslog
- # kill -HUP 10367
- #/etc/init.d/syslog restart
四、成功測試
- # cat /var/log/sudo.log
- Dec 14 23:07:39 : firerat : TTY=pts/0 ; PWD=/home/firerat ; USER=root ;
- COMMAND=/bin/cat /etc/passwd