1. 程式人生 > >Centos7給ntpd服務指定日誌檔案

Centos7給ntpd服務指定日誌檔案

Centos7給ntpd服務指定日誌檔案

有一次伺服器的上ntpd服務莫名關閉,為了弄清楚原因,我查看了ntpd服務的日誌。但是ntpd服務預設沒有專用的日誌檔案,而是直接寫入系統日誌檔案/var/log/messages。這樣我檢視起來很不方便,於是決定看能否給ntpd服務指定專用的日誌檔案。 
我查看了ntpd的配置檔案/etc/ntp.conf,裡面並沒有關於日誌檔案的定義;同時網上查看了一些資料,也沒有太多有效的方案。 
於是我將ntpd服務所有的配置檔案列出:

[[email protected] ~]#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

[[email protected] ~]#cat /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g"
  • 1
  • 2
  • 3

同時觀察ntpd的服務執行狀態:

[[email protected] ~]#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命令的選項中是否有關於日誌檔案的選項:

[[email protected] ~]#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

[[email protected] ~]#cat /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g -l /var/log/ntpstats/ntpd.log"
  • 1
  • 2
  • 3

此時,-l指定的檔案不需要我們手動建立,然後直接重啟ntpd服務,再次觀察服務的執行狀態:

[[email protected] ~]#systemctl restart ntpd
[[email protected] ~]#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服務已經可以指定日誌檔案。而日誌檔案也已經自動建立,並有新的日誌寫入:

[[email protected] ~]#ll -h /var/log/ntpstats/ntpd.log 
-rw-r--r-- 1 root root 1.3K Jun  4 18:30 /var/log/ntpstats/ntpd.log
[[email protected] ~]#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