Ubuntu离线安装ntp时间同步服务
准备阶段:下载安装包
apt-get download ntp apt-get download ntpdate
服务器端配置
1、环境准备
- 先判断是否已安装 systemd-timesyncd
systemctl is-active systemd-timesyncd
- 如果返回结果是 active,则表示 systemd-timesyncd 已经在运行。在这种情况下,你可以使用以下命令停止并卸载它:
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd
sudo apt-get remove systemd-timesyncd
- 如果返回inactive,则表示 systemd-timesyncd没有在运行,可直接安装ntp
2、安装
sudo dpkg -i ntp包
sudo dpkg -i ntpdate包
3、编辑配置文件/etc/ntp.conf
# /etc/ntp.conf, NTP服务配置文件 # 指定 drift 文件的位置,它用于存储系统时钟的漂移信息 driftfile /var/lib/ntp/ntp.drift # 指定闰秒的定义文件 leapfile /usr/share/zoneinfo/leap-seconds.list # 启用统计数据记录,以便进行性能监控 statistics loopstats peerstats clockstats # 定义日志文件的生成,可以在文件中存储不同类型的统计信息 filegen loopstats file loopstats type day enable filegen peerstats file peerstats type day enable filegen clockstats file clockstats type day enable # 配置本地系统的时钟作为 NTP 时间服务器 # 127.127.1.0 表示本地时钟 每16秒向本地时钟服务器发出一次查询 server 127.127.1.0 minpoll 4 maxpoll 4 # 设置本地时钟的层级,stratum 值表示时间源的等级 # stratum 0 是最高等级,通常不应该更改 fudge 127.127.1.0 stratum 5 # 使用 Ubuntu 提供的 NTP 服务器作为备用时间源 #pool ntp.ubuntu.com # 限制外部访问,不允许未授权的操作 restrict -4 default kod notrap nomodify nopeer noquery limited restrict -6 default kod notrap nomodify nopeer noquery limited # 允许本地主机 127.0.0.1 访问 NTP 服务 restrict 127.0.0.1 # 允许 IPv6 本地主机 ::1 访问 NTP 服务 restrict ::1 # 不允许外部主机修改或请求服务器的状态信息 restrict source notrap nomodify noquery
4、重启
service ntp restart
客户端配置
1、环境准备
- 先判断是否已安装 systemd-timesyncd
systemctl is-active systemd-timesyncd
- 如果返回结果是 active,则表示 systemd-timesyncd 已经在运行。在这种情况下,你可以使用以下命令停止并卸载它:
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd
sudo apt-get remove systemd-timesyncd
- 如果返回inactive,则表示 systemd-timesyncd没有在运行,可直接安装ntp
2、安装
sudo dpkg -i ntp包
sudo dpkg -i ntpdate包
3、编辑配置文件/etc/ntp.conf
# 指定 drift 文件的位置,用于存储系统时钟漂移信息 driftfile /var/lib/ntp/drift # 针对默认配置,限制 NTP 服务的访问权限,阻止外部修改服务器状态 restrict default kod limited nomodify notrap nopeer noquery # 允许本地主机 127.0.0.1 访问 NTP 服务 restrict 127.0.0.1 # 允许 IPv6 本地主机 ::1 访问 NTP 服务 restrict ::1 # 限制特定 IP 地址 192.166.6.47 访问,但不允许修改或请求服务器状态 restrict 192.166.6.47 nomodify notrap noquery # 配置 NTP 服务器的地址 server 192.166.6.47 # 包含文件,通常用于加密配置信息 includefile /etc/ntp/crypto/pw # 指定 NTP 密钥文件的位置 keys /etc/ntp/keys # 禁用监视器功能 disable monitor
4、重启
service ntp restart
5、手动更新系统的时钟
ntpdate -d 192.166.6.xx[时间服务器ip]
本文作者:jikefan
本文链接:https://www.cnblogs.com/jikefan/articles/18573881
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。