1, linux 系统时钟有两个, 一个是硬件时钟, 即 BIOS 时间, 就是我们进行 CMOS 设置时看到的时间, 另一个是系统时钟, 是 linux 系统 Kernel 时间. 当 Linux 启动时, 系统 Kernel 会去读取硬件时钟的设置, 然后系统时钟就会独立于硬件运作. 有时我们会发现系统时钟和硬件时钟不一致, 因此需要执行时间同步.
2, 修改硬件时钟
查看硬件时钟命令:
hwclock --show
设置硬件时间:
hwclock --set --date="09/18/18 10:35:25" (月 / 日 / 年 时: 分: 秒)
3, 修改系统时间.
用 date 命令将日期设置为 2018 年 9 月 18 日
date -s 09/18/18 (月 / 日 / 年)
将时间设置为 10 点 38 分 15 秒
date -s 10:38:15
4, 同步系统时钟和硬件时钟
重启 Linux 系统后, 硬件时钟和系统时钟同步. 如果不大方便重新启动的话 (服务器通常很少重启), 使用 clock 或 hwclock 命令来同步系统时钟和硬件时钟.
硬件时钟与系统时钟同步:
hwclock --hctosys
或者
clock --hctosys
上面命令中,--hctosys 表示 Hardware Clock to SYStem clock.
系统时钟和硬件时钟同步:
hwclock --systohc
或者
clock --systohc
5, 安装 ntp 软件同步网络时间
yum -y install ntp
测试
命令格式
ntpdate 时间服务器域名或者 IP 地址
ntpdate -u 210.72.145.44
注意: 若不加上 - u 参数, 会出现以下提示: no server suitable for synchronization found
-u: 从 man ntpdate 中可以看出 - u 参数可以越过防火墙与主机同步;
210.72.145.44: 中国国家授时中心的官方服务器.
ntp 常用服务器:
中国国家授时中心: 210.72.145.44
NTP 服务器 (上海) :ntp.api.bz
美国: time.nist.gov
复旦: ntp.fudan.edu.cn
微软公司授时主机 (美国) :time.windows.com
台警大授时中心 (台湾):asia.pool.ntp.org
写入定时任务同步时间
10 */usr/sbin/ntpdate asia.pool.ntp.org>/dev/null &
重启定时任务服务
service crond restart
6, 最后验证时间是否与网络时间同步
date
来源: http://www.bubuko.com/infodetail-2772028.html