Linux系统时间同步
一、第一步:搭建用于时间同步的服务器
1.1 先安装npt模块:`sudo yum -y install ntp`1.2 写配置文件`/etc/npt.conf`1.3 启动ntp服务 二、第二步:在需要同步时间服务器上进行时间同步
2.1 执行同步命令2.2 执行命令的时候遇到的一个问题2.3 将时间同步部署成定时任务
一、第一步:搭建用于时间同步的服务器在一台时间比较准确的服务器上,启动ntpd服务,比如这台时间比较精确的服务ip为11.225.118.166。
1.1 先安装npt模块:sudo yum -y install ntp 1.2 写配置文件/etc/npt.confsudo vi /etc/npt.conf
driftfile /var/lib/ntp/drift restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 # 10.114.57.0 代表允许时间同步的服务器ip restrict 10.114.57.0 mask 255.255.255.0 nomodify notrap server 127.127.1.0 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor tinker panic 01.3 启动ntp服务
-- 查看状态 sudo systemctl status ntpd -- 启动nptd sudo systemctl start ntpd二、第二步:在需要同步时间服务器上进行时间同步
该需要时间同步的服务ip应该以10.114.57开头。
2.1 执行同步命令sudo ntpdate 11.225.118.166 -- 或者 sudo /usr/sbin/ntpdate 11.225.118.1662.2 执行命令的时候遇到的一个问题
遇到的一个问题描述如下:
20 Jan 10:16:31 ntpdate[95320]: the NTP socket is in use,exiting
解决方案:
sudo lsof -i:123 sudo kill -9 进程id 然后继续执行同步命令2.3 将时间同步部署成定时任务
crontab -e
# 同步时间,每隔十五分钟同步一次 */15 * * * * bash /root/importShells/run_sync_time.sh
/root/importShells/run_sync_time.sh 内容如下:
#!/bin/bash
current_month=$(date "+%Y%m")
current_day=$(date "+%Y%m%d")
logfilename=sync_time_${current_day}.log
mkdir -p /root/importShells/logs/${current_month}
log_file_path=/root/importShells/logs/${current_month}/${logfilename}
bash /root/importShells/time_sync.sh 1>>${log_file_path} 2>>${log_file_path}
/root/importShells/time_sync.sh的内容如下:
#!/bin/bash /usr/sbin/ntpdate 11.225.118.166



