Linux时钟同步NTP配置
一、基本操作
hwclock --show #硬件时间时钟(RTC)
date #还有一个称之为系统时钟(System Clock)
hwclock --hctosys #把硬件时间设置成系统时间 hwclock -s
hwclock --systohc #把系统时间设置成硬件时间 hwclock -w
hwclock --set --date="mm/dd/yy hh:mm:ss" #设置硬件时间
date -s "dd/mm/yyyy hh:mm:ss" #设置系统时间
ntpstat #运行命令 ntpstat 查看每次更新间隔
vi /etc/sysconfig/ntpd #同步硬件时钟,NTP一般只会同步system clock
SYNC_HWCLOCK=yes
timedatectl set-timezone Asia/Shanghai #设置时区
二、NTP服务器搭建
1、yum install -y ntp ntpdate #安装服务
2、vi /etc/ntp.conf #修改配置文件(除以下外其余默认即可)
server 192.168.1.70 prefer # ntp同步主用服务器
server 192.168.1.39 # ntp同步备用服务器
server 127.127.1.0 # 将本地服务器作为ntp服务器
fudge 127.127.1.0 stratum 10 #设置本地服务器作为ntp服务器的优先级(0-15,越小优先级越高)
3、/etc/init.d/ntpd start #(systemctl start ntpd)当前启动ntpd服务
chkconfig ntpd on #(systemctl enable ntpd)下次开机自启ntpd服务
4、ntpq -p #检查是否成功
三、Linux服务器配置
1、环境准备
rpm –qa |grep ntp #检查ntp和ntpdate软件包是否安装
例如:
rpm -qa|grep ntp
ntpdate-4.2.6p5-22.el7.x86_64
ntp-4.2.6p5-22.el7.x86_64
rpm –ivh ntp* # 如果未安装,需安装对应操作系统版本的软件包
2、定时同步时间
service ntpd stop #关闭ntpd服务,ntpd是实时同步服务
chkconfig ntpd off
定时同步时间
root用户下:
crontab –e
0 6 * * * /usr/sbin/ntpdate X.X.X.X >> /ntpdate.log
3、配置自动同步,实时同步
vim /etc/ntp.conf #修改配置文件
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server x.x.x.x prefer
server x.x.x.x
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
service nptd start #启动ntpd服务,设置开机自动启动ntpd服务
chkconfig ntpd on
ntpq –p #检查ntpd状态
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)