chronyd //ntp服务,是一个进程,需要百度一下,了解一下
使用chrony代替ntp同步时间 : https://blog.csdn.net/qq_36330643/article/details/77868337
https://blog.csdn.net/hnhuangyiyang/article/details/52711631
https://blog.csdn.net/linuxprobe18/article/details/80460068 //参考此3个链接
[root@lb01 ~]# yum install chrony -y
[root@lb01 ~]# rpm -qa|grep chrony
chrony-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 iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 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命令时间时区操作详解:https://www.cnblogs.com/zhi-leaf/p/6282301.html
CentOS 6 时间,时区,设置修改及时间同步: https://blog.csdn.net/testcs_dn/article/details/39803919
在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.conf
server ntp1.aliyun.com iburst
server time.nist.gov iburst
server time.windows.com iburst //增加以上三个
stratumweight 0
driftfile /var/lib/chrony/drift
makestep 10 3
rtcsync
allow 192.168.10.0/16 //允许这个网段的来同步自己的时间
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
[root@lb01 ~]# service chronyd start
Starting chronyd: [ OK ]
[root@lb01 ~]# service chronyd status
chronyd (pid 1823) is running...
[root@lb01 ~]# chronyc -a makestep //设置完时区后,强制同步下系统时钟:
200 OK
200 OK
[root@lb01 ~]# chkconfig chronyd on
[root@lb01 ~]# date +%F\ %T //此时的时间与当时时间是准的
2018-12-07 15:38:44
[root@lb01 ~]# ps -ef|grep chrony
chrony 1823 1 0 15:34 ? 00:00:00 /usr/sbin/chronyd -u chrony
root 1849 1707 0 15:46 pts/0 00:00:00 grep --color=auto chrony
[root@lb01 ~]# netstat -lntup
Active 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 0
driftfile /var/lib/chrony/drift
makestep 10 3
rtcsync
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
[root@lb02 ~]# service chronyd status
chronyd is stopped
[root@lb02 ~]# service chronyd start
Starting 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 sources
210 Number of sources = 1
MS 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 -v
210 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 tracking
Reference ID : 0.0.0.0 ()
Stratum : 0
Ref time (UTC) : Thu Jan 1 00:00:00 1970
System time : 0.000000000 seconds fast of NTP time
Last offset : +0.000000000 seconds
RMS offset : 0.000000000 seconds
Frequency : 0.000 ppm fast
Residual freq : +0.000 ppm
Skew : 0.000 ppm
Root delay : 0.000000 seconds
Root dispersion : 0.000000 seconds
Update interval : 0.0 seconds
Leap 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: yes
NTP 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: yes
NTP synchronized: yes //表示已经同步上了
RTC in local TZ: no
DST active: n/a
重新搞了个从的服务器(客户端)
修改了主服务器的配置,然后重启了chronyd服务
[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf
server ntp1.aliyun.com iburst
server time.nist.gov iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.10.0/16
logdir /var/log/chrony
在从服务器上直接安装配置好
[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf
server 192.168.10.129 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /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: yes
NTP synchronized: yes //成功了,时间和主服务器上是相同的
RTC in local TZ: no
DST active: n/a
转载于:https://blog.51cto.com/12445535/2370402