标签(linux): chrony

笔者Q:972581034 交流群:605799367。有任何疑问可与笔者或加群交流

之前centos6我们一直用的ntp时间服务器,虽然到CentOS7上也可以装ntp。但是各种坑啊。这次换一个时间同步工具---->chrony

=环境

server端

[root@zabbix ~]# hostname
zabbix
[root@zabbix ~]# hostname -I
10.0.0.120 172.16.1.120

先说下环境,我这里是用ansible批量执行的。server端为外网为10.0.0.120。

目标是让客户端四台机器做到时间同步,一秒不差

小提示:在利用ansible批量分发文件的时候,覆盖文件是一件很危险的事,如果原文件存在,最好先备份。其实不管是ansible还是其它操作,覆盖都是很危险的
[root@zabbix ~]# cat /etc/ansible/hosts
[client]
172.16.1.51
172.16.1.52
172.16.1.53
172.16.1.250

防火墙关闭:

[root@zabbix ~]# systemctl status firewalld.service
???firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)

selinux关闭:

[root@zabbix ~]# getenforce
Disabled
[root@localhost ~]# systemctl status chrony
● chrony.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)

服务端=

1.安装chrony(所有机器)

yum install chrony -y

2.启动chrony

[root@zabbix ~]# systemctl start chronyd.service
[root@zabbix ~]# systemctl status chronyd.service
???chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2017-05-27 11:47:43 CST; 4s ago

3.编辑配置文件(注意:现在是服务器端的修改)

     22 allow 10.0.0.0/24
23
24 # Listen for commands only on localhost.
25 bindcmdaddress 127.0.0.1
26 bindcmdaddress ::1
27
28 # Serve time even if not synchronized to any NTP server.
29 local stratum 10 #第22行设置为本网段
#第29行的注释取消

4.查看配置文件如下

[root@zabbix ~]# egrep -v "#|^$" /etc/chrony.conf
server ntp1.aliyun.com
server time1.aliyun.com
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
allow 10.0.0.0/24
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
local stratum 10
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

5.重启时间同步服务

[root@zabbix ~]# systemctl restart chronyd.service

==客户端=

方法一

客户端的配置文件是同一个文件(/etc/chrony.conf)

1.删掉哪些没用的server xxxxxxxxxx iburst

1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/j oin.html).
3 server 10.0.0.120 iburst
4 # Ignore stratum in source selection.

2.在server端把配置文件编辑好然后用ansible批量分发过去

[root@zabbix ~]# ansible client -m copy -a "src=/root/chrony.conf dest=/etc/"
172.16.1.250 | SUCCESS => {
"changed": true,
"checksum": "52bda81d895de3c7c54886d342e5eec074df757e",
"dest": "/etc/chrony.conf",
"gid": 0,
"group": "root",
"md5sum": "aee9cc7faa70a0c189033cdb8692e4b1",
"mode": "0644",
"owner": "root",
"size": 1038,
"src": "/root/.ansible/tmp/ansible-tmp-1495860905.35-183232559888238/source",
"state": "file",
"uid": 0
}
172.16.1.53 | SUCCESS => {
"changed": true,
"checksum": "52bda81d895de3c7c54886d342e5eec074df757e",
"dest": "/etc/chrony.conf",
"gid": 0,
"group": "root",
"md5sum": "aee9cc7faa70a0c189033cdb8692e4b1",
"mode": "0644",
"owner": "root",
"size": 1038,
"src": "/root/.ansible/tmp/ansible-tmp-1495860905.34-134007063835838/source",
"state": "file",
"uid": 0
}
172.16.1.51 | SUCCESS => {
"changed": true,
"checksum": "52bda81d895de3c7c54886d342e5eec074df757e",
"dest": "/etc/chrony.conf",
"gid": 0,
"group": "root",
"md5sum": "aee9cc7faa70a0c189033cdb8692e4b1",
"mode": "0644",
"owner": "root",
"size": 1038,
"src": "/root/.ansible/tmp/ansible-tmp-1495860905.43-104570916452677/source",
"state": "file",
"uid": 0
}
172.16.1.52 | SUCCESS => {
"changed": true,
"checksum": "52bda81d895de3c7c54886d342e5eec074df757e",
"dest": "/etc/chrony.conf",
"gid": 0,
"group": "root",
"md5sum": "aee9cc7faa70a0c189033cdb8692e4b1",
"mode": "0644",
"owner": "root",
"size": 1038,
"src": "/root/.ansible/tmp/ansible-tmp-1495860905.43-40575778655199/source",
"state": "file",
"uid": 0
}

3.启动同步服务,防火墙也需要关闭

[root@zabbix ~]# ansible client -m shell -a "systemctl start chronyd.service"
172.16.1.53 | SUCCESS | rc=0 >> 172.16.1.250 | SUCCESS | rc=0 >> 172.16.1.52 | SUCCESS | rc=0 >> 172.16.1.51 | SUCCESS | rc=0 >>

4.注意客户端时间同步定时任务关闭

[root@zabbix ~]# ansible client -m shell -a "crontab -l"
172.16.1.51 | SUCCESS | rc=0 >> 172.16.1.250 | SUCCESS | rc=0 >> 172.16.1.53 | SUCCESS | rc=0 >> 172.16.1.52 | SUCCESS | rc=0 >>

5.Centos7依然可以用ntpdate命令同步时间

[root@zabbix ~]# ansible client -m shell -a "ntpdate 10.0.0.120"
172.16.1.53 | SUCCESS | rc=0 >>
27 May 13:05:57 ntpdate[26817]: adjust time server 10.0.0.120 offset -0.001686 sec 172.16.1.250 | SUCCESS | rc=0 >>
27 May 13:05:57 ntpdate[17419]: adjust time server 10.0.0.120 offset -0.004419 sec 172.16.1.52 | SUCCESS | rc=0 >>
27 May 13:05:57 ntpdate[50111]: adjust time server 10.0.0.120 offset -0.004410 sec 172.16.1.51 | SUCCESS | rc=0 >>
27 May 13:05:57 ntpdate[114089]: adjust time server 10.0.0.120 offset -0.000597 sec

6.查看时间,现在已经都同步了,一秒不差

[root@zabbix ~]# ansible client -m shell -a "date"
172.16.1.250 | SUCCESS | rc=0 >>
Sat May 27 13:06:04 CST 2017 172.16.1.51 | SUCCESS | rc=0 >>
Sat May 27 13:06:04 CST 2017 172.16.1.53 | SUCCESS | rc=0 >>
Sat May 27 13:06:04 CST 2017 172.16.1.52 | SUCCESS | rc=0 >>
Sat May 27 13:06:04 CST 2017

方法二:放入定时任务

[root@zabbix ~]# ansible client -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate 10.0.0.120 &>/dev/null'"
172.16.1.51 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"time sync"
]
}
172.16.1.52 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"time sync"
]
}
172.16.1.53 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"time sync"
]
}
172.16.1.250 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"time sync"
]
} [root@zabbix ~]# ansible client -m shell -a "crontab -l"
172.16.1.51 | SUCCESS | rc=0 >>
#Ansible: time sync
*/5 * * * * /usr/sbin/ntpdate 10.0.0.120 &>/dev/null 172.16.1.52 | SUCCESS | rc=0 >>
#Ansible: time sync
*/5 * * * * /usr/sbin/ntpdate 10.0.0.120 &>/dev/null 172.16.1.53 | SUCCESS | rc=0 >>
#Ansible: time sync
*/5 * * * * /usr/sbin/ntpdate 10.0.0.120 &>/dev/null 172.16.1.250 | SUCCESS | rc=0 >>
#Ansible: time sync
*/5 * * * * /usr/sbin/ntpdate 10.0.0.120 &>/dev/null

CentOS7搭建时间服务器-chrony(不坑)的更多相关文章

  1. CentOS7搭建时间服务器-chrony

      系统:centos7防火墙:关闭防火墙和selinux软件:chrony centos6我们一直用的ntp时间服务器,虽然到CentOS7上也可以装ntp.但是各种问题.所以建议centos7使用 ...

  2. CentOS 7.X 搭建时间服务器 --- chrony

    之前centos6我们一直用的ntp时间服务器,虽然到CentOS7上也可以装ntp.但是各种坑啊.这次换一个时间同步工具---->chrony ======================== ...

  3. centos7搭建时间服务器

    时区概念 GMT.UTC.CST.DST UTC:整个地球分为二十四个时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时间(UTC:Univers ...

  4. CentOS7 搭建 SVN 服务器

    CentOS7 搭建 SVN 服务器 介绍SVN: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上 ...

  5. centos7 搭建WEB服务器

    centos7 搭建WEB服务器 2017年09月17日 09:44:50 逝然1994 阅读数:18321 标签: centosapacheweb服务器 更多 个人分类: centos服务器简单配置 ...

  6. Centos7 Ntp 时间服务器

    Centos7 Ntp 时间服务器 安装环境 [root@m02 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 安装 ...

  7. centos7搭建svn服务器及客户端设置

    centos7搭建svn服务器及客户端设置 centos7貌似预装了svn服务(有待确认),因此我们直接启动该服务即可 一.svn服务端配置(服务器IP假设为192.168.100.1) 步骤1:创建 ...

  8. CentOS7搭建NTP服务器及客户端同步时间

    一.服务器配置 1.查看服务器.客户端操作系统版本 [root@hadoop101 ~]# cat /etc/redhat-release CentOS Linux release (Core) 2. ...

  9. centos7 搭建DHCP服务器

    一.DHCP简单讲解 DHCP就是动态主机配置协议(Dynamic Host Configuration Protocol)是一种基于UDP协议且仅限用于局域网的网络协议,它的目的就是为了减轻TCP/ ...

随机推荐

  1. js 抓取屏幕宽度设置字体大小 rem

    var doc = window.document var docEl = doc.documentElement var tid function refreshRem () { var width ...

  2. html的布局demo

    header section footer 都是水平,垂直居中,文本内容居中 section的高度是根据文本内容自适应的,footer会一直在最下面 <!DOCTYPE html> < ...

  3. Inception服务的安装以及使用Python 3 实现MySQL的审计

    Inception服务的安装以及使用Python实现MySQL的审计 Bison是Inception服务所依赖的包之一,但是某些Linux版本已安装的Bison,或者是通过yum安装的Bison,通常 ...

  4. Machine Learning - week 4 - 编程练习

    = X' * (h - y) LrCostFunction 与上一周的有什么不同? 与 week3 的 costFunctionReg 是一样的.Week3 中参考答案没有排除 theta 第一行,但 ...

  5. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…【字符串+模拟】

    P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He… 题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都 ...

  6. hdu_2030

    一个小小知识点,统计字符串中汉字出现的次数,直接给出代码 //ASCII码的范围是0-127所以,超出范围的都是汉字,因为一个汉字占两个字符位置,所以结果除以2就可以了 #include<cst ...

  7. (a == 1 && a == 2 && a == 3),何时为true?

    今天浏览一些技术网站,看到这个题目.虽然觉着代码这么写的可能性低之又低,但是却也考验对js了解的程度. 在 JavaScript 中 (a ==1 && a== 2 && ...

  8. flume1.8 Sinks类型介绍(三)

    1. Flume Sinks 1.1 HDFS Sink 该sink把events写进Hadoop分布式文件系统(HDFS).它目前支持创建文本和序列文件.它支持在两种文件类型压缩.文件可以基于数据的 ...

  9. day1 基础

    1.python 简介 一.python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的 ...

  10. .27-浅析webpack源码之事件流make(2)

    上一节跑到了NormalModuleFactory模块,调用了原型方法create后,依次触发了before-rsolve.factory.resolver事件流,这节从resolver事件流开始讲. ...