基本概念
- linux系统有两个时间,一个是系统时间(System Clock),通过内核模拟出来的时间,一个是硬件时间(Real Time
Clock,简称RTC),通过读取BIOS的时间。系统时间和硬件时间彼此之间可单独运行。 - centos7使用timedatectl查看的是系统时间(centos早期版本使用date命令),使用clock或者hwclock查看的是硬件时间。
tips:
硬件时间(RTC)建议用UTC( Universal Time Coordinated世界协调时间)或本地时间,建议使用UTC;CST (China Standard Time中国标准时间) UT+8:00
1.显示当前系统/硬件的日期和时间、时区信息:
timedatectl status
2.列出系统可用的时区:
timedatectl list-timezones
3.设置系统时区为上海:
timedatectl set-timezone "Asia/Shanghai"
4.设置系统日期和时间:(如果开启了NTP时间同步,则设置无效)
tips:日期和时间可以分来来设置,但是需要遵循下列各式:“YYYY-MM-DD HH:MM:SS”。
timedatectl set-time '2019-05-08 21:45:00'
或者使用date -s 设置:
date -s '20190508 21:45:00'
5.设置时钟时区:
查看设置硬件时钟是否设置为本地时区
timedatectl | grep local
将硬件时钟设置为本地时区:
timedatectl set-local-rtc 1
将你的硬件时钟设置为协调世界时(UTC):
timedatectl set-local-rtc 0
NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。
6.设置自动同步或不同步NTP时间:
安装ntp、ntpdate服务
yum install -y ntp ntpdate
先测试手动同步时间服务器(注意这个不能用于生产服务器)
ntpdate asia.pool.ntp.org
设置自动同步NTP时间服务器
timedatectl set-ntp yes #no表示禁用
设置开机启动
systemctl enable ntpd.service
最终结果如下: