Centos7安装配置NTP时间服务器

匿名 (未验证) 提交于 2019-12-02 22:56:40

Centos7安装配置NTP时间服务器


1.查看当前服务器时区&列出时区并设置时区
[root@mail ~]# timedatectl                              #查看时区       Local time: Tue 2018-08-07 14:09:35 CST   Universal time: Tue 2018-08-07 06:09:35 UTC         RTC time: Tue 2018-08-07 06:09:33        Time zone: Asia/Shanghai (CST, +0800)      NTP enabled: no NTP synchronized: yes  RTC in local TZ: no       DST active: n/a
[root@mail ~]# timedatectl list-timezones               #列出所有时区 [root@mail ~]# timedatectl list-timezones  | grep Asia/Shanghai Asia/Shanghai
[root@mail ~]# timedatectl set-timezone Asia/Shanghai   #设置时区为上海

2.时区概念理解

GMT、UTC、CST、DST

UTC

整个地球分为二十四个时区,每个时区都有自己的本地时间,在国际无线电通电场合,为了统一起见,使用一个统一时间,称为通用协调 时间

GMT

格林威治标准时间(Greenwich Mean Time)指于英国伦敦郊区的×××格林尼治天文的标准时间,因为本初子午线被定义在通过那里的经 线(UTC与GMT时间基本相同)

CST

中国标准时间(China Standard Time) GMT+8=UTC+8=CST

DST

夏令时(Daylight Saving Time)指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用(中国不使用)

3.NTP的安装配置

  • 查看是否安装
[root@mail ~]# rpm -qa | grep ntp ntp-4.2.6p5-25.el7.centos.2.x86_64 fontpackages-filesystem-1.44-8.el7.noarch ntpdate-4.2.6p5-25.el7.centos.2.x86_64
[root@mail ~]# yum install ntpdate ntp -y           #如果没安装则安装
  • 修改NTP配置

    [root@mail ~]# egrep -v "#|^$" /etc/ntp.conf
    driftfile /var/lib/ntp/drift

    #新增:日志文件
    logfile /var/log/ntpd.log
    restrict default kod nomodify notrap nopeer noquery
    restrict 127.0.0.1
    restrict ::1

    #授权192.168.1.0网段上所有机器可以从这台机器上查询和时间同步
    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

    #新增:时间服务器列表
    server 0.cn.pool.ntp.org iburst
    server 1.cn.pool.ntp.org iburst
    server 2.cn.pool.ntp.org iburst
    server 3.cn.pool.ntp.org iburst

    #新增:当外部时间不可用时,使用本地时间
    server 192.168.1.1 biurst
    fudge 127.0.0.1 stratum 10

    #新增:允许上层时间服务器主动修改本机时间
    restrict 0.cn.pool.ntp.org nomodify notrap noquery
    restrict 1.cn.pool.ntp.org nomodify notrap noquery
    restrict 2.cn.pool.ntp.org nomodify notrap noquery

    includefile /etc/ntp/crypto/pw
    keys /etc/ntp/keys
    disable monitor

  • 设置系统开机自启动

    [root@mail ~]# systemctl enable ntpd
    [root@mail ~]# systemctl enable ntpdate

  • NTP服务启动时,先使用ntpdate命令同步时间

    [root@mail ~]# ntpdate -u 1.cn.pool.ntp.org
    7 Aug 14:41:45 ntpdate[7427]: adjust time server 120.25.115.19 offset -0.002322 sec
    ntpdate -u:指定使用无特权的端口发送数据包。 当在一个对特权端口的输入流量进行阻拦的防火墙后是很有益的, 并希
    望在防火墙之外和主机同步。防火墙是一个系统或者计算机,它控制从外网对专用网的访问。
    原因之一:当服务端与客户端之间的时间误差过大时,此时修改时间可能对系统或应用带来不可预知问题,NTP会停止时间同步。
    若检查NTP启动后时间未同步时,应考虑可能是时间误差过大所致,此时需要先手动进行时间同步

  • 启动NTP服务器

    注意:先启动ntpdate后启ntpd
    [root@mail ~]# systemctl start ntpdate
    [root@mail ~]# systemctl start ntpd

  • 查看ntp连接状态,如果没有问题,将正确时间写入硬件

    [root@mail ~]# ss -tlunp | grep ntp
    udp UNCONN 0 0 192.168.1.1:123 : users:(("ntpd",pid=8034,fd=19))
    udp UNCONN 0 0 127.0.0.1:123 : users:(("ntpd",pid=8034,fd=18))
    udp UNCONN 0 0 :123 : users:(("ntpd",pid=8034,fd=16))
    udp UNCONN 0 0 :::123 :::
    users:(("ntpd",pid=8034,fd=17))

    [root@mail ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter

    *ntp7.flashdance 192.36.143.152 2 u 30 64 7 273.055 7.649 25.571
    ntp6.flashdance 194.58.202.20 2 u 29 64 7 224.037 19.673 30.313
    b.hnd.pobot.net 255.254.0.28 2 u 98 64 6 171.231 84.197 11.033
    static-5-103-13 .GPS. 1 u 95 64 2 231.730 22.122 0.694
    192.168.1.1 .INIT. 16 u - 64 0 0.000 0.000 0.000

ntpq:ntpq 命令使用 NTP 模式 6 数据包与 NTP 服务器通信,能够在允许的网络上查询任意的兼容的服务器 -c  SubCommand  指定交互式格式的命令。此标志添加 SubCommand 到运行在指定主机上的命令列表。 输入多个 -c 标志。 -i  指定交互方式。标准输出显示提示,标准输入读取命令。 -n  以点十进制格式(x.x.x.x)显示所有的主机地址,而不是规范的主机名。 -p  显示服务器同级设备的列表,并显示一个它们状态的总结。如同使用 peers 子命令
[root@mail ~]# hwclock -w hwclock:命令是一个硬件时钟访问工具,它可以显示当前时间、设置硬件时钟的时间和设置硬件时钟为系统时间,也可设置系统时间为硬件时钟的时间 在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。 当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。  hwclock选项: -r, --show        读取并打印硬件时钟(read hardware clock and print result ) -s, --hctosys     将硬件时钟同步到系统时钟(set the system time from the hardware clock ) -w, --systohc     将系统时钟同步到硬件时钟(set the hardware clock to the current system time ) --adjust          hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟; --debug           显示hwclock执行时详细的信息; --directisa       hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟; --hctosys         将系统时钟调整为与目前的硬件时钟一致; --set --date=<日期与时间>:设定硬件时钟; --show            显示硬件时钟的时间与日期; --systohc         将硬件时钟调整为与目前的系统时钟一致; --test            仅测试程序,而不会实际更改硬件时钟; --utc             若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作; --version         显示版本信息。

系统时间与硬件时间

硬件时间

RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行,仅保持日期和时间数值,无法保存时区 和夏令时设置

系统时间

一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置

客户端

以服务进程方式实时同步(需安装NTP)

vim /etc/ntp.conf server 192.168.1.1 systemctl restart ntpd

crontab 任务计划同步时间(需安装ntpdate,每天24点更新同步时间)

crontab -l 0 0 * * * /usr/sbin/sntp -P no -r 192.168.1.1;hwclock -w
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!