[关闭]
@natsumi 2017-06-02T07:08:56.000000Z 字数 5710 阅读 1447

ntp服务器配置

Linux


参考:
http://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

服务器段配置文件

  1. # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
  2. #driftfile /var/lib/ntp/ntp.drift
  3. # Enable this if you want statistics to be logged.
  4. statsdir /var/log/ntpstats/
  5. statistics loopstats peerstats clockstats
  6. filegen loopstats file loopstats type day enable
  7. filegen peerstats file peerstats type day enable
  8. filegen clockstats file clockstats type day enable
  9. # Specify one or more NTP servers.
  10. server 127.127.1.0 #local clock
  11. fudge 127.127.1.0 stratum 10
  12. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
  13. # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
  14. # more information.
  15. pool 0.ubuntu.pool.ntp.org iburst
  16. pool 1.ubuntu.pool.ntp.org iburst
  17. pool 2.ubuntu.pool.ntp.org iburst
  18. pool 3.ubuntu.pool.ntp.org iburst
  19. # Use Ubuntu's ntp server as a fallback.
  20. pool ntp.ubuntu.com
  21. # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
  22. # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
  23. # might also be helpful.
  24. #
  25. # Note that "restrict" applies to both servers and clients, so a configuration
  26. # that might be intended to block requests from certain clients could also end
  27. # up blocking replies from your own upstream servers.
  28. # By default, exchange time with everybody, but don't allow configuration.
  29. restrict -4 default kod notrap nomodify nopeer noquery limited
  30. restrict -6 default kod notrap nomodify nopeer noquery limited
  31. # Local users may interrogate the ntp server more closely.
  32. restrict 127.0.0.1
  33. restrict ::1
  34. # Needed for adding pool entries
  35. restrict source notrap nomodify noquery
  36. # Clients from this (example!) subnet have unlimited access, but only if
  37. # cryptographically authenticated.
  38. restrict 192.168.10.0 mask 255.255.255.0 nomodify
  39. # If you want to provide time to your local subnet, change the next line.
  40. # (Again, the address is an example only.)
  41. #broadcast 192.168.123.255
  42. # If you want to listen to time broadcasts on your local subnet, de-comment the
  43. # next lines. Please do this only if you trust everybody on the network!
  44. #disable auth
  45. #broadcastclient
  46. #Changes recquired to use pps synchonisation as explained in documentation:
  47. #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
  48. #server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
  49. #fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware
  50. #server 127.127.22.1 # ATOM(PPS)
  51. #fudge 127.127.22.1 flag3 1 # enable PPS API

客户端配置文件

  1. # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
  2. driftfile /var/lib/ntp/ntp.drift
  3. # Enable this if you want statistics to be logged.
  4. #statsdir /var/log/ntpstats/
  5. statistics loopstats peerstats clockstats
  6. filegen loopstats file loopstats type day enable
  7. filegen peerstats file peerstats type day enable
  8. filegen clockstats file clockstats type day enable
  9. # Specify one or more NTP servers.
  10. server 192.168.10.2 #server IP(example!)
  11. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
  12. # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
  13. # more information.
  14. pool 0.ubuntu.pool.ntp.org iburst
  15. pool 1.ubuntu.pool.ntp.org iburst
  16. pool 2.ubuntu.pool.ntp.org iburst
  17. pool 3.ubuntu.pool.ntp.org iburst
  18. # Use Ubuntu's ntp server as a fallback.
  19. pool ntp.ubuntu.com
  20. # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
  21. # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
  22. # might also be helpful.
  23. #
  24. # Note that "restrict" applies to both servers and clients, so a configuration
  25. # that might be intended to block requests from certain clients could also end
  26. # up blocking replies from your own upstream servers.
  27. # By default, exchange time with everybody, but don't allow configuration.
  28. restrict -4 default kod notrap nomodify nopeer noquery limited
  29. restrict -6 default kod notrap nomodify nopeer noquery limited
  30. # Local users may interrogate the ntp server more closely.
  31. restrict 192.168.10.2 #server IP(example!)
  32. restrict 127.0.0.1
  33. restrict ::1
  34. # Needed for adding pool entries
  35. restrict source notrap nomodify noquery
  36. # Clients from this (example!) subnet have unlimited access, but only if
  37. # cryptographically authenticated.
  38. #restrict 192.168.123.0 mask 255.255.255.0 notrust
  39. # If you want to provide time to your local subnet, change the next line.
  40. # (Again, the address is an example only.)
  41. #broadcast 192.168.123.255
  42. # If you want to listen to time broadcasts on your local subnet, de-comment the
  43. # next lines. Please do this only if you trust everybody on the network!
  44. #disable auth
  45. #broadcastclient
  46. #Changes recquired to use pps synchonisation as explained in documentation:
  47. #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
  48. #server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
  49. #fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware
  50. #server 127.127.22.1 # ATOM(PPS)
  51. #fudge 127.127.22.1 flag3 1 # enable PPS API

启动NTP服务

在服务器段需要启动NTP服务

  1. # service ntp start

有可能出现启动失败的情况

  1. //启动失败
  2. Job for ntp.service failed because the control process exited with error code. See "systemctl status ntp.service" and "journalctl -xe" for details.

根据错误提示通过systemctl status ntp.service查看

  1. root@rasp-2:/home/xietianrui# systemctl status ntp.service
  2. ntp.service - LSB: Start NTP daemon
  3. Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
  4. Active: failed (Result: exit-code) since 2017-06-02 14:46:35 CST; 5min ago
  5. Docs: man:systemd-sysv-generator(8)
  6. Process: 2112 ExecStart=/etc/init.d/ntp start (code=exited, status=5)
  7. 6 02 14:46:35 rasp-2 systemd[1]: Starting LSB: Start NTP daemon...
  8. 6 02 14:46:35 rasp-2 systemd[1]: ntp.service: Control process exited, code=exit
  9. 6 02 14:46:35 rasp-2 systemd[1]: Failed to start LSB: Start NTP daemon.
  10. 6 02 14:46:35 rasp-2 systemd[1]: ntp.service: Unit entered failed state.
  11. 6 02 14:46:35 rasp-2 systemd[1]: ntp.service: Failed with result 'exit-code'.

这里提到/etc/init.d/ntp,退出码是5,在这个文件开头的部分就可以找到退出的原因是test -x $DAEMON不成功。test -x是检测文件是否存在且有执行权限。

  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: ntp
  4. # Required-Start: $network $remote_fs $syslog
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 1
  8. # Short-Description: Start NTP daemon
  9. ### END INIT INFO
  10. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  11. . /lib/lsb/init-functions
  12. DAEMON=/usr/sbin/ntpd
  13. PIDFILE=/var/run/ntpd.pid
  14. test -x $DAEMON || exit 5
  15. # ...

解决方法:给/usr/sbin/ntpd加上执行权限

  1. root@rasp-2:/home/xietianrui# chmod +x /usr/sbin/ntpd

开机启动NTP服务

  1. root@rasp-2:/home/xietianrui# systemctl enable ntp

客户端对时

  1. # ntpdate 192.168.10.2
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注