本文共 10249 字,大约阅读时间需要 34 分钟。
chronyd //ntp服务,是一个进程,需要百度一下,了解一下
使用chrony代替ntp同步时间 : //参考此3个链接[root@lb01 ~]# yum install chrony -y[root@lb01 ~]# rpm -qa|grep chronychrony-2.1.1-2.el6_8.x86_64[root@lb01 ~]# cat /etc/chrony.conf //默认配置文件# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).server 0.rhel.pool.ntp.org iburstserver 1.rhel.pool.ntp.org iburstserver 2.rhel.pool.ntp.org iburstserver 3.rhel.pool.ntp.org iburst# Ignore stratum in source selection.stratumweight 0# Record the rate at which the system clock gains/losses time.driftfile /var/lib/chrony/drift# In first three updates step the system clock instead of slew# if the adjustment is larger than 10 seconds.makestep 10 3# Enable kernel synchronization of the real-time clock (RTC).rtcsync# Allow NTP client access from local network.#allow 192.168/16# Serve time even if not synchronized to any NTP server.#local stratum 10# Specify file containing keys for NTP and command authentication.keyfile /etc/chrony.keys# Specify key number for command authentication.commandkey 1# Generate new command key on start if missing.generatecommandkey# Disable logging of client accesses.noclientlog# Send message to syslog when clock adjustment is larger than 0.5 seconds.logchange 0.5# Specify directory for log files.logdir /var/log/chrony# Select which information is logged.#log measurements statistics tracking
Linux centos7 下使用timedatectl命令时间时区操作详解:
CentOS 6 时间,时区,设置修改及时间同步: 在centos7上执行 timedatectl set-ntp true //使用网络时间同步在CentOS 6版本,时间设置有date、hwclock命令,从CentOS 7开始,使用了一个新的命令timedatectl。[root@lb01 ~]# date +%F\ %T 2018-12-07 23:31:37[root@lb01 ~]# grep '^[a-Z]' /etc/chrony.confserver ntp1.aliyun.com iburstserver time.nist.gov iburstserver time.windows.com iburst //增加以上三个stratumweight 0driftfile /var/lib/chrony/driftmakestep 10 3rtcsyncallow 192.168.10.0/16 //允许这个网段的来同步自己的时间keyfile /etc/chrony.keyscommandkey 1generatecommandkeynoclientloglogchange 0.5logdir /var/log/chrony[root@lb01 ~]# service chronyd startStarting chronyd: [ OK ][root@lb01 ~]# service chronyd statuschronyd (pid 1823) is running...[root@lb01 ~]# chronyc -a makestep //设置完时区后,强制同步下系统时钟:200 OK200 OK[root@lb01 ~]# chkconfig chronyd on [root@lb01 ~]# date +%F\ %T //此时的时间与当时时间是准的 2018-12-07 15:38:44[root@lb01 ~]# ps -ef|grep chronychrony 1823 1 0 15:34 ? 00:00:00 /usr/sbin/chronyd -u chronyroot 1849 1707 0 15:46 pts/0 00:00:00 grep --color=auto chrony[root@lb01 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1562/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1638/master tcp 0 0 :::22 :::* LISTEN 1562/sshd tcp 0 0 ::1:25 :::* LISTEN 1638/master udp 0 0 127.0.0.1:323 0.0.0.0:* 1823/chronyd udp 0 0 ::1:323 :::* 1823/chronyd [root@lb01 ~]# chronyc sources -v //验证服务端是否能同步时间210 Number of sources = 3 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined,| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.|| .- xxxx [ yyyy ] +/- zzzz|| Reachability register (octal) -. | xxxx = adjusted offset,|| Log2(Polling interval) --. | | yyyy = measured offset,|| \ | | zzzz = estimated error.|| | | \MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^* 120.25.115.20 2 8 377 496 -2413us[-2900us] +/- 22ms^? time-a-g.nist.gov 0 9 0 10y +0ns[ +0ns] +/- 0ns^- 52.168.138.145 2 8 377 97 +16ms[ +16ms] +/- 182ms以上服务器端配置完成
lb01作为服务器端 1b02 作为客户端,来同步他的时间[root@lb02 ~]# date +%F\ %T //此时时间是不准的2018-12-07 23:41:17[root@lb02 ~]# yum -y install chrony[root@lb02 ~]# grep '^[a-Z]' /etc/chrony.conf server 192.168.10.160 iburst //添加这个stratumweight 0driftfile /var/lib/chrony/driftmakestep 10 3rtcsynckeyfile /etc/chrony.keyscommandkey 1generatecommandkeynoclientloglogchange 0.5logdir /var/log/chrony[root@lb02 ~]# service chronyd statuschronyd is stopped[root@lb02 ~]# service chronyd startStarting chronyd: [ OK ][root@lb02 ~]# chkconfig chronyd on[root@lb02 ~]# chronyc sources -v //验证同步的源210 Number of sources = 1 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined,| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.|| .- xxxx [ yyyy ] +/- zzzz|| Reachability register (octal) -. | xxxx = adjusted offset,|| Log2(Polling interval) --. | | yyyy = measured offset,|| \ | | zzzz = estimated error.|| | | \MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^? 192.168.10.160 0 8 0 10y +0ns[ +0ns] +/- 0ns
———————————————————————————————————————————————————————————————————————————————————————————————————————————————
7.常用命令查看时间同步源:$ chronyc sources -v[root@lb02 ~]# chronyc sources210 Number of sources = 1MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^? 192.168.10.160 0 8 0 10y +0ns[ +0ns] +/- 0ns查看时间同步源状态:$ chronyc sourcestats -v[root@lb02 ~]# chronyc sourcestats -v210 Number of sources = 1 .- Number of sample points in measurement set. / .- Number of residual runs with same sign. | / .- Length of measurement set (time). | | / .- Est. clock freq error (ppm). | | | / .- Est. error in freq. | | | | / .- Est. offset. | | | | | | On the -. | | | | | | samples. \ | | | | | | |Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev==============================================================================192.168.10.160 0 0 0 +0.000 2000.000 +0ns 4000ms设置硬件时间硬件时间默认为UTC:$ timedatectl set-local-rtc 1启用NTP时间同步:$ timedatectl set-ntp yes校准时间服务器:$ chronyc tracking[root@lb02 ~]# chronyc trackingReference ID : 0.0.0.0 ()Stratum : 0Ref time (UTC) : Thu Jan 1 00:00:00 1970System time : 0.000000000 seconds fast of NTP timeLast offset : +0.000000000 secondsRMS offset : 0.000000000 secondsFrequency : 0.000 ppm fastResidual freq : +0.000 ppmSkew : 0.000 ppmRoot delay : 0.000000 secondsRoot dispersion : 0.000000 secondsUpdate interval : 0.0 secondsLeap status : Not synchronised
最后需要注意的是,配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。
————————————————————————————————————————————————————————————————————————————————————————————————————————————————**centos7系统**[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) 24 yum install chrony -y 25 rpm -qa|grep chrony 26 date 27 cat /etc/chrony.conf 28 grep '^[a-Z]' /etc/chrony.conf 29 vim /etc/chrony.conf 30 grep '^[a-Z]' /etc/chrony.conf 32 systemctl start chronyd 33 systemctl status chronyd 34 chronyc -a makestep 35 systemctl enable chronyd 36 date +%F\ %T 37 date -s 18:00:00 //我设置的错误的时间 38 date +%F\ %T 39 ps -ef|grep chrony 40 netstat -lntup 41 chronyc sources -v 42 date +%F\ %T 43 chronyc -a makestep [root@localhost ~]# timedatectl status Local time: Mon 2018-12-10 18:01:32 CST Universal time: Mon 2018-12-10 10:01:32 UTC RTC time: Mon 2018-12-10 10:43:59 Timezone: Asia/Shanghai (CST, +0800) NTP enabled: yesNTP synchronized: no //表示,还没有进行时间同步 RTC in local TZ: no DST active: n/a[root@localhost ~]# date +%F\ %T //此时的时间是正确的 2018-12-10 18:44:57[root@localhost ~]# timedatectl status Local time: Mon 2018-12-10 18:45:44 CST Universal time: Mon 2018-12-10 10:45:44 UTC RTC time: Mon 2018-12-10 10:45:43 Timezone: Asia/Shanghai (CST, +0800) NTP enabled: yesNTP synchronized: yes //表示已经同步上了 RTC in local TZ: no DST active: n/a重新搞了个从的服务器(客户端)修改了主服务器的配置,然后重启了chronyd服务[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf server ntp1.aliyun.com iburstserver time.nist.gov iburstdriftfile /var/lib/chrony/driftmakestep 1.0 3rtcsyncallow 192.168.10.0/16logdir /var/log/chrony在从服务器上直接安装配置好[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf server 192.168.10.129 iburstdriftfile /var/lib/chrony/driftmakestep 1.0 3rtcsynclogdir /var/log/chrony[root@localhost ~]# timedatectl Local time: Mon 2018-12-10 18:56:19 CST Universal time: Mon 2018-12-10 10:56:19 UTC RTC time: Mon 2018-12-10 10:56:19 Timezone: Asia/Shanghai (CST, +0800) NTP enabled: yesNTP synchronized: yes //成功了,时间和主服务器上是相同的 RTC in local TZ: no DST active: n/a
转载于:https://blog.51cto.com/12445535/2370402