Ubuntu 18.04 ntp 的入门配置

配置前的几个注意点

本人使用root权限做测试,权限不够自行sudo

系统时钟设置

# 系统时间查看
date
-------------------------------------------------------------
# 系统时间设置
date -s "YYYY-MM-DD hh:mm:ss"
e.g.
date -s "2020-10-15 10:20:15"

硬件时钟

# 硬件时钟查看
hwclock 
-----------------------------------------------------------
# 硬件时钟同步系统时钟(改了硬件时钟)
hwclock -w
-----------------------------------------------------------
# 系统时钟同步硬件时钟(改了系统时钟)
hwclock -h

Ubuntu自带的时间同步行为

Ubuntu本身自带ntp来更新时间,如有必要,需要自行关闭
# 关闭自带的更新功能
timedatectl set-ntp no  # yes为开启(不成功就yes/no反复几次)
-----------------------------------------------------------
# 查看timedatectl功能是否成功关闭
timedatectl status
# 结果       
       System clock synchronized: no
systemd-timesyncd.service active: no
                 RTC in local TZ: no

ntp 配置

ntp 安装

# serve(提供同步时间)端
sudo apt-get install ntp  
-----------------------------------------------------------
# client(请求同步)端
sudo apt-get install ntpdate

集群中提供同步(serve)配置

1.配置ntp.conf文件

vim /etc/ntp.conf
-----------------------------------------------------------
文件中间(修改)
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
restrict 192.168.50.0 mask 255.255.255.0 nomodify notrip

文件任意位置(添加)
server 127.127.1.0
fudge 127.127.1.0 stratum 10

2.启动ntp服务

/etc/init.d/ntp restart

3.查看服务是否正确启动

watch ntpq -p
-------------------------------------------------------------------------------
结果类似于:
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
 LOCAL(0)        .LOCL.          10 l  150   64   74    0.000    0.000   0.000
+golem.canonical 134.71.66.21     2 u    4   64   75  319.753    8.247  25.024
 alphyn.canonica 132.163.96.1     2 u    3   64   17  277.850   30.059  30.159
*pugot.canonical 17.253.108.125   2 u    6   64   57  298.084   31.296  35.446
+chilipepper.can 17.253.108.253   2 u   13   64   37  313.275   16.580  32.254

集群中请求同步(client)配置

1.使用ntpdate请求服务(服务启动需要一段时间)

ntpdate hadoop0(服务对应域名/ip)
------------------------------------------------------------------------------------------
16 Oct 23:15:49 ntpdate[6920]: adjust time server 192.168.50.100 offset 0.088294 sec

2.使用crontab完成定时同步请求任务

crontab -e
*/1 * * * * /usr/sbin/ntpdate hadoop0

可能出现的问题

16 Oct 22:59:49 ntpdate[2392]: no server suitable for synchronization found
# 这很可能是服务还未启动好,需要耐心等待5-10分钟
# 除此之外,还有可能是防火墙/配置文件存在notrust属性
# restrict 192.168.50.0 mask 255.255.255.0 notrust