-- xinetd (也叫inetd, super daemon) # yum install xinetd # chkconfig --list --用这个命令可以看到后面有下面的一段信息 ...... xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:off xinetd based services: chargen-dgram: off chargen-stream: off cvs: off daytime-dgram: off daytime-stream: off discard-dgram: off discard-stream: off echo-dgram: off echo-stream: off rsync: off tcpmux-server: off tftp: on time-dgram: off time-stream: off xinetd 就类似于一个拖管服务,可以拖管一些其它的小服务。拖管后的好处就是可以使用xinetd强大的参数来控制这些服务。(比如一个小服务没有一些控制功能,但支持xinetd拖管,你就可以拖管并使用xinetd的参数来控制它) /etc/xinetd.conf --主配置文件,一般不用配置,主要配置子配置文件 /etc/xinetd.d --子配置文件目录 # vim /etc/xinetd.d/rsync service rsync { disable = yes --yes表示关闭,no表示开启 flags = IPv6 socket_type = stream wait = no --并发连接 user = root --跑守护进程的用户 server = /usr/bin/rsync --启动程序路径 server_args = --daemon --启动参数 log_on_failure += USERID } 例一:以sshd为例,把sshd拖管到xinetd下 # /etc/init.d/sshd stop # vim /etc/xinetd.d/ssh service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i } # /etc/init.d/xinetd restart # lsof -i:22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME xinetd 5472 root 5u IPv6 58673 0t0 TCP *:ssh (LISTEN) --查看22端口的进程,由sshd变为了xinetd就表示你拖管成功了 例二:上面的例子并没有看出拖管与不拖管的区别 下面在例一的基础上加上对IP或网段的访问控制功能,这个是sshd本身不具备的功能(除非写iptables) service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i only_from = 10.1.1.26 10.1.1.35 --表示只有这两个IP能连,其它都不能连这个ssh服务 } service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i no_access = 10.1.1.26 10.1.1.35 --只有这两个IP不能连,其它都可以 } service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i only_from = 10.1.1.0/24 no_access = 10.1.1.35 10.1.1.26 --这表示只能10网段访问,但10网段里的10和26这两台也不能访问 } 例三:在例二的基础再加一些功能 man xinetd.conf 1,控制这个服务最多只能3个连接,每个源IP只能1个连接 2,控制只能9:00到18:00才能ssh连接 3,指定日志记录到/var/log/xinetd_ssh.log里 service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i instances = 3 per_source = 1 access_times = 9:00-18:00 log_type = file /var/log/xinetd_ssh.log } 例四:修改ssh服务的连接端口 # vim /etc/xinetd.d/ssh service ssh { disable = no socket_type = stream protocol = tcp wait = no user = root server = /usr/sbin/sshd server_args = -i instances = 3 per_source = 1 access_times = 9:00-18:00 log_type = file /var/log/xinetd_ssh.log port = 2222 } # vim /etc/services ssh 2222 ssh 2222 --这个文件里对应的也要改,man xinetd.conf里有说明 # /etc/init.d/xinetd restart 客户端访问是要使用ssh 10.1.1.35 -p 2222,但我自己连自己不用加-p 2222,因为我默认就是用2222来连接。 这也就是说/etc/services里的端口也决定了你做为客户端去访问别人的默认端口 =================================================================== 时间同步 # system-config-date ntp (network time protocol) 端口:123 如果你想同步公网的时间服务器,使用 ntpdate 公网时间服务器域名或IP 下面三个是rhel默认的外网时间服务器 0.rhel.pool.ntp.org 1.rhel.pool.ntp.org 2.rhel.pool.ntp.org 这几个外网服务器的IP比较多,有些测试不能同步,有些可以,下面这个IP目前测试是OK的 ntpdate 202.112.31.197 ntpdate ntp.fudan.edu.cn --或者去找一些其它的公网时间同步服务器,这个是复旦大学的一个时间服务器 自己想要搭建时间同步服务器的话 # vim /etc/ntp.conf restrict 10.1.1.0 mask 255.255.255.0 nomodify notrap --我这里就把18行的注释打开,并把网段改成了当前自己局域网的网段10.1.1.0,就表示在这个网段内能被时间同步 # /etc/init.d/ntpd restart # netstat -ntlup |grep :123 --启动服务后,123端口就有监听了 # ntpdate 10.1.1.35 --你自己启动NTPD服务后,去同步别人会报socket被占用的错误 26 Aug 14:42:52 ntpdate[7417]: the NTP socket is in use, exiting # ntpdate 10.1.1.35 --这是表示同步成功 26 Aug 14:43:29 ntpdate[7117]: step time server 61.129.42.44 offset 146586394.753774 sec 2,使用xinetd来做另一种时间 vim /etc/xinetd.d/time-dgram disable = no # vim /etc/xinetd.d/time-stream disable = no # /etc/init.d/xinetd restart # netstat -ntlup |grep :37 tcp 0 0 :::37 :::* LISTEN 5740/xinetd udp 0 0 :::37 :::* 5740/xinetd 客户端要同步服务器用下面命令就可以了 rdate -s 10.1.1.35 总结:如果你想实现你公司服务器的所有时间与北京时间一致。可以用一台能上外网的服务器去同步公网上的时间服务器,然后把这台也配置成时间服务器,其它的机器通过内网定时同步就可以了。 hwclock -r --查看硬件时间 hwclock -w --把系统时间写入到硬件时间 ====================================== rsyslog 日志服务 rsyslog 日志服务 rhel5里叫syslog /etc/init.d/rsyslog restart chkconfig rsyslog on log facility: 用来记录一种类型日志的日志设备 daemon auth authpriv user mail lpr news uucp ftp local0-7 log level: 日志级别,指定记录日志信息的详细程度 debug --调式日志,一般看不懂,不用此级别 info --最常见的级别 notice warning alert err crit emerg none --什么都不记录 --上面的级别从上到下,级别是由低到高 符号 . 代表大于或等于此级别 .= 只等于此级别 .! 不等于此级别 # vim /etc/rsyslog.conf *.info;mail.none;authpriv.none;cron.none /var/log/messages --除了mail,authpriv,cron的日志外,其它所有设备的info级别以上的都记录到messages里 authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /etc/init.d/rsyslog restart chkconfig rsyslog on log facility: 用来记录一种类型日志的日志设备 daemon auth authpriv user mail lpr news uucp ftp local0-7 log level: 日志级别,指定记录日志信息的详细程度 debug --调式日志,一般看不懂,不用此级别 info --最常见的级别 notice warning alert err crit emerg none --什么都不记录 --上面的级别从上到下,级别是由低到高 符号 . 代表大于或等于此级别 .= 只等于此级别 .! 不等于此级别 # vim /etc/rsyslog.conf *.info;mail.none;authpriv.none;cron.none /var/log/messages --除了mail,authpriv,cron的日志外,其它所有设备的info级别以上的都记录到messages里 authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg * uucp,news.crit /var/log/spooler local7.* /var/log/boot.log 例1,把ssh的日志指定记录到/var/log/ssh下,有哪些方法? 就目前所学, 第一种:拖管到xinetd下 log_type = file /var/log/ssh 第二种:拖管到xinetd下 log_type = syslog local5 info 然后在/etc/rsyslog.conf里加上一句 local5.info /var/log/ssh 第三种:不拖管到xinetd下,用原来的sshd来做的话 # vim /etc/ssh/sshd_config SyslogFacility local5 # /etc/init.d/sshd restart # vim /etc/rsyslog.conf local5.* /var/log/ssh # /etc/init.d/rsyslog restart 例2.远程日志 (日志集中化管理) 把多台服务器的日志远程记录到其中一台集中化管理,方便对其统一分析 log server log clinet 10.1.1.35 10.1.1.198 要求:把client上的ssh的服务日志,远程记录到server上/var/log/ssh里 第一步:在server上打开远程日志的接收服务端口 # vim /etc/rsyslog.conf $ModLoad imtcp $InputTCPServerRun 514 --把这两句打开注释 # /etc/init.d/rsyslog restart # netstat -ntlup |grep :514 tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 7295/rsyslogd tcp 0 0 :::514 :::* LISTEN 7295/rsyslogd 第二步:在client上配置ssh服务的日志远程发送给server # vim /etc/ssh/sshd_config SyslogFacility local5 # /etc/init.d/sshd restart # vim /etc/rsyslog.conf local5.* @@10.1.1.35:514 # /etc/init.d/rsyslog restart 第三步:回到server上配置传过来的日志记录到哪个文件里 # vim /etc/rsyslog.conf local5.* /var/log/ssh # /etc/init.d/rsyslog restart 测试:都去ssh连接client,然后在server上cat /var/log/ssh可以看到已经有client的日志了。如果有100台日志都传到此文件,怎么区分? 可以看到日志里的有一列是不同的,就是主机名的主机头,所以所有主机都要绑定主机名,并且主机头要不一样就可以了 ========================================================================= logrotate 日志轮转,切割,备份,归档 # ls /var/log/ --查看此目录下的日志文件,你可能会看到类似messages-20140724这种名字的日志文件名,这就表示此日志在7月24号轮转过 日志为什么要轮转? # vim /etc/logrotate.conf weekly --每周轮转 rotate 4 --保留四份 create --创建新的日志 #dateext --轮转的日志文件加时间后缀,这里为了测试方便建议注释掉 #compress include /etc/logrotate.d /var/log/wtmp { monthly --表示此文件是每月轮转,而不会用到上面的每周轮转 create 0664 root utmp --轮转的文件的权限,owner,group minsize 1M --最小1M和每月时间两条件满足,则轮转 rotate 1 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # vim /etc/logrotate.d/syslog --这个子配置文件,没有指定的参数都会以默认方式 /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } 验证日志轮转: # logrotate -f /etc/logrotate.conf --用此命令强制轮转方便测试,注意后面一定只能接主配置文件 例一: 如果我100台的ssh日志都写到了我这台的/var/log/ssh里,我想对其做日志轮转,怎么做? 把下面这段写到主配置文件,或者任意子配置文件里,再或者自己建立一个新的子配置文件都可以 /var/log/ssh { monthly rotate 2 missingok } 例二: 讨论一下下面这段的意思 sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true --这一句话表示轮转后对rsyslog的pid进行刷新(但pid其实不变) endscript sharedscripts prerotate 轮转前执行脚本 endscript sharedscripts postrotate 轮转后执行脚本 endscript 为什么轮转后要做上面的kill -HUP? 先问一个小问题:一个服务它的日志记录到aaa.log,手动mv aaa.log aaa.log.1,然后touch aaa.log,请问新的日志信息会记录到哪个文件里? 答案是它还会记录到aaa.log.1里,因为它还找原来的innode(同分区里mv一个文件innode不变);所以要在做完后去kill -HUP它的pid(有些服务是kill -USR1) 小实验: # logger -t "哈哈" "你好" # tail -3 /var/log/messages --默认这条信息在当前的messages里 sharedscripts postrotate logger -t "呵呵" "再见" /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript 请问这条信息在轮转后在哪个文件里? messages.1 sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true logger -t "呵呵" "再见" endscript 请问这条信息在轮转后在哪个文件里? messages ===================================================================== logrotate的邮件报警功能 在linux下默认能本机收发邮件,需要把postfix服务启起来(rhel6默认使用postfix,rhel5默认使用sendmail) /var/log/ssh { monthly rotate 2 missingok mail test maillast --这表示在轮转之后发一个邮件给test用户,主题会为/var/log/ssh.3,就是man logrotate里指的about-to-expire的文件 } /var/log/ssh { monthly rotate 2 missingok mail test mailfirst --这表示在轮转之后发一个邮件给test用户,主题会为/var/log/ssh.1,就是man logrotate里指的just-rotated的文件 } --上面是默认的两个参数,下面是用自己的方式去实现上面的功能,并且可以自定义邮件正文和主题 /var/log/ssh { monthly rotate 2 missingok sharedscripts postrotate echo "ssh轮转成功了.xxxxx" |mail -s "logrotated succeed" test endscript } ==================================================================== dhcp (dynamic host configure protocol,动态主机配置协议) 可以动态分配IP,网关,DNS指向,主机名(需要DNS支持) 客户端如何用dhcp协议获取IP,网关,DNS指向等 方法一:下面的配置文件里配置dhcp动态获取,然后network重启时都会使用dhcp协议 /etc/sysconfig/network-scripts/ifcfg-eth0 方法二: dhclient eth0 --手动dhcp获取 客户端发dhcp discover广播包 服务端回dhcp offer包 客户端收到offer包后,再发dhcp request广播包 服务端回dhcp ack包 # yum install dhcp* -y # cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf 实验,使用宿主机模拟dhcp服务器,虚拟机模拟客户端,但是不要用bridge网络;建议使用hostonly网络(防止同一个网络内多个dhcp冲突) 第一步: 虚拟机(dhcp客户端)指定网卡连接vmnet1网络(并可以通过vmware-netcfg命令,把vmware自带的dhcp关闭); 然后配置网卡通过dhcp获取IP;使用/etc/init.d/network restart或者 dhclient eth0命令来动态获取IP; 结果是获取不成功(因为vmware自带的dhcp关闭,自己要配置的dhcp服务器还没有开始配置) 第二步: 在宿主机(dhcp服务器端)先确认vmnet1网卡的地址和网段(我这里是192.168.62.1) 然后配置下面一段,并启动服务 # vim /etc/dhcp/dhcpd.conf subnet 192.168.62.0 netmask 255.255.255.0 { range 192.168.62.50 192.168.62.100; option domain-name-servers 8.8.8.8; option routers 192.168.62.1; option broadcast-address 192.168.62.255; } # /etc/init.d/dhcpd restart 第三步: 再次回到虚拟机(dhcp客户端)使用/etc/init.d/network restart或者 dhclient eth0命令来动态获取IP; 这次就可以成功了 第四步: 验证 1,客户端上ifconfig 查看IP是否获取的是服务器指定的范围 2,客户端cat /etc/resolv.conf和route -n查看是否成功获取DNS指向和网关 3,客户端cat /var/lib/dhclient/dhclient-eth0.leases或者cat /var/log/messages |grep dhcp查看相关信息 4,服务端cat /var/lib/dhcpd/dhcpd.leases查看分配情况 在配置里加上下面一段,指定客户端MAC地址为00:0c:29:f4:4c:de的会获取IP为192.168.62.77 host sfsfdsa { --这个名字随意,有主机名就写主机名 hardware ethernet 00:0c:29:f4:4c:de; fixed-address 192.168.62.77; } 然后服务端/etc/init.d/dhcpd restart重启服务,客户端重新获取验证 ========================================================= 思考题: 如果现在针对ssh服务做日志轮转,要求每两天日志轮转一次,保留所有的日志,并且以时间来对其分类 vim /bin/sshlogrotate.sh mv /var/log/ssh /var/log/`date +%F`.ssh touch /var/log/ssh kill -HUP `cat /var/run/sshd.pid` 01 10 */2 * * sh /bin/sshlogrotate.sh 练习: 1,配置服务器对一个服务日志(假设为/var/log/clientssh.log)配置日志轮转, 要求:(1.)配置到/etc/logrotate.conf文件里或者配置到/etc/logrotate.d/目录里的子配置文件 (2.)每天轮转一次 (3.)最多保留4个副本 2,还要要求在对/var/log/clientssh.log的日志轮转之前自动把/var/log/clientssh.log 的最后一个副本(也就是/var/log/clientssh.log.4)文件给cp到/backup/4天前的年/4天前的月/4天前的天.clientssh.log的格式 (比如:今天为2014-07-28,cp的文件名就是/backup/2014/7/24.clientssh.log) 3,并且轮转完成后,发一封邮件通知给root用户,邮件主题和正文内容自定义 提示:man date # date -d '-4 days' +%F /var/log/clientssh.log { rotate 4 daily create 0664 root root sharedscripts prerotate mkdir -p /backup/`date -d "-4 days" +%Y`/`date -d "-4 days" +%m`/ cp /var/log/clientssh.log.4 /backup/`date -d "-4 days" +%Y`/`date -d "-4 days" +%m`/`date -d "-4 days" +%d`.clientssh.log endscript sharedscripts postrotate kill -HUP `cat /var/run/sshd.pid` echo "日志轮转了" |mail -s 'ssh日志轮转了' root endscript } 4,上网查询了解一下日志分析,日志合并的方法和概念 日志分析:脚本分析,软件分析(现在对于web日志的分析比较常见,主要的开源软件有awstats,weblizer) 还有一些服务也可以分析,比如系统自带的logwatch可以分析/var/log/下面的日志,并发邮件;或者比如oracle数据也有日志巡检工具 日志合并:追加,排序(sort) [root@li test]# cat 1.txt 1 3 5 7 [root@li test]# cat 2.txt 8 4 9 [root@li test]# cat 3.txt 6 10 2 # sort -n -o 4.txt 1.txt 2.txt 3.txt # yum install logwatch -y [root@li ~]# rpm -qa |grep logwatch logwatch-7.3.6-49.el6.noarch [root@li ~]# rpm -qc logwatch-7.3.6-49.el6.noarch /etc/logwatch/conf/ignore.conf /etc/logwatch/conf/logwatch.conf /etc/logwatch/conf/override.conf # vim /usr/share/logwatch/default.conf/logwatch.conf # cat /usr/share/logwatch/default.conf/logwatch.conf |grep -v '#' |grep -v ^$ LogDir = /var/log TmpDir = /var/cache/logwatch MailTo = root MailFrom = Logwatch Print = Range = yesterday Detail = Low Service = All mailer = "sendmail -t" # /etc/init.d/postfix restart # logwatch # cat /var/mail/root