RHCE(十六)计算机时间同步——NTP

时钟

硬件时钟: 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因

系统时钟: 就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值

#查看时钟
date 
#查看所在时区
[root@web ~]# date -R
#查看所有时区
[root@web ~]# ls /usr/share/zoneinfo/

#查看其他时区的当前时间
[root@web ~]# zdump Hongkong
[root@web ~]# zdump ZuLu

更改时区

tzselect 命令更改时区步骤:

  1. 使用tzselect命令查询需要的时区
  2. 查看命令最后的提示,添加变量到~/.bash_profile文件中
  3. 将追加TZ=‘Africa/Nairobi’; export TZ
  4. 重新登陆生效

timedatectl 命令更改时区

#查看当前时区信息
Timedatectl
#列出所有时区
timedatectl list-timezones 
#更改时区
[root@haha ~]# timedatectl set-timezone Europe/Lisbon
[root@haha ~]# date -R    以RFC22格式输出日期和时间
Tue, 05 Apr 2016 05:46:55 +0100
#修改日期
timedatectl set-time 2016-04-25
#修改时间
timedatectl set-time '2016-04-26 21:53:50'
#设置系统时区为上海
timedatectl set-timezone Asia/Shanghai 

NTP

NTP(Network Time Protocol)网络时间协议,是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。

NTP服务器

安装软件
yum install -y ntp
修改配置文件
vim /etc/ntp.conf
在这里插入图片描述
配置文件详解

#系统时间和硬件时间的偏差记录
driftfile /var/lib/ntp/drift

#允许所有的访问
restrict default nomodify notrap nopeer noquery
restrict [192.168.221.0] mask [255.255.255.0] [parameter]

restrict 控制相关权限
语法为: restrict IP地址 mask 子网掩码 参数
其中IP地址也可以是default ,default 就是指所有的IP其中参数主要有:

ignore : 拒绝所有类型的ntp连接
nomodify : 客户端不能使用ntpc与ntpq两支程式来修改服务器的时间参数
noquery : 客户端不能使用ntpq、ntpc等指令来查询服务器时间,等于不提供ntp的网络校时
notrap : 不提供trap这个远程时间登录的功能
notrust : 拒绝没有认证的客户端
nopeer : 不与其他同一层的ntp服务器进行时间同步
restrict 127.0.0.1
restrict ::1

#上层时间服务器地址(自定义添加时,把这些注释掉)
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

Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

下面是自定义本机为ntp服务端来测试

server 127.127.1.0             本机地址用来测试网络连通性的
fudge 127.127.1.0 stratum 8    不管怎么写都表示本机

观察时间同步状况:ntpq -p
ntpq -p 各个选项相关信息如下:
remote 远程主机的主机名或IP

*目前正在使用的上层NTP
+已连线,可提供时间更新的候补服务器
-远程服务器被clustering algorithm认为是不合格的NTP Server  
x 远程服务器不可用

refid 上级NTP的时间基准服务器
st 就是stratum 上层NTP的层级,层级0-15
when 几秒钟前曾做过时间同步更新
poll 下一次更新在几秒后,逐步增大
reach 八进制数,已经向上层服务器要求更新的次数
delay 网络传输过程中的延迟时间
offset 本地和服务器之间的时间差别,越接近0,说明和服务器的时间越接近
jitter linux 系统时间与bios硬件时钟之间的差异

修改配置文件后重启服务生效

启动服务
systemctl restart ntpd
查看状态
systemctl status ntpd

ntpdate

#查看上层服务器状态
ntpdate -q 192.168.40.131
#更新时间
ntpdate 192.168.75.129

停止防火墙
服务端使用date修改时间
客户端:watch ntpdate 192.168.75.129(服务端地址) 每两秒会发生一次变化

ntpd与ntpdate的区别

ntpd与ntpdate在更新时间时有什么区别?

ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。时钟的跃变,对于某些程序会导致很严重的问题。

原创文章 102 获赞 105 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43624033/article/details/105775163