Linux时间同步的那些事儿

来自linux中国网wiki
Evan讨论 | 贡献2020年10月16日 (五) 02:43的版本 →‎see also
跳到导航 跳到搜索

centos7(debian10)从外网同步时间和时区设置

 

# 设置亚洲时区 tzselect 有时不准备
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步 #关闭是 false 
timedatectl set-ntp true # yes
#关闭 时间同步   关了 就可以改时间了 
timedatectl set-ntp false


set-ntp [BOOL]
           Takes a boolean argument. Controls whether network time synchronization is active and enabled (if available). If the argument is true, this enables and starts the first
           existed service listed in the environment variable $SYSTEMD_TIMEDATED_NTP_SERVICES of systemd-timedated.service. If the argument is false, then this disables and stops the
           all services listed in $SYSTEMD_TIMEDATED_NTP_SERVICES.

#NOTE
ntpdate cn.pool.ntp.org #发现有效果   直接这样同步就行了,不用像下面一样设置一个时间点 

date -s "2019-05-23 01:01:01"

019
[root@prod-sns-php01 ~]#  timedatectl set-ntp true 
Failed to set ntp: NTP not supported.
 yum install -y ntp


也许可以用这个
ln -sf  /usr/share/zoneinfo/Asia/Shanghai   /etc/localtime



#下面几个可以不要    安装
yum install chrony
# 启用
systemctl start chronyd
systemctl enable chronyd

timedatectl set-time 15:58:30
Failed to set time: Automatic time synchronization is enabled
root@debian-hyper:~# timedatectl set-ntp  no   #or
timedatectl set-ntp false
root@debian-hyper:~# timedatectl set-time 15:58:30  #修改时间 


 

基础知识

timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系统和服务管理器的一部分,代替旧的传统的用在基于Linux分布式系统的sysvinit守护进程的date命令、
在最新的 Ubuntu 版本中,timedatectl 替代了老旧的 ntpdate。默认情况下,timedatectl 在系统启动的时候会立刻同步时间,并在稍后网络连接激活后通过 socket 再次检查一次。
 如果已安装了 ntpdate / ntp,timedatectl 会退而让你使用之前的设置。这样确保了两个时间同步服务不会相互冲突,同时在你升级的时候还保留原本的行为和配置。但这也意味着从旧版本的发行版升级时ntp/ntpdate 仍会安装,因此会导致新的基于 systemd 的时间服务被禁用。


   UTC 整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
   GMT 格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)
   CST 中国标准时间 (China Standard Time)


timedatectl时钟同步

CentOS 7时间和日期和时间同步

centos7 chrony 自建的时间同步 server

Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确。它由两个程序组成:chronyd和chronyc。
          chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
          Chrony 是网络时间协议的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地且准确的同步系统时钟。


 centos7  建议是用 chrony

yum -y install chrony ntp  -y

systemctl start chronyd
systemctl status chronyd
systemctl enable  chronyd


timedatectl set-timezone Asia/Shanghai

timedatectl set-ntp true
 
 crontab  -l  #如果实在要用
* */2 * * * ntpdate time.windows.com

yum install chrony -y 

#改配置
 cat /etc/chrony.conf 

 #没用的全删除了 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server   news.neu.edu.cn iburst 
#server 0.centos.pool.ntp.org iburst


systemctl  start chronyd.service && systemctl  enable chronyd.service


测试 

故意超时 
date -s "2020-11-11 11:11:11"

哪个命令fix对了时间的
重启这个就行了 其实是不是不用动也行 过了阵就会自己同步
  systemctl restart chronyd.service

timedatectl set-ntp yes

chrony 使用

使用chronyc
你也可以通过运行chronyc命令来修改设置,命令如下:

accheck - 检查NTP访问是否对特定主机可用

activity - 该命令会显示有多少NTP源在线/离线



add server - 手动添加一台新的NTP服务器。

clients - 在客户端报告已访问到服务器

delete - 手动移除NTP服务器或对等服务器

settime - 手动设置守护进程时间

tracking - 显示系统时间信息



#有用 ?
将系统时间写入硬件时间

 sudo hwclock --systohc

老方法 centos7

#proxmox 上的 vm centos7 老是时间不对 ,以前用的ntpd老是不成功呢 

yum install ntp  -y
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp true
ntpq -p


#设置系统时间为中国时区并启用NTP同步
yum install ntp -y#//安装ntp服务
systemctl enable ntpd #//开机启动服务
systemctl start ntpd #//启动服务
timedatectl set-timezone Asia/Shanghai #//更改时区
timedatectl set-ntp yes # 用的是true ?//启用ntp同步
ntpq -p //同步时间



CentOS配置时间同步NTP
为什么要使用ntpd而不是ntpdate?
原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作)

NTP通信协议原理

/usr/sbin/ntpd: 主要提供 NTP 服务的程序啰!配置文件为 /etc/ntp.conf
/usr/sbin/ntpdate: 用于客户端的时间校正,如果你没有要启用 NTP 而仅想要使用 NTP Client 功能的话,那么只会用到这个指令而已啦!

#这个运行不了 ali的是直接跑在  /etc/ntpd.conf配置的   
*/10 * * * * /usr/sbin/ntpd cn.pool.ntp.org > /dev/null  2>&1

*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null  2>&1

debian

UTC时区切换到CST 时区

#用这个啦 
# 设置亚洲时区
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步 #关闭是 false 
timedatectl set-ntp yes


 echo "export TZ='Asia/Shanghai'"  >> /etc/profile  
 cat /etc/profile |grep TZ  
source /etc/profile
date -R
 date 
Sat Aug 19 17:03:17 CST 2017

debian 手工修改时间  

#将系统日期设定成1996年6月10日的命令
date -s 06/22/96

sudo  date  -s 10:20:00
sudo hwclock -w   #当前时间和日期写入BIOS,避免重启后失效 2020用了这种 有效果

我的kali

本本老是时间不对

ntpdate cn.pool.ntp.org #发现有效果 


1,查看debian当前时间
 debian:~# date

2,手动更改系统时间

 debian:~# date 120110552007.12
 顺序为      月 日时分 年 . 秒

3,同步Internet时间,首先安装时间同步软件

 apt-get install ntpdate

4,手动同步系统时间

 ntpdate cn.pool.ntp.org

5,如果重新启动后时间不是Internet时间可采取以下方法

vim   /etc/default/ntpdate
法1.修改/etc/default目录下的ntpdate
  在配置文件中发现了它设定时间服务器为cn.pool.ntp.org,
  
# Not used if NTPDATE_USE_NTP_CONF is yes.
NTPSERVERS="cn.pool.ntp.org  0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org"
#NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org"

法2.做一个同步的脚本ntpupdate在开机时调用,内容如下

  #!/bin/sh
  /usr/sbin/ntpdate time.nist.gov | logger -t NTP
  /sbin/hwclock -w

Linux 修改系统时间成功未生效

使用 date -s “2019-02-02 02:02:02” 命令修改系统时间,提示修改成功但是系统时间未发生变化;


使用timedatectl命令查看系统时间

 timedatectl 
               Local time: Wed 2020-02-26 22:28:22 PST
           Universal time: Thu 2020-02-27 06:28:22 UTC
                 RTC time: Thu 2020-02-27 06:28:22
                Time zone: America/Los_Angeles (PST, -0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

关闭ntp同步 timedatectl set-ntp false



see also

Linux基础:timedatectl命令使用介绍

Linux下使用timedatectl命令时间时区操作详解

Linux时间同步命令:ntpdate(系统时间)、hwclock(硬件时间)

Linux查看与修改时区、时间的命令

How To Set Up Time Synchronization on Debian 10

Centos7 NTP网络时间协议

CentOS 7 时间, 日期设置 (含时间同步)

http://cn.linux.vbird.org/linux_server/0440ntp.php

CentOS 7 时间, 日期设置

CentOS7 中使用NTP进行时间同步

使用NTP进行CentOS7时间同步

linux时间同步,ntpd、ntpdate

服务器时间同步


ntpd时钟同步服务

chrony

使用chrony代替ntp同步时间

详解:Linux Chrony 设置服务器集群同步时间

centos7.4 用chrony代替ntpd时间同步服务器

Centos 7.3_1611使用chrony做时间同步

linux中时间同步服务(chronyd服务)以及系统时间设定

CentOS7.2 chrony替代ntp搭建时间服务器

CentOS 7.x中正确设置时间与时钟服务器同步

CentOS7.2 chrony替代ntp搭建时间服务器