关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

Linux 之 NTP 服务 服务器

发布时间:2020-04-19 10:58:28

我们选择第三台机器( mysql02 )为 NTF 服务器,其他机器和这台机器进行同步。

1、 检查 ntp 服务是否已经安装
[root@mysql02 ~]# sudo rpm -qa | grep ntp
ntpdate-4.2.6p5-5.el6.centos.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
ntp-4.2.6p5-5.el6.centos.x86_64
[root@mysql02 ~]#

说明:显示已经安装过了 ntp 程序,其中 ntpdate-4.2.6p5-1.el6.centos.x86_64 是用来和某台服务器进行同步
的,ntp-4.2.6p5-1.el6.centos.x86_64 是用来提供时间同步服务的。

2、 修改配置文件 ntp.conf
[root@mysql02 ~]# vim /etc/ntp.conf
2.1 启用 restrice,修改网段
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
将这行的注释去掉,并且将网段改为集群的网段,我们这里是 1 网段。

2.2 注释掉 server 域名配置
#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

说明:上面是时间服务器的域名,这里不需要连接互联网,所以将他们注释掉。

2.3 在 server 0.centos.pool.ntp.org iburst 前,添加如下
server 127.127.1.0 # local clock 
fudge 127.127.1.0 stratum 10

补充:
① 配置方法一:只允许192.168.100.0网段的客户机进行时间同步
在restrict default kod nomodify notrap nopeer noquery(表示默认拒绝所有IP的时间同步)之后增加一行
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

② 配置方法二:允许任何ip的客户机都可以进行时间同步
将restrict default kod nomodify notrap nopeer noquery修改为如下行:
Restrict default nomodify
权限的设定主要以restrict这个参数来设定,主要的语法为:
restrict IP地址 mask 子网掩码 参数
其中IP可以是IP地址也可以是default,default是指所有的IP

参数主要有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询

3、 修改配置文件 ntpd
[root@mysql02 ~]# sudo vim /etc/sysconfig/ntpd
添加一行配置:SYNC_CLOCK=yes

4、 启动 ntp 服务
[root@mysql02 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@mysql02 ~]# sudo chkconfig ntpd on
这样每次机器启动时,ntp 服务都会自动启动。

5、配置其他机器的同步 
-- 需要被同步的机器
切换到 root 用户进行配置通过 contab 进行定时同步:
[root@bigdata-senior02 hadoop]# crontab -e
*/10 * * * * /usr/sbin/ntpdate 10.219.24.25


6、测试同步是否有效
6.1、 查看目前三台机器的时间
[root@mysql02 ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-14 05:57:13
[root@mysql01 ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-14 05:57:17

6.2、 修改 mysql02 上的时间
将时间改为一个以前的时间
[root@mysql02 ~]# sudo date -s '2017-08-13 22:01:00'
Sun Aug 13 22:01:00 CST 2017
[root@mysql02 ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-13 22:01:08

等 10 分钟,看是否可以实现自动同步

6.3、 查看是否自动同步时间
[root@mysql02 ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-13 23:01:45
[root@mysql01 ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-13 23:01:46

说明:mysql01已经自动跟mysql02同步了。



/template/Home/Zkeys2/PC/Static