之前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

CentOS 7.X 搭建时间服务器 --- chrony的更多相关文章

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

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

  2. CentOS7搭建时间服务器-chrony(不坑)

    标签(linux): chrony 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 之前centos6我们一直用的ntp时间服务器,虽然到CentOS7上也可以装 ...

  3. CentOS 6.4 搭建git 服务器

    CentOS 6.4 搭建git 服务器 (2013-11-22 19:04:09)转载▼ 标签: it 分类: Linux 此文件是依据markdown所编写,更好效果参见本人github的文档ht ...

  4. Linux Centos 6.6搭建SFTP服务器

    Linux Centos 6.6搭建SFTP服务器 在Centos 6.6环境使用系统自带的internal-sftp搭建SFTP服务器. 打开命令终端窗口,按以下步骤操作. 0.查看openssh的 ...

  5. Centos 6.4搭建git服务器【转】

    前阵子公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅.本篇本章只涉及搭建部分的操作,更多git的使用可以参考文 ...

  6. CentOS Linux上搭建PPPoE服务器及拨号设置

    CentOS Linux上搭建PPPoE服务器及拨号设置 搭建PPPoE,成功了的话,就觉得超级简单,在CentOS Linux更是5步左右就能搞定. 1.安装pppoe,安装完成后,会有pppoe- ...

  7. 搭建时间服务器(linux)

    我们搭建集群环境的时候,时间必须是要统一的,才能保证集群数据的一致性. 一般操作是直接使用NTP,跟默认的时间服务器同步,但是最好还是让所有节点跟集群中的某台作为时间服务器的节点同步. 步骤:(节点有 ...

  8. CentOS利用postfix搭建邮件服务器

    之前我用nodemailer通过163邮箱来发送邮件,不过没过几天就一直ETIMEDOUT,不知道什么原因,想着还是自己搭一个来发邮件可能靠谱点(flag?) 安装postfix CentOS 7 自 ...

  9. 在阿里云主机上基于CentOS用vsftpd搭建FTP服务器

    最近需要在一台阿里云的云服务器上搭建FTP服务器,在这篇博文中分享一下我们根据实际需求进行的一些配置. ftp软件用的是vsftpd. vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

随机推荐

  1. PHP:将json数据放进html标签中的详细讲解

    1.在controller中调用某方法,得到最终的json数据.根据框架开发形式,将Json数据传入视图中 2.在视图中,我们放置一个hidden形式的标签,并获取到json数据 3.这时候,我们查看 ...

  2. react及flux架构范例Todomvc分析

    react及flux架构范例Todomvc分析 通过分析flux-todomvc源码,学习如何通过react构建web程序,了解编写react应用程序的一般步骤,同时掌握Flux的单向数据流动架构思想 ...

  3. Open XML的上传、下载 、删除 ......文件路径

    /// <summary> /// Get download site, if download tempfolder not existed, create it first /// & ...

  4. Git学习环境搭建和git对用户的增删改查命令

    git安装 windows下安装git git下载进入网址: https://git-scm.com/downloads 点击下载,进行安装,安装成功,你会看到图标,点击进入到git bash 查看g ...

  5. 谷歌浏览器模拟手机浏览器且指定IP运行

    1.背景 因为现在项目是要做分布式,而以前使用谷歌浏览器模拟手机运行做的分布式,是指定在某台机器运行是通过Jenkins配置,来指定服务器,但是这样有一个问题,如果大家都同时配置到某台电脑,那台服务器 ...

  6. 2017.10.10 java中的继承与多态(重载与重写的区别)

    1. 类的继承 继承是面向对象编程技术的主要特征之一,也是实现软件复用的重要手段,使用继承特性子类(subclass) 可以继承父类(superclass)中private方法和属性,继承的目的是使程 ...

  7. Spring/Spring boot中静态变量赋值

    情形1:静态变量为自动注入的对象 解决方案:设置两个变量,非静态变量使用@resource注入Bean,然后使用@PostConstruct在Spring初始化Bean成功后为静态变量赋值 @Comp ...

  8. jQuery 二级联动

    jQuery 二级联动 ----请选择省份---- 北京 上海 江苏 ----请选择城市---- 东城 西城 崇文 宣武 朝阳  黄浦 卢湾 徐汇 长宁 静安 南京  镇江 苏州 南通 扬州 & ...

  9. C#静态成员和非静态成员

    一.C#静态成员和非静态成员 1. C#静态成员和非静态成员 当类中的某个成员使用static修饰符时,就会被声明为静态成员.类中的成员要么是静态成员,要么是非静态成员.一般情况下,静态成员属于整个类 ...

  10. java并发多线程(摘自网络)

    1. 进程和线程之间有什么不同? 一个进程是一个独立(self contained)的运行环境,它可以被看作一个程序或者一个应用.而线程是在进程中执行的一个任务.Java运行环境是一个包含了不同的类和 ...