ntp时间同步

环境说明:
现有三台主机:dfs01 dfs02 和dfs03 系统是centos7.4
需求:以dfs01为ntp主服务器,其他连个节点来与其同步。

dfs01:服务端

1.安装ntp服务,配置服务(/etc/ntp.conf),最后启动ntp服务

[root@dfs01 ~]# yum install -y ntp  
[root@dfs01 ~]# vim  /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 0.asia.pool.ntp.org     //这个是互联网同步亚洲时区
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org

server 127.127.1.0 iburst local clock    //这步很重要,如果该服务器网络出问题了,就同步自己的本地时间
restrict 192.168.100.0  mask 255.255.255.0  nomodify  notrap     这个是允许客户端192.168.100段的地址可以同步该时间同步服务器。
然后启动服务
[root@dfs01 ~]# systemctl start ntpd
查看ntp服务状态
    [root@dfs01 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-07-30 10:23:50 CST; 33min ago
  Process: 11411 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11413 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─11413 /usr/sbin/ntpd -u ntp:ntp -g

Jul 30 10:39:01 dfs01.crunii.com ntpd_intres[11415]: host name not found: 3.asia.pool.ntp.org
Jul 30 10:39:51 dfs01.crunii.com ntpd[11413]: 0.0.0.0 0512 02 freq_set kernel 0.000 PPM
Jul 30 10:39:51 dfs01.crunii.com ntpd[11413]: 0.0.0.0 0515 05 clock_sync
Jul 30 10:54:04 dfs01.crunii.com ntpd[11413]: Deleting interface #6 ens32, fe80::f532:9f52:82:e73d#123, interface stats: received=0, sent=0, dropped=0, active_time=1814 secs
Jul 30 10:54:06 dfs01.crunii.com ntpd[11413]: Listen normally on 7 ens32 fe80::f532:9f52:82:e73d UDP 123
Jul 30 10:54:06 dfs01.crunii.com ntpd[11413]: new interface(s) found: waking up resolver
Jul 30 10:54:08 dfs01.crunii.com ntpd_intres[11415]: DNS 0.asia.pool.ntp.org -> 124.108.20.1
Jul 30 10:54:08 dfs01.crunii.com ntpd_intres[11415]: DNS 1.asia.pool.ntp.org -> 129.250.35.251
Jul 30 10:54:08 dfs01.crunii.com ntpd_intres[11415]: DNS 2.asia.pool.ntp.org -> 62.201.225.9
Jul 30 10:54:08 dfs01.crunii.com ntpd_intres[11415]: DNS 3.asia.pool.ntp.org -> 133.243.238.163
[root@dfs01 ~]# ntpq -p  //ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系。*表示目前使用的ntp server,这里选择的本机;
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l   61   64  377    0.000    0.000   0.000
 124-108-20-1.st 127.67.113.92    2 u   32   64   17  212.961  103649.   2.832
 y.ns.gin.ntt.ne 249.224.99.213   2 u   94   64   16  256.639  103563.   0.290
 time.iqnet.com  62.201.214.162   2 u   32   64   17  287.642  103648.   4.689
 ntp-b2.nict.go. .NICT.           1 u   32   64   17  184.827  103597.   2.250

dfs02 和dfs03 客户端

都只需执行 ntpdate dfs01 与服务端dfs01同步即可;然后再写个定时任务,每小时同步一次。

[root@dfs02 ~]# crontab -l
15 * * * *  /usr/sbin/ntpdate  dfs01 >>/var/log/ntpdate.log   2>&1
[root@dfs02 ~]# 

注意:如果服务器要进行时间同步,最好都先检查一下自己的时区和客户端的时区是否一致。

[root@dfs02 ~]# timedatectl 
      Local time: Tue 2019-07-30 11:09:52 CST     //当前时间
  Universal time: Tue 2019-07-30 03:09:52 UTC
        RTC time: Tue 2019-07-30 03:09:52
       Time zone: Asia/Shanghai (CST, +0800)    //所在的时区
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@dfs02 ~]# 
[root@dfs02 ~]# timedatectl  --help   //通过命令帮助修改不正确的时区
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled

你可能感兴趣的:(linux)