ntp服务

ntp与ntpdate

①两个服务都是centos自带的(centos7中不自带ntp)。ntp的安装包名是ntp;ntpdate的安装包是ntpdate。他们并非由一个安装包提供

②ntp守护进程为ntpd,配置文件是/etc/ntp.conf

③ntpdate用于客户端的时间矫正,非NTP服务器可以不启动NTP

ntp配置详解:

driftfile /var/lib/ntp/drift  #默认即可。driftfile用来指定记录本机与上层NTP server之间的频率误差。单位是百万分之一秒。
restrict default nomodify notrap nopeer noquery 
#restrict用来管理权限控制。格式为 restrict [单个ip|网络|default] parameter
#      parameter:
#        ignore:拒绝所有的ntp连接
#        nomodify:客户端不能使用ntpc和ntpq这两个程序来更改服务器的时间参数,但客户端可以通过此主机来进行网络校时。
#        noquery:客户端不能使用ntpc和ntpq等命令来查询时间服务器,等于不提供网络校时服务。
#        notrap:不提供trap这个网络时间登陆的功能
#        notrust:拒绝没有认证的客户端
restrict 127.0.0.1  #以下两条默认,放行本机来源
restrict ::1
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
#server:用来设置上层NTP服务器,说白了就是client向谁请求NTP时间同步。
#server 127.127.1.0 prefer #以本机时间作为时间服务。内网中这个配置一定要加上,否则会导致NTP服务不可用,prefer代表这台主机优先级最高。
keys /etc/ntp/keys ##除了restrict来限制客户端连接外,还可以通过秘钥方式来给客户端认证

配置文件模板

ftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
#允许内网其他机器同步时间
restrict 172.16.1.0 mask 255.255.255.0 nomodify notr         <--内网网段
#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
#定义使用的上游 ntp服务器,将原来的注释
server ntp1.aliyun.com
server time1.aliyun.com
#允许上层时间服务器主动修改本机时间
restrict time1.aliyun.com nomodify notrap noquery
restrict ntp1.aliyun.com nomodify notrap noquery
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
#stratum根据上层server的层次而设定(+1)。对于提供network time service provider的主机来说,stratum的设定要尽可能准确。而作为局域网的time service provider,通常将stratum设置为10
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

启动ntp

systemctl start ntpd
netstat -lntup|grep ntp #ntp默认监听udp 123端口

测试

image-20210309173001243

ntpstat  #查看客户端是否与server连接成功(客户端必须启动ntp服务)
ntpq -p  #列出当前主机的ntp和上层ntp的状态
remote:上层NTP服务器的IP或者主机名。主要最左边的*
  *:代表目前正在使用中的上层FTP
  +:已经连接成功,且可以作为下一个提供时间服务的候选人。

refid:它指的是给远程服务器(156.0.26.6)提供时间同步的服务器,本机上级的上级NTP服务器。
  为什么显示为LOCAL(0)?
   由于此处为内网环境,无法去外网公用的主机同步时间,因此我们在156.0.26.6上设置的是156.0.26.6自身作为时间服务器同时,使用server 127.127.1.0 prefer指名他的上级NTP为自身。
 st:就是stratum层级。 类似于DNS,NTP是层级结构,有顶端的服务器,最多有15层。为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。
when:几秒之前同步过时间
poll:在过多长时间去同步时间。
reach:已经同步时间的次数
delay:网络传输过程中的延迟,单位是10的-6次方秒。
offset:时间修正值,这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别.。单位是10的-3次方。
jitter:linux系统时间(软件时间)与BIOS硬件时间的差异时间,单位是10的-6次方秒。在主机和NTP服务器同步时间欧,可以使用 hwclock -w将系统时间写入BIOS. 
查看硬件BIOS时间:
hwclock -r    读出BIOS的时间参数
hwclock -w    将当前系统时间写入BIOS中
/etc/sysconfig/clock        #这个是linux 的主要时区设定文件。每次开机后,Linux会自动的读取这个文件来设定自己系统所默认要显示的时间
posted @ 2021-04-14 16:44  海军同学  阅读(616)  评论(0编辑  收藏  举报