ntpd时间同步## 安装ntp包
yum install -y ntp
配置ntp服务
vi /etc/ntp.conf
修改ntpd
vi /etc/sysconfig/ntpd
# 添加如下内容
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
配置ntp自启动服务
chkconfig ntpd on
# 重启ntpd服务,应手动同步一次时间防止偏差过大导致系统无法同步时间
service ntpd stop
service ntpd start
查看ntp状态
ntpq -p
ntpdate时间同步
安装ntpdate包
yum install -y ntpdate
# 手动同步时间
ntpdate 10.10.18.250
设置自动更新时间
[root@ecology ~]# whereis ntpdate
ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz
[root@ecology ~]# crontab -e
# 设置每十分钟自动同步一次时间
*/10 * * * * /usr/sbin/ntpdate 10.10.18.250 >> /tmp/nptdate.log
手动时间同步date
Linux操作系统默认系统时间和硬件时间不会自动同步,在Linux运行过程中统时间和硬件时间以异步的方式运行,互不干扰。
硬件时间的运行靠Bios电池来维持,而系统时间是用CPU tick来维持的。
linux系统开机时,会从Bios中取得硬件时间,设置为系统时间。
linux系统时间设置
Linux可以用date命令设置系统时间:
- 查看系统时间:
[root@test ~]# date
2022年 02月 21日 星期一 16:49:40 CST
- 修改系统时间:
格式:年-月-日 时:分【:秒】
类型:string
如下命令效果一致。
#01
[root@test ~]# date -s '2022-02-21 12:00:00'
2022年 02月 21日 星期一 12:00:00 CST
#02
[root@test ~]# date --set '2022-02-21 12:00'
2022年 02月 21日 星期一 12:00:00 CST
- 另一种格式:
格式:月日时分年.秒
类型:数字
[root@test ~]# date 022112002022.10
2022年 02月 21日 星期一 12:00:10 CST
linux硬件时间设置
硬件时间的设置可使用hwclock或者clock命令。
hwclock和clock用法相近,只用一个就行。
clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
- 查看硬件时间
下列六条命令结果一样。
#01
[root@test ~]# hwclock --show
2022年02月21日 星期一 18时00分57秒 -1.025398 秒
#02
[root@test ~]# hwclock -r
2022年02月21日 星期一 18时01分47秒 -0.599059 秒
#03
[root@test ~]# hwclock
2022年02月21日 星期一 18时01分52秒 -0.958525 秒
#04
[root@test ~]# clock --show
2022年02月21日 星期一 18时02分26秒 -0.380324 秒
#05
[root@test ~]# clock -r
2022年02月21日 星期一 18时02分30秒 -0.958441 秒
#06
[root@test ~]# clock
2022年02月21日 星期一 18时02分32秒 -0.505293 秒
- 设置硬件时间
如下两条语句效果一致。
#01
[root@test ~]# hwclock --set --date='2022-02-21 12:00'
[root@test ~]# clock
2022年02月21日 星期一 12时00分06秒 -0.802214 秒
#02
[root@test ~]# clock --set --date='2022-02-21 12:00'
[root@test ~]# clock
2022年02月21日 星期一 12时00分03秒 -0.208406 秒
根据系统时间设置硬件时间
[root@test ~]# clock --set --date='2022-02-21 12:00'
[root@test ~]# date
2022年 02月 21日 星期一 12:04:13 CST
[root@test ~]# clock -w
[root@test ~]# clock
2022年02月21日 星期一 12时04分58秒 -0.770954 秒
根据硬件时间设置系统时间
[root@test ~]# clock
d2022年02月21日 星期一 12时01分55秒 -0.520934 秒
[root@test ~]# date
2022年 02月 21日 星期一 12:06:04 CST
[root@test ~]# clock -s
[root@test ~]# date
2022年 02月 21日 星期一 12:02:18 CST