NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。
1、查看核对操作系统时区
timedatectl #查看当前时间与时区
timedatectl set-timezone Asia/Shanghai #设置时区,如果时区不对需要设置
timedatectl set-time "YYYY-MM-DD HH:MM:SS" #手动设置时间
2、安装NTP服务
yum install ntp -y #安装NTP服务
systemctl start ntpd #启动NTP服务
systemctl enable ntpd #设置NTP开机自启
3、修改NTP配置文件
vim /etc/ntp.conf
restrict ntp1.aliyun.com nomodify notrap noquery #设置ntp1.aliyun.com可以修改本地系统时间
server ntp1.aliyun.com #设置阿里云NTP服务器
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#注释以上默认NTP服务器
4、查看同步结果
systemctl restart ntpd #重启NTP服务
ntpq -p #查看NTP服务同步状态,见下面输出代表成功
remote refid st t when poll reach delay offset jitter
==============================================================================
120.25.115.20 10.137.53.7 2 u 9 64 1 47.461 0.578 0.000
5、仅单次同步时间
ntpdate -u ntp1.aliyun.com #仅单次同步时间