1. 服务端上的操作
1.1 安装ntp并启动
[root@bigdata001 ~]#
[root@bigdata001 ~]# yum install ntp ntpdate
[root@bigdata001 ~]#
[root@bigdata001 ~]# systemctl start ntpd
[root@bigdata001 ~]# systemctl enable ntpd
[root@bigdata001 ~]#
1.2 修改/etc/ntp.conf
以下几行添加注释:
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
在上面的注释前面,添加内容如下:
#局域网设置:server 127.0.0.1 iburst
server cn.ntp.org.cn iburst
# 配置允许上游时间服务器主动修改本机的时间
restrict cn.ntp.org.cn nomodify notrap noquery
1.3 重启ntp服务
[root@bigdata001 ~]#
[root@bigdata001 ~]# systemctl restart ntpd
[root@bigdata001 ~]#
[root@bigdata001 ~]# ntpstat
synchronised to NTP server (111.230.50.201) at stratum 4
time correct to within 1083 ms
polling server every 64 s
[root@bigdata001 ~]#
2. 客户端上的操作
2.1 安装ntp并启动
[root@bigdata002 ~]#
[root@bigdata002 ~]# yum install ntp ntpdate
[root@bigdata002 ~]#
[root@bigdata002 ~]# systemctl start ntpd
[root@bigdata002 ~]# systemctl enable ntpd
[root@bigdata002 ~]#
2.2 修改/etc/ntp.conf
以下几行添加注释:
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
在上面的注释前面,添加内容如下:
server bigdata001 iburst
# 配置允许上游时间服务器主动修改本机的时间
restrict bigdata001 nomodify notrap noquery
2.3 重启ntp服务
[root@bigdata002 ~]#
[root@bigdata002 ~]# systemctl restart ntpd
[root@bigdata002 ~]#
[root@bigdata002 ~]# ntpstat
synchronised to NTP server (192.168.xxx.xxx) at stratum 5
time correct to within 1293 ms
polling server every 64 s
[root@bigdata002 ~]#
3. 修改服务端和客户端的时间
[root@bigdata001 ~]#
[root@bigdata001 ~]# date -s '2021-01-01'
2021年 01月 01日 星期五 00:00:00 CST
[root@bigdata001 ~]#
[root@bigdata001 ~]# date
2021年 01月 01日 星期五 00:00:02 CST
[root@bigdata001 ~]#
[root@bigdata002 ~]#
[root@bigdata002 ~]# date -s '2021-01-01'
2021年 01月 01日 星期五 00:00:00 CST
[root@bigdata002 ~]#
[root@bigdata002 ~]# date
2021年 01月 01日 星期五 00:00:04 CST
[root@bigdata002 ~]#
等一段时间后,查看服务端和客户端的时间
[root@bigdata001 ~]#
[root@bigdata001 ~]# date
2021年 10月 13日 星期三 11:37:18 CST
[root@bigdata001 ~]#
[root@bigdata002 ~]#
[root@bigdata002 ~]# date
2021年 10月 13日 星期三 11:37:34 CST
[root@bigdata002 ~]#