Linux之NTP时间同步服务器搭建

本文介绍如何在CentOS系统上安装配置ntpd服务,包括安装ntpd、配置ntp.conf文件、设置开机启动、检查服务状态、写入BIOS时间、设置定时任务等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.安装ntpd服务

2.修改/etc/ntp.conf的配置

3.启动设置开机自启

4.检查是否开启成功

5.将当前日期写入BIOS

6.检查同步信息

7.设置定时任务


1.安装ntpd服务

   [root@giantfox ~]# yum install -y ntp ntpdate

   Centos7版本可能自带ntp 如果没有就手动下载一下

2.修改 /etc/ntp.conf 的配置

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

[root@giantfox ~]# cat /etc/ntp.conf

driftfile /var/lib/ntp/drift  #会把时间误差记录下来

logfile /var/log/ntpd.log   #日志记录

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  #控制允许那些网络查询和同步时间

#server 0.centos.pool.ntp.org iburst   //这四行server注释掉 增加本机的server

#server 1.centos.pool.ntp.org iburst

#server 2.centos.pool.ntp.org iburst

#server 3.centos.pool.ntp.org iburst

server 192.168.1.6 iburst

fudge 127.0.0.1 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

3.启动并开机自启

   [root@giantfox ~]# systemctl restart ntpd ; systemctl enable ntpd

4.检查ntpd是否开启成功

   [root@giantfox ~]# systemctl status ntpd

● ntpd.service - Network Time Service

   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)

   Active: active (running) since Sat 2022-11-05 02:46:32 UTC; 10min ago

  Process: 9401 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 9402 (ntpd)

   CGroup: /system.slice/ntpd.service

           └─9402 /usr/sbin/ntpd -u ntp:ntp -g

Nov 05 02:46:32 giantfox.novalocal systemd[1]: Starting Network Time Service...

Nov 05 02:46:32 giantfox.novalocal ntpd[9402]: proto: precision = 0.085 usec

Nov 05 02:46:32 giantfox.novalocal ntpd[9402]: 0.0.0.0 c01d 0d kern kernel time sync enabled

Nov 05 02:46:32 giantfox.novalocal systemd[1]: Started Network Time Service.

状态正常

查看UDP123端口是否启动成功

[root@giantfox ~]# netstat -ntplu |grep ntp

udp        0      0 192.168.1.6:123         0.0.0.0:*                           9402/ntpd           

udp        0      0 127.0.0.1:123           0.0.0.0:*                           9402/ntpd           

udp        0      0 0.0.0.0:123             0.0.0.0:*                           9402/ntpd           

udp6       0      0 fe80::f816:3eff:fe7:123 :::*                                9402/ntpd           

udp6       0      0 ::1:123                 :::*                                9402/ntpd           

udp6       0      0 :::123                  :::*                                9402/ntpd   

5.将当前时间和日期写入BIOS

 [root@giantfox ~]#  echo "SYNC_HWCLOCK=yes" >> /etc/sysconfig/ntpd

6.检查同步信息

[root@giantfox ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

 ns01.linux.com  .INIT.          16 u    -  512    0    0.000    0.000   0.000

7.设置定时任务进行时间校对

需安装ntpdate,每天24点更新同步时间
[root@giantfox ~]# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 192.168.1.6; hwclock -w

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值