【ntpdate】CentOS7.x上使用ntpdate同步ntp服务器

【ntpdate】CentOS7.x上使用ntpdate同步ntp服务器,第1张

NTP服务器顾名思义就是时间同步服务器(Network Time Protocol),Linux下的ntp服务器配置相对来说都比较容易,但在Linux下有一个弊端, 不同时区或者说是时间相差太大的无法同步 ,所以在配置ntp服务器之前需要把时间配置成相同的。

NTP时钟同步方式说明

NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:

直接同步

使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。

平滑同步

使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。

标准时钟同步服务

http://www.pool.ntp.org/zone/cn

这个网站包含全球的标准时间同步服务,也包括对中国时间的同步,对应的URL为:cn.pool.ntp.org

在其中也描述了ntp配置文件中的建议写法:

server  1.cn.pool.ntp.org

server  2.asia.pool.ntp.org

server  3.asia.pool.ntp.org

实验室集群没有联网,我们需要搭建ntp服务器并进行时间同步。

现使用的系统为centos7.2,机器使用情况如下表所示,这里以192.168.1.102为ntp server,192.168.1.104为client对时间进行同步。

NTP server         192.168.1.102

NTP client           192.168.1.104

1.在集群中所有节点上安装ntp

# yum  -y  install ntp

2.所有节点设置时区,这里设置为中国所用时间

# timedatectl set-timezone Asia/Shanghai 

3.在server节点上启动ntp服务

# systemctl startntpd

# systemctl enable ntpd

4.在server节点上设置现在的准确时间

# timedatectl set-time HH:MM:SS 

5.在server节点上设置其ntp服务器为其自身,同时设置可以接受连接服务的客户端,是通过更改/etc/ntp.conf文件来实现,其中server设置127.127.1.0为其自身,新增加一个 restrict 段为可以接受服务的网段

 # vim  /etc/ntp.conf

6.重启ntpd服务 

# systemctl restart ntpd 

# timedatectl

NTP synchronized: yes

启用ntpd后,服务器就开启了ntpd自动同步,无法使用 timedatectl set-time HH:MM:SS重新设置时间。

如果要使用timedatectl set-time HH:MM:SS 重新设置时间:

#  systemctl stop  ntpd 

# timedatectl set-ntp  false

# timedatectl set-time HH:MM:SS

# hwclock  -w

#  systemctl start  ntpd 

1.客户端时区需要和服务端保持一致,否则无法同步时间

# timedatectl set-timezone   

Asia/Shanghai 

2.安装同步软件包

# yum   -y install  ntpdate

3. 修改/etc/sysconfig/ntpdate,让ntpdate每次同步时间之后把时间写入hwclock,相当于命令hwclock -w

将最后一行SYNC_HWCLOCK=no修改为:SYNC_HWCLOCK=yes

#  vim  /etc/sysconfig/ntpdate

4.客户端定时任务配置

# crontab  -e

59  23  *  *  *   /sbin/ntpdate  192.168.1.102

设置为每天23:59分执行,重启crond服务

######################

定时任务基本格式 : 

***** command 

分 时 日 月 周 命令 

第1列表示分钟1~59 每分钟用*或者 */1表示 

第2列表示小时1~23(0表示0点) 

第3列表示日期1~31 

第4列表示月份1~12 

第5列标识号星期0~6(0表示星期天) 

第6列要运行的命令

################################ 

5.重启定时任务

# systemctl  restart crond.service

*** 作系统:CentOS7.x 

ntp_sever:192.168.1.110

ntp_client:192.168.1.120

# yum -y install  ntp ntpdate

# firewall-cmd --zone=public --add-port=123/udp --permanent

# firewall-cmd --reload

# vim  /etc/ntp.conf

########################################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

restrict 192.168.1.0 mask 255..255.255.0 nomodify notrap

server 192.168.1.110 iburst

server 127.127.1.0

fudge 127.127.1.0 stratum 10

interface ignore  wildcard

interface listen  192.168.1.110

interface listen  127.0.0.1

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

##########################################

# systemctl start ntpd

# systemctl enable ntpd

# firewall-cmd --zone=public --add-port=123/udp --permanent

# firewall-cmd --reload

# vim  /etc/ntp.conf

#########################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

server 192.168.1.110 iburst

restrict 192.168.1.110 nomodify notrap noquery

server 127.127.1.0

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

#########################

# ntpdate 192.168.1.110

# systemctl  start   ntpd

# systemctl enable ntpd


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11299824.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存