NTP服务

NTP时间服务器

作用:NTP主要是用于对计算机的时间同步管理操作。

时间是对服务器来说十分重要的,一般很多网站都需要读取服务器时间来记录相关信息,如果时间不准,则可能造成很大的影响。

部署安装NTP服务器

  • 安装服务
[root@localhost ~]# yum install ntp -y
  • 配置NTP文件
[root@localhost ~]# vim /etc/ntp.conf       #把文件中的内容全部删除,填入以下内容
server 127.127.1.0                          #本地时钟地址,以本机作为时间服务器,也可以根据需要选择阿里时间服务器
server timel.aliyun.com                     #网络时钟地址
restrict 127.0.0.1                          #允许本机使用时间服务器
restrict 192.168.127.0 mask 255.255.255.0 #允许192.168.127.0这网段的所有机器使用本机的时间服务器
  • 重启NTP服务
[root@localhost ~]# systemctl restart ntpd 
  • 检查NTP状态
[root@localhost ~]# ntpstat
synchronised to local net at stratum 6 
   time correct to within 3949 ms
   polling server every 64   
  • 客户端下载NTP客户端服务
[root@localhost ~]# yum install ntpdate -y
  • 客户端进行同步
[root@localhost ~]# date           #当前服务端时间
2019年 07月 27日 星期六 11:35:10 CST
[root@localhost ~]# date           #当前客户端时间
2019年 07月 27日 星期六 11:35:43 CST
[root@localhost ~]# ntpdate 192.168.127.140    #客户端进行时间同步
27 Jul 11:42:08 ntpdate[38818]: step time server 192.168.127.140 offset -3.145190 sec
  • 查看时间进行验证
[root@localhost ~]# date -s "2019-5-26 5:21:0"    #修改客户端时间
2019年 05月 26日 星期日 05:21:00 CST
[root@localhost ~]# ntpdate 192.168.127.140       #进行时间同步
27 Jul 11:46:46 ntpdate[38862]: step time server 192.168.127.140 offset 5379923.666986 sec
[root@localhost ~]# date                          #查看时间,发现和服务端时间一致
2019年 07月 27日 星期六 11:46:54 CST

 

时间服务器自动执行解决方案

配合计划任务进行时间同步

  • 查看crond是否已经启动
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# ps aux | grep "crond"
root      38937  0.0  0.1 126220  1580 ?        Ss   11:53   0:00 /usr/sbin/crond -n
root      38939  0.0  0.0 112664   956 pts/0    R+   11:53   0:00 grep --color=auto crond
  • 获取ntpdate路径
[root@localhost ~]# which ntpdate
/usr/sbin/ntpdate
  • 编辑计划任务
[root@localhost ~]# crontab -e      #写入命令的绝对路径
*/5 * * * * /usr/sbin/ntpdate 192.168.127.140    #每五分钟执行一次
  • 查看计划任务
[root@localhost ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate 192.168.127.140
  • 查看计划任务执行结果
[root@localhost ~]# date -s "2019-4-4 1:2:2"
2019年 04月 04日 星期四 01:02:02 CST
[root@localhost ~]# date
2019年 04月 04日 星期四 01:03:03 CST
[root@localhost ~]# date
2019年 07月 27日 星期六 14:00:18 CST

 

  

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2019-07-26 15:26  crucis  阅读(470)  评论(0编辑  收藏  举报