1 删除已经安装的

查询已安装版本:rpm -qa | grep ntp 
    删除已安装版本:rpm -e --nodeps 查询所得版本    
     注意有个  fontpackages-filesystem-1.44-8.el7.noarch  不要删
        如:rpm -e --nodeps ntpdate-4.2.6p5-28.el7.centos.x86_64

2 安装ntp

将以下rpm安装文件复制到服务器,文件下载推荐阿里的rpm仓库,http://mirrors.aliyun.com/,注意ntp与ntpdate版本要一致,然后在文件夹下依次执行下列安装命令
我已经放到百度云里了 :
链接: https://pan.baidu.com/s/1Vo1pjZ9HCDr4DuEZwWhr5A 提取码: m8h2

rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
rpm -ivh ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm
rpm -ivh ntp-4.2.6p5-28.el7.centos.x86_64.rpm

绝对能安装成功,不成功自己看看是不是下错版本了

3 修改配置

修改两个配置文件
第一个: vim /etc/ntp.conf配置文件:

案例展示的是:   104ip  与 105 ip  想拉取 207 ip 的时间,设置成自己的时间     207 是个ip 地址,为了保密我用 192.168.xxx.207  来代替

如下:

207 104 105 的 /etc/ntp.conf 配置文件末尾都加上以下两行,避免分层过高导致同步时出现 Server dropped: strata too high问题

server 127.127.1.0 fudge
    127.127.1.0 stratum 8

如下图:

centos libaio1离线安装 centos离线安装ntp_客户端

第二个: vim /etc/ntp/step-tickers 只有105 104 修改!!!!207 不用,

vim /etc/ntp/step-tickers

# List of NTP servers used by the ntpdate service.
# 增加的这个ip 就是104,105 节点对应 /etc/ntp.conf 中配置的207 ip
server 192.100.3.207 prefer
#0.centos.pool.ntp.org

下图表示:配置中 server 192.68.xxx.207 , 表示将来 105,104 将来要 去207同步时间

centos libaio1离线安装 centos离线安装ntp_linux_02

4 启动

启动:systemctl start ntpd
停止:systemctl stop ntpd
状态:systemctl status ntpd

验证: 使用命令netstat -tlunp |grep ntp查看123 端口是否被ntp使用
[root@fastdfs105 ~]# netstat -tlunp |grep ntp

此时104,105虽然启动了,但是并不代表,可以 拉取207 的时间,作为自己的时间 还需要 触发拉取时间的命令动作

5 拉取时间作为自己的时间

拉取时间作为自己的时间 ,在104 105 上执行命令,

/usr/sbin/ntpdate -u 192.100.3.207;
/sbin/hwclock -w;

之所以这样做,因为 相对于207, 105 104 是 ntp 客户端

也可以用域名 /usr/sbin/ntpdate -u ntp.api.bz; ntp.api.bz 为域名
一般设置成定时任务,每天同步下:
crontab -e
输入:
0 0 3 * * ? /usr/sbin/ntpdate -u 192.100.3.207; /sbin/hwclock -w

表示每天3点的时候会从ntp.api.bz(114.118.7.163)同步一次时间,
并写入到硬件时钟(/sbin/hwclock -w)。

6设置开机启动

vim /etc/rc.d/rc.local文件,
文末添加:
systemctl start ntpd
    
对/etc/rc.d/rc.local文件添加执行权限,
chmod +x /etc/rc.d/rc.local

含义restrict default nomodify notrap nopeer noquery

  1. restrict default ignore这行保留为注释状态,因为sales不需要做time server

参数有以下几个:

ignore :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置
更多ntp.conf 含义:

查看105 或104 与上一层 207 间的状态 使用命令 ntpq -p

[root@fastdfs105 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
*192.100.3.207 LOCAL(0) 6 u 20 128 377 0.278 2.100 0.626
LOCAL(0) .LOCL. 5 l 1486 64 0 0.000 0.000 0.000

remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间 // 如果没有和207 通信这里就是 -
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差
要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。