执行date命令打印当前时间,结果中的CST就表示当前系统设置的时区
[root@taotaoplus ~]# date
2022年 08月 12日 星期五 00:18:38 CST
[root@taotaoplus ~]#
UTC:协调世界时,又称世界统一时间、世界标准时间、国际协调时间。由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC。 中国大陆、中国香港、中国澳门、中国台湾、蒙古国、新加坡、马来西亚、菲律宾、西澳大利亚州的时间与UTC的时差均为+8,也就是UTC+8。
CST:同时代表了下面4个时区:美国中部时间:Central Standard Time (USA) UT-6:00、澳大利亚中部时间:Central Standard Time (Australia) UT+9:30、中国标准时间:China Standard Time UT+8:00、古巴标准时间:Cuba Standard Time UT-4:00
这里的CST(Chinese Standard Time)指的是中国标准时间,就是通常所说的北京时间
举个例子
中国的晚上八点就可以表示为:20:00 CST
中国处在东八区(UTC+8),通过计算也可以表示为:12:00 UTC
时区对于linux时间同步的意义在于:我们从一个服务器同步时间时,服务器只会给我们给出UTC+0的时间值,因为它不知道我们在哪里
在Linux下glibc提供了很多事先编译好的时区文件, 放在/usr/share/zoneinfo目录下
[root@taotaoplus zoneinfo]# ls -F /usr/share/zoneinfo/
Africa/ Asia/ Canada/ Cuba EST GB GMT-0 HST iso3166.tab Kwajalein Mexico/ NZ Portugal PST8PDT Singapore Universal W-SU
America/ Atlantic/ CET EET EST5EDT GB-Eire GMT+0 Iceland Israel leapseconds MST NZ-CHAT posix/ right/ Turkey US/ zone1970.tab
Antarctica/ Australia/ Chile/ Egypt Etc/ GMT Greenwich Indian/ Jamaica Libya MST7MDT Pacific/ posixrules ROC tzdata.zi UTC zone.tab
Arctic/ Brazil/ CST6CDT Eire Europe/ GMT0 Hongkong Iran Japan MET Navajo Poland PRC ROK UCT WET Zulu
zdump 命令可以查看每个时区(地区)的当前时间
[root@taotaoplus ~]# zdump /usr/share/zoneinfo/UTC
/usr/share/zoneinfo/UTC Thu Aug 11 17:01:16 2022 UTC
[root@taotaoplus ~]# zdump /usr/share/zoneinfo/Hongkong
/usr/share/zoneinfo/Hongkong Fri Aug 12 01:01:26 2022 HKT
[root@taotaoplus ~]# zdump /usr/share/zoneinfo/Asia/Shanghai
/usr/share/zoneinfo/Asia/Shanghai Fri Aug 12 01:01:37 2022 CST
[root@taotaoplus ~]# zdump /usr/share/zoneinfo/Japan
/usr/share/zoneinfo/Japan Fri Aug 12 02:01:54 2022 JST
[root@taotaoplus ~]# date
2022年 08月 12日 星期五 01:03:26 CST
date命令查看的则是当前系统设置的时区的时间,那么怎么设置当前的时间呢?
/etc/localtime这个文件,这个文件定义了我们所在的时区,这是个软连接,我们可以重新创建软连接指向/usr/share/zoneinfo中定义的时区文件
举个栗子:时区修改为新加坡,再修改为上海:
oot@taotaoplus ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 2月 17 21:40 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
[root@taotaoplus ~]# ln -sf /usr/share/zoneinfo/Singapore /etc/localtime
[root@taotaoplus ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 29 8月 12 01:16 /etc/localtime -> /usr/share/zoneinfo/Singapore
[root@taotaoplus ~]# date
2022年 08月 12日 星期五 01:17:06 +08
[root@taotaoplus ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@taotaoplus ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 33 8月 12 01:17 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
[root@taotaoplus ~]# date
2022年 08月 12日 星期五 01:17:30 CST
[root@taotaoplus ~]#
当前时区

执行tzselect,进行交互式选择


简单总结:(理解这些需要知道Linux环境变量相关的知识哦Q_Q)

在一台计算机上我们有两个时钟,硬件时间时钟(Real Time Clock,RTC)和系统时钟(System Clock)
硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因
系统时钟就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值 在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了
那么既然两个时钟独自运行,那么时间久了必然就会产生误差

同步误差
# 硬件时间设置成系统时间
hwclock --hctosys
# 系统时间设置成硬件时间
hwclock --systohc
# 那么如果想设置硬件时间我们可以开机的时候在BIOS里设定.也可以用hwclock命令
hwclock --set --date="mm/dd/yy hh:mm:ss"
通常我们用date命令操作系统时间
# 修改系统时间
date -s "dd/mm/yyyy hh:mm:ss"
网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。
我们的计算机时间久了都会产生误差
NTP Server的时间是准确的
那就让时间服务器给我们的定时同步时间吧
找到给我们提供同步服务的NTP Server:
NTP的官方网站
阿里云NTP服务器

建议我们为了保障时间的准确性,最少找两个NTP Server
// todo 困了,有空再卷…