linux时间设置与同步--NTP

本文介绍了Linux系统中时区的设置方法及时间同步原理,包括CST、UTC的概念,时区文件的作用,以及如何使用date、zdump等命令查看和修改时区。此外,还详细讲解了NTP协议的工作原理及其配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时区 Time Zone

普及一点地理知识

执行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 Time Zone

在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

当前时区
在这里插入图片描述
执行tzselect,进行交互式选择
在这里插入图片描述
在这里插入图片描述
简单总结:(理解这些需要知道Linux环境变量相关的知识哦Q_Q

  • 直白点说tzselect命令只是生成信息,创建TZ环境变量才会使时区配置生效
  • TZ环境变量的时区配置会覆盖 /etc/localtime.也就是说TZ变量没有定义的时候系统才使用/etc/localtime来确定时区
  • 根据export命令的特性重启TZ环境就没了,所以你想永久修改时区的话就把TZ变量的设置直接写入/etc/profile里
方案三 timedatectl

在这里插入图片描述

时间

明确概念

在一台计算机上我们有两个时钟,硬件时间时钟(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"

NTP和时间的同步

网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。

我们的计算机时间久了都会产生误差
NTP Server的时间是准确的
那就让时间服务器给我们的定时同步时间吧

找到给我们提供同步服务的NTP Server:
NTP的官方网站
阿里云NTP服务器
在这里插入图片描述

两种时间同步工具ntpd、ntpdate

ntpd平滑同步、ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行
时钟的跃变,对于某些程序会导致很严重的问题。

ntpdate (不推荐使用)

ntp服务开启时,ntpdate 会报错,冲突!!!

# 安装 ntpdate 
yum install ntpdate -y 
# 同步时间
ntpdate cn.pool.ntp.org
# 配合定时crontab定时我任务,定时同步时间
echo "0 12 * * * /usr/sbin/ntpdate cn.pool.ntp.org" >> /var/spool/cron/root 

[root@taotaoplus ~]# crontab -l
0 12 * * * /usr/sbin/ntpdate cn.pool.ntp.org

ntp服务

概述

NTP(Network Time Protocol,网络时间协议)
C/S模式
NTP是层级结构,有顶端的服务器,多层的Relay Server再到客户端。所以服务器从高到低,级别可以设定为1~16级。为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。
在这里插入图片描述

安装:

yum -y install ntp

服务命令

systemctl enable ntpd 开机启动
systemctl start ntpd 启动服务
systemctl stop ntpd 停止服务
systemctl status ntpd 查看服务状态

服务端口

在这里插入图片描述
服务启动后的端口监听
在这里插入图片描述

配置文件:/etc/ntp.conf
  1. server
    配置的多个ntp服务器
    本机从哪里同步时间?上游时间服务器
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 配置为阿里的时间服务器
server 120.25.115.20		# ntp1.aliyun.com
server 203.107.6.88			# ntp2.aliyun.com

  1. restrict
    谁可以访问?谁可以修改我的时间?
    上级时间服务器能访问我,并且能修改我的时间
    当我作为局域网内的时间服务器,需要给局域网内的其他机器同步时间,此时就要设置这个网段的机器能访问我,但不能修改我的时间
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# 默认是拒绝所有来源的任何访问
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 120.25.115.20		# ntp1.aliyun.com 允许上级时间服务器访问,并修改我的时间
restrict 203.107.6.88		# ntp2.aliyun.com 允许上级时间服务器修改,并修改我的时间
restrict 127.0.0.1			# 自己可以访问自己(ipv4)
restrict ::1				# 自己可以访问自己(ipv6)
restrict  192.168.65.0 mask 255.255.255.0 nomodify # 允许局域网机器访问,但不能修改我的时间

其他参数暂时不关注了 以后用到再说

检查同步结果

配置完重启服务:systemctl restart ntpd
执行ntpstat,检查是否与上有服务器链接时间是否自动同步(每64s 同步一次)
PS:ntpstat需要ntp服务重启后几分钟执行
在这里插入图片描述
执行 ntpq -p,查看和上游时间服务器的连接状态
列出了所有作为时钟源校正过本地NTP服务器时钟上层NTP服务器的列表。
在这里插入图片描述

  • remote:响应这个请求的NTP服务器的名称。*表示当前选择的主服务器,+表示辅助服务器,-表示不合格的服务器,x表示服务器不可用。
  • refid:NTP服务器使用的上一级ntp服务器的IP地址
  • st:远程NTP服务器的级别。
  • t:本地NTP服务器与远程NTP服务器的通信方式。u:单播;b:广播;I:本地
  • when:上次成功请求后到现在的秒数
  • poll:本地NTP服务器与远程NTP服务器同步的时间间隔。
  • reach:这是一个八进制的值,用来测试衡量前八次查询是否成功和服务器连接。377表示都成功,0表示不成功
  • delay:网络延时,单位为微秒(μs)
  • offset:本地NTP服务器与远程NTP服务器的时间偏移,单位为毫秒(ms)。offset越接近于0,主机与NTP服务器的时间越接近
  • jitter:查询偏差的分布值,用于表示远程NTP的网络延时是否稳定,单位为微秒(μs)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值