侧边栏壁纸
博主头像
运维贼船的技术小站 博主等级

行动起来,活在当下

  • 累计撰写 30 篇文章
  • 累计创建 19 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux下的时间同步方式

aoarasi
2025-06-09 / 0 评论 / 0 点赞 / 9 阅读 / 0 字

一.使用ntpdate

ntpdate是Linux系统下专门用于时间同步的工具之后,使用起来简单高效,采用以下方法安装。

# ubuntu类操作系统
sudo apt-get install ntpdate
​
#CentOS类操作系统
sudo yum install ntpdate

安装完成后,使用以下命令验证,如果输出正常则安装成功。

[root@test ~]# ntpdate -v
 7 Oct 21:52:29 ntpdate[28185]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
 7 Oct 21:52:29 ntpdate[28185]: no servers can be used, exiting

使用ntpdate工具同步时间时,需要参照一个时间服务器,通常我们采用阿里云提供的授时服务器,国内速度较快,使用以下命令进行同步。

ntpdate ntp.aliyun.com
# 备用
ntpdate ntp1.aliyun.com
​
# 显示如下,成功
[root@test ~]# ntpdate ntp.aliyun.com
 7 Oct 21:55:40 ntpdate[23343]: adjust time server 203.107.6.88 offset 0.006025 sec
[root@test ~]# 
# 最后把时间写到固件里
clock -w

二.使用chrony

chrony可以作为时间同步的服务器,也可以做客户端,通常也与ntpdate工具搭配使用。 此工具的安装方法也很简单,在Centos默认是安装的,如果你的系统没有安装,使用以下命令进行。

# ubuntu类操作系统
sudo apt-get install chrony
​
#CentOS类操作系统
sudo yum install chrony

安装完成,修改配置文件

# 修改chrony的配置文件
vim /etc/chrony.conf
# 修改3-6行,第3行,把服务器改为阿里云。4-6行注释掉
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server ntp.aliyun.com iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
​
# 重启服务生效
[root@test ~]# systemctl start chronyd

查看同步效果

[root@mx ~]# chronyc sources -v
210 Number of sources = 1
​
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6    17    55   -197us[-1258us] +/-   29ms
[root@mx ~]# 

如果最后一行为^*开头,并后面附带IP地址,表示成功,此时时间也同步了。 至此!

0

评论区