Linux ❀ NTP-网络时间协议

NTP-网络时间协议(Network Time Protocol)

我们是如何定义时间和时区的:
在地球环绕太阳旋转的24个小时中,世界各地日出日落的时间是不一样的.所以我们才有划分时区(timezone))的必要,也就是把全球划分成24个不同的时区. 所以我们可以把时间的定义理解为一个时间的值加上所在地的时区(注意这个所在地可以精确到城市)

在Linux中查看时区:

[root@csa ~]# date -R

查看所有的时区:

[root@csa ~]# ls /usr/share/zoneinfo/

查看其他时区的当前时间:

[root@csa ~]# zdump HongKong

如何更改时区

一、使用tzselect命令查看:
1、使用tzselect命令查询需要的时区
2、查看命令最后的提示,添加变量到~/.bash_profile文件中
3、重新登陆生效

二、使用timedatectl 命令查看:
#查看当前时区信息

[root@csa ~]# timedatectl

#列出所有的时区

[root@csa ~]# timedatectl list-timezones 

#修改日期

[root@csa ~]# timedatectl set-time 2016-04-25

#修改时间

[root@csa ~]# timedatectl set-time '2016-04-26 21:53:50'

#设置系统时区为上海

[root@csa ~]# timedatectl set-timezone Asia/Shanghai 

#设置之间自动同步,如果需要手动更改时间,需更改此选项

[root@csa ~]# timedatectl set-ntp yes

安装软件
注意和ntpd的区别
用yum安装ntp服务:

[root@csa ~]# yum install -y ntp

ntp的配置文件:

[root@csa ~]# vim /etc/ntp.conf

#系统时间和硬件时间的偏差记录

[root@csa ~]# driftfile /var/lib/ntp/drift

#允许所有的访问

[root@csa ~]# restrict default nomodify notrap nopeer noquery 
		nomodify-客户端不能修改服务端的时间,但是可以通过服务端校时
		notrap-不提供trap远程登陆功能,该功能提供远程记录日志服务
		nopeer-阻止客户端和服务器端对等
		noquery-不提供客户端时间查询

restrict 127.0.0.1
restrict ::1

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

#上层时间服务器地址
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

使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0
fudge 127.127.1.0 stratum 8

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

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

重启NTP服务

[root@csa ~]# systemctl restart ntpd

查看NTP服务的状态

[root@csa ~]# systemctl status ntpd

客户端使用:
#查看上层服务器状态

[root@csa ~]# ntpdate -q 192.168.40.131

#更新时间

[root@csa ~]# ntpdate 192.168.75.129

#使用计划任务自动更新时间
#编辑/etc/crontab文件

[root@csa ~]# root /usr/sbin/ntpdate 192.168.40.131 > /dev/null 2>&1

创作者:Eric· Charles

你可能感兴趣的:(Linux)