文章目录

  • centos 7 配置ntp时间服务器
      • 参考地址:
    • 1. 服务端
      • 1.1 安装ntp服务和ntpdate工具
      • 1.2 修改配置文件
      • 1.3 设置开机自启(初始化)
      • 1.4 启用ntp服务
      • 1.5 设置硬件时间
      • 1.6 查看 ntp 情况
      • 1.7 执行同步
    • 2. 客户端
      • 2.1 安装ntp服务和ntpdate工具
      • 2.2 配置时间同步
        • 2.2.1 方法一
        • 2.2.2 方法二

centos 7 配置ntp时间服务器

最终目的:将我们的一台主机配置成ntp服务器,同网段的其他主机可以通过ntpdate -u host-addr命令以ntp服务 器的时间来进行客户端的时间同步。

参考地址:

https://blog.csdn.net/leezsj/article/details/118417954

1. 服务端

1.1 安装ntp服务和ntpdate工具

我们需要先安装 ntp 服务和 ntpdate 工具:yum -y install ntp ntpdate 即使是作为服务端的主机,在必要时刻也是 需要向公用的 ntp 服务器进行时间同步的(一般不用)。

yum -y install ntp ntpdate

1.2 修改配置文件

vim /etc/ntp.conf# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).driftfile /var/lib/ntp/drift# 默认情况下,NTP服务器的日志保存在 /var/log/messages.当然我们也可以自己指定
# 自己指定日志目录
# 我们要确保他的属性和SELinux环境(这两项一般不用改)
# chown ntp:ntp /var/log/ntpd.log
# chcon -t ntpd_log_t /var/log/ntpd.log
logfile /var/log/ntpd.log# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1# Hosts on local network are less restricted.
# 授权下述网段上所有的机器允许从ntp服务器上查询和同步时间
restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 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# 增加下述几个时间服务器列表,除了0.asia.pool.ntp.org还会有很多时间服务器.比如0.cn.pool.ntp.org或者
time.nist.gov或者
server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst#broadcast 192.168.1.255 autokey   # broadcast server
#broadcastclient            # broadcast client
#broadcast 224.0.1.1 autokey        # multicast server
#multicastclient 224.0.1.1      # multicast client
#manycastserver 239.255.254.254     # manycast server
#manycastclient 239.255.254.254 autokey # manycast client# Enable public key cryptography.
#crypto#这两行内容表示当外部时间不可用时,使用本地时间
server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10
##下述四行表示允许上层服务器修改本机时间
restrict 0.asia.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.asia.pool.ntp.org nomodify notrap noquery
restrict 3.asia.pool.ntp.org nomodify notrap noqueryincludefile /etc/ntp/crypto/pw# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys# Specify the key identifiers which are trusted.
#trustedkey 4 8 42# Specify the key identifier to use with the ntpdc utility.
#requestkey 8# Specify the key identifier to use with the ntpq utility.
#controlkey 8# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

1.3 设置开机自启(初始化)

# 使服务端服务ntp的守护进程ntpd生效
systemctl enable ntpd# 使客户端工具ntpdate工具生效(选做)
systemctl enable ntpdate# systemctl 检查
systemctl is-enabled ntpd

1.4 启用ntp服务

# 启动 ntp 服务
systemctl start ntpd# 查看 ntp 进程
ps -ef | grep ntpd

注意查看是否开启防火墙配置

1.5 设置硬件时间

对时间的解释

linux 的时间分为系统时间和硬件时间。

系统时间:通常在开机时复制硬件时间,之后独立运行并保存了时间、时区和夏令时设置。

通过 date 命令设置。硬件时间:(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。

通过 hwclock 命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w 同步前需要测试 ntp 上层服务器的连通性

1.6 查看 ntp 情况

ntpq -p
参数 解释说明
remote 即NTP主机的IP或主机名称.注意最左边是+表示目前正在起作用的上层NTP,如果是*表示这个也连接上了,不过是作为次要联机的NTP主机
refid 参考的上一层NTP主机的地址
st 即stratum阶层
t 是连接类型. u代表单播(unicast) l代表本地(local) ,其他的还有多播(multicast),广播(broadcast)
when 这个时间之前刚刚做过时间同步
poll 下次更新在几秒之后
reach 已经向上层NTP服务器要求更新的次数
delay 网络传输过程中的延迟时间
offset 时间补偿的结果
jitter Linux系统时间和Bios硬件时间的差异时间

1.7 执行同步

hwclock -wntpstat

2. 客户端

2.1 安装ntp服务和ntpdate工具

yum -y install ntp ntpdate

2.2 配置时间同步

2.2.1 方法一

配置用于同步的服务器

# 1. 先执行 hwclock -w 让系统时间和 bios 时间同步
hwclock -w# 2. 在执行如下命令
echo "server 10.0.0.210" >/etc/ntp.conf# 3. 重启服务以使配置生效,之后大概要等几分钟左右,才会同步成功
systemctl enable ntpd
systemctl restart ntpd

这样做的好处:

  1. 客户端的ntpd服务始终运行着,定期同步时间,不用我们每次都手动同步或者写定时器
  2. ntpd服务是慢慢改变时间直至标准时间

注意点:

最好先执行 hwclock -w ,否则如果bios时间和系统时间差异超过了30分钟,就会报错

FAQ:系统报错-Sep 25 12:23:33 localhost kerne: set_rtc_mmss: can’t update from 3 to 22

2.2.2 方法二

进行客户端与服务器端的时间同步

[root@slave1 ~]# systemctl enable ntpdate
[root@slave1 ~]# /usr/sbin/ntpdate -u 192.168.10.20028 Mar 04:54:43 ntpdate[1727]: adjust time server 192.168.10.200 offset 0.000001 sec

让系统时间和硬件时间同步

[root@slave1 ~]# hwclock -w

可以设置定时器,定时执行,因为 ntpdate 每次执行完就失效了

[root@slave1 ~]# crontab -e
每天和主机同步一次
10 23 * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log

常见错误分析:

客户端 执行 ntpdate master 显示: no server suitable for synchronization found
原因:

​ 错误 Server dropped: Strata too high (在客户端执行 ntpdate -d master 可以看到,且显示 stratum 16 。而正常情况下 stratum 这个值得范围是“0~15”)
解决:
这是因为 NTP server 还没有和其自身或者它的 server 同步上。
以下的定义是让 NTP Server 和其自身保持同步,如果在 /ntp.conf 中定义的 server 都不可用时,将使用 local 时间作为 ntp 服务提供给 ntp 客户端。

server 127.127.1.0
fudge 127.127.1.0
stratum 8

centos 7 配置ntp时间服务器相关推荐

  1. CentOS 下配置NTP时间服务器

    NTP服务器[Network Time Protocol(NTP)]是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化. 近来发现使用ntpdate ...

  2. Centos下部署NTP时间服务器同步环境

    Centos下部署NTP时间服务器同步环境记录 我是在购买的3A平台购买的服务器进行测试. 服务端部署 安装所需软件包 [root@test ~]# yum -y install ntp ntpdat ...

  3. Redhat6.5下配置NTP时间服务器

    Redhat6.5下配置NTP时间服务器 在配置服务之前需要知道的一些概念: [整篇文章有很多是借鉴别人的,少部分是自己编写的,希望能对大家有所裨益] 1.时间和时区 如果有人问你说现在几点? 你看了 ...

  4. 配置ntp时间服务器,确保客户端主机能和服务主机同步时间

    配置ntp时间服务器,确保客户端主机能和服务主机同步时间 1配置Chrony服务器 先下载chrony dnf install -y chrony 查看和配置chrony.conf文件 rpm -qc ...

  5. Linux配置ntp时间服务器

    Linux配置ntp时间服务器 时间服务器作用: 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同步的标准时间用于记录各种事件发生时序, 如E-MAIL信息.文件创建和访问时间.数据库处理 ...

  6. Linux基础:配置ntp时间服务器

    1. 概述 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同步的标准时间用于记录各种事件发生时序,如E-MAIL信息.文件创建和访问时间.数据库处理时间等.大数据系统内不同计算设备之间控制 ...

  7. linux系统安装ntp,Linux系统安装配置NTP时间服务器

    NTP(The Network Time Protocol) 是网络时间协议,用以同步网络内计算机的时间. 它通过udp包交换,用特定算法进行协商,从而把计算机上的时间与时间服务器上的 时间保持一致. ...

  8. centos下部署NTP时间服务器同步环境记录

    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 30 31 32 33 34 35 36 3 ...

  9. Linux下 配置NTP时间服务器

    多台linux服务器时间同步 时间服务器作用: 若计算机时间不同步,如E-MAIL信息.文件创建和访问时间.数据库处理时间等这些应用或操作或将无法正常进行. 时间同步是业务能够得到正确处理的基础保障. ...

最新文章

  1. Android 相对布局别自己快遗忘的属性layout_alignRight,layout_alignBottom,layout_alignTop,layout_alignLeft
  2. 生产者消费者模式 php 【转】
  3. 最全面的Android Studio使用教程【申明:来源于网络】
  4. jfinal poi
  5. visual studio 2010常用快捷键
  6. 蓝牙配对不弹配对框_配对编程在工作场所的好处和陷阱
  7. python应聘要求_python爬取招聘要求等信息实例
  8. 北大青鸟网络工程师benet培训招生问答
  9. linux---常用命令(二)
  10. mac 电脑使用小鹤音形和小鹤双拼
  11. JS将任意格式的时间转为Date对象
  12. Dojo.Book中文版
  13. vim+Vundle+vim-autoformat+astyle格式化代码
  14. 【python机器学习基础教程】(四)
  15. 虚拟页式存储管理——页面置换算法及其影响的缺页中断率
  16. php调用itunes,使用PHP将SQL列从秒转换为iTunes兼容的播客时间格式
  17. Java基础知识学习笔记总结
  18. 大数据-Hadoop集群测试
  19. VIA图像标注系统汉化版
  20. 软件测试梳理 第九节 缺陷和缺陷报告

热门文章

  1. 菜鸟浅谈——web安全测试
  2. AI视频动画制作《狼来了》全揭秘!
  3. 程序员的自我修养之数学基础06:秩(线性无关和线性相关,秩的意义、秩的求法、奇异矩阵和非奇异矩阵)
  4. mysql在xml中jdbctype_MyBatis中的JdbcType映射介绍
  5. python selenium 给textarea赋值
  6. py计时器 为了孙曌凌/CSDN
  7. JavaScript setTimeout用法,js setTimeout带参数
  8. 2014华为成都实习招聘软件类机试回忆录
  9. Hologres笔记
  10. 几何级数的计算机验证法c语言解析,c语言编程20例.doc