Centos7给ntpd服务指定日志文件
有一次服务器的上ntpd服务莫名关闭,为了弄清楚原因,我查看了ntpd服务的日志。但是ntpd服务默认没有专用的日志文件,而是直接写入系统日志文件/var/log/messages
。这样我查看起来很不方便,于是决定看能否给ntpd服务指定专用的日志文件。
我查看了ntpd的配置文件/etc/ntp.conf
,里面并没有关于日志文件的定义;同时网上查看了一些资料,也没有太多有效的方案。
于是我将ntpd服务所有的配置文件列出:
[root@Server ~]#rpm -ql ntp|grep conf
/etc/ntp.conf
/etc/sysconfig/ntpd
/usr/share/man/man5/ntp.conf.5.gz
- 1
- 2
- 3
- 4
发现了ntpd服务的第二个配置文件/etc/sysconfig/ntpd
[root@Server ~]#cat /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g"
- 1
- 2
- 3
同时观察ntpd的服务运行状态:
[root@Server ~]#systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-06-04 17:55:52 CST; 2min 39s ago
Process: 2002 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2003 (ntpd)
CGroup: /system.slice/ntpd.service
└─2003 /usr/sbin/ntpd -u ntp:ntp -g
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
显然,/etc/sysconfig/ntpd
为ntpd命令指定了服务启动的相关选项。我们再来看看ntpd命令的选项中是否有关于日志文件的选项:
[root@Server ~]#ntpd --help
ntpd - NTP daemon program - Ver. 4.2.6p5
Usage: ntpd [ -<flag> [<val>] | --<name>[{=| }<val>] ]...
Flg Arg Option-Name Description
……
-f Str driftfile frequency drift file name
-g no panicgate Allow the first adjustment to be Big
- may appear multiple times
-i Str jaildir Jail directory
-I Str interface Listen on an interface name or address
- may appear multiple times
-k Str keyfile path to symmetric keys
-l Str logfile path to the log file
-L no novirtualips Do not listen to virtual interfaces
-m no mlock Lock memory
-n no nofork Do not fork
-N no nice Run at high priority
-p Str pidfile path to the PID file
- may appear up to 2 times
-P Num priority Process priority
-q no quit Set the time and quit
-r Str propagationdelay Broadcast/propagation delay
Str saveconfigquit Save parsed configuration and quit
-s Str statsdir Statistics file location
-t Str trustedkey Trusted key number
- may appear multiple times
-u Str user Run as userid (or userid:groupid)
- may appear up to 2 times
……
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
可以得知-l
选项可以指定日志文件,于是将其写入/etc/sysconfig/ntpd
[root@Server ~]#cat /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g -l /var/log/ntpstats/ntpd.log"
- 1
- 2
- 3
此时,-l
指定的文件不需要我们手动创建,然后直接重启ntpd服务,再次观察服务的运行状态:
[root@Server ~]#systemctl restart ntpd
[root@Server ~]#systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-06-04 18:30:12 CST; 8s ago
Process: 2563 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2564 (ntpd)
CGroup: /system.slice/ntpd.service
└─2564 /usr/sbin/ntpd -u ntp:ntp -g -l /var/log/ntpstats/ntpd.log
Jun 04 18:30:12 Server.test.com systemd[1]: Starting Network Time Service...
Jun 04 18:30:12 Server.test.com systemd[1]: Started Network Time Service.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
可以看到,ntpd服务已经可以指定日志文件。而日志文件也已经自动创建,并有新的日志写入:
[root@Server ~]#ll -h /var/log/ntpstats/ntpd.log
-rw-r--r-- 1 root root 1.3K Jun 4 18:30 /var/log/ntpstats/ntpd.log
[root@Server ~]#tail /var/log/ntpstats/ntpd.log
4 Jun 18:30:12 ntpd[2564]: Listen normally on 3 ens33 192.168.2.240 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listen normally on 4 ens37 192.168.25.240 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listen normally on 5 virbr0 192.168.122.1 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listen normally on 6 lo ::1 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listen normally on 7 ens37 fe80::cf42:e535:aa48:f0f4 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listen normally on 8 ens33 fe80::1ab4:2936:7f4c:7739 UDP 123
4 Jun 18:30:12 ntpd[2564]: Listening on routing socket on fd #25 for interface updates
4 Jun 18:30:13 ntpd[2564]: 0.0.0.0 c016 06 restart
4 Jun 18:30:13 ntpd[2564]: 0.0.0.0 c012 02 freq_set kernel 6.912 PPM
4 Jun 18:30:19 ntpd[2564]: 0.0.0.0 c615 05 clock_sync