页面“Mysqld multi:管理多个MySQL服务器的程序-mysql多实例”与“Linux时间同步的那些事儿”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
 +
== centos7(debian10)从外网同步时间和时区设置==
 +
<pre>
  
mysqld_multi:管理多个MySQL服务器的程序
+
# 设置亚洲时区 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  #修改时间
 +
 +
 +
</pre>
 +
 +
==基础知识==
 +
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)
 +
 +
 +
[https://my.oschina.net/airship/blog/1544886 timedatectl时钟同步]
 +
 +
[https://blog.csdn.net/l1212xiao/article/details/80328918 CentOS 7时间和日期和时间同步]
 +
 +
==centos7  chrony 自建的时间同步 server==
 
<pre>
 
<pre>
 +
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"
  
mysqld_multi可以管理多个监听不同Unix套接字文件和TCP/IP端口的连接的mysqld 进程。它可以启动或停止服务器,或报告它们的当前状态。
+
哪个命令fix对了时间的
 +
重启这个就行了 其实是不是不用动也行 过了阵就会自己同步
 +
  systemctl restart chronyd.service
  
程序寻找my.cnf中的[mysqldN]组(或--config-file选项指定的文件)。N 可以为任何正整数。在下面的讨论中该数字指选项组号,或GNR。组号区别各选项组,并用作mysqld_multi的参数来指定想要启动、停止哪个服务器或获取哪个服务器的状态报告。这些组中的选项与将用来启动mysqld的[mysqld]组中的相同。(例如,参见2.9.2.2节,“自动启动和停止MySQL”)。但是,当使用多个服务器时,需要每个服务器使用自己的选项值,例如Unix套接字文件和TCP/IP端口号。关于在多服务器环境中,每个服务器对应唯一选项的详细信息,参见5.12节,“在同一台机器上运行多个MySQL服务器”。
+
timedatectl set-ntp yes
  
要想调用mysqld_multi,使用下面的语法:
+
</pre>
 +
===chrony 使用 ===
 +
<pre>
 +
使用chronyc
 +
你也可以通过运行chronyc命令来修改设置,命令如下:
  
shell> mysqld_multi [''options''] {start|stop|report} [''GNR''[,''GNR''] ...]<br/> start、stop和report表示你想要执行的操作。你可以在单个服务器或多个服务器上执行指定的操作,取决于选项名后面的GNR 列。如果没有该列,mysqld_multi为选项文件中的所有服务器执行该操作。
+
accheck - 检查NTP访问是否对特定主机可用
  
每个GNR值代表一个选项组号或组号范围。GNR值应为选项文件中组名末尾的号。例如,组[mysqld17]的GNR为17。要想指定组号的范围,用破折号间隔开第1个和最后1个号。GNR值10-13代表组[mysqld10]到[mysqld13]。可以在命令行中指定多个组或组范围,用逗号间隔开。GNR列不能有空格字符(空格或tab);空格字符后面的内容将被忽略掉。
+
activity - 该命令会显示有多少NTP源在线/离线
  
该命令使用选项组[mysqld17]启动单个服务器:
 
  
shell> mysqld_multi start 17<br/> 该命令停止多个服务器,使用选项组[mysql8]和[mysqld10]至[mysqld13]:
 
  
shell> mysqld_multi stop 8,10-13<br/> 使用该命令列出设置选项文件的示例:
+
add server - 手动添加一台新的NTP服务器。
  
shell> mysqld_multi --example<br/> mysqld_multi支持下面的选项:
+
clients - 在客户端报告已访问到服务器
  
· --config-file=name
+
delete - 手动移除NTP服务器或对等服务器
  
指定选项文件名。这关系到mysqld_multi从哪里寻找[mysqldN]选项组。没有该选项,从通用my.cnf文件读所有选项。选项不影响 mysqld_multi从哪里读取自己的选项,总是从通用my.cnf文件的[mysqld_multi]组读取。
+
settime - 手动设置守护进程时间
  
· --example
+
tracking - 显示系统时间信息
  
显示示例选项文件。
 
  
· --help
 
  
显示帮助消息并退出。
+
#有用 ?
 +
将系统时间写入硬件时间
  
· --log=name
+
sudo hwclock --systohc
  
指定日志文件名。如果该文件存在,后面为日志输出。
+
</pre>
  
· --mysqladmin=prog_name
+
==老方法 centos7==
 +
<pre>
 +
#proxmox 上的 vm centos7 老是时间不对 ,以前用的ntpd老是不成功呢
  
用来停止服务器的mysqladmin二进制。
+
yum install ntp  -y
 +
timedatectl set-timezone Asia/Shanghai
 +
timedatectl set-ntp true
 +
ntpq -p
  
· --mysqld=prog_name
 
  
可用的mysqld二进制。请注意你还可以将该选项的值指定为mysqld_safe。选项被传递给 mysqld。确保在PATH环境变量设定值或mysqld_safe中有mysqld所在目录。
+
#设置系统时间为中国时区并启用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 //同步时间
  
· --no-log
 
  
按照标准输出打印日志信息,不要写入日志文件。默认情况下,输出写入日志文件。
+
</pre>
  
· --password=password
 
  
调用mysqladmin时使用的MySQL账户的密码。请注意该密码值不是可选项,不象其它MySQL程序。
 
  
· --silent
+
<pre>
 +
CentOS配置时间同步NTP
 +
为什么要使用ntpd而不是ntpdate?
 +
原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作)
  
禁用警告。
+
NTP通信协议原理
  
· --tcp-ip #这个我就用昨比较 多了
+
/usr/sbin/ntpd: 主要提供 NTP 服务的程序啰!配置文件为 /etc/ntp.conf
 +
/usr/sbin/ntpdate: 用于客户端的时间校正,如果你没有要启用 NTP 而仅想要使用 NTP Client 功能的话,那么只会用到这个指令而已啦!
  
通过TCP/IP端口而不是Unix套接字文件来连接每个MySQL服务器。(如果找不到套接字文件, 服务器仍然可以运行,但只能通过 TCP/IP端口访问)。默认情况下,使用Unix套接字文件进行连接。该选项影响stop和report操作。
+
#这个运行不了 ali的是直接跑在  /etc/ntpd.conf配置的 
 +
*/10 * * * * /usr/sbin/ntpd cn.pool.ntp.org > /dev/null  2>&1
  
· --user=user_name
+
*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null  2>&1
 +
</pre>
  
调用mysqladmin时使用的MySQL账户的用户名。
+
=debian=
 +
<pre>
 +
UTC时区切换到CST 时区
  
· --verbose
+
#用这个啦
 +
# 设置亚洲时区
 +
timedatectl set-timezone Asia/Shanghai
 +
# 启用NTP同步 #关闭是 false
 +
timedatectl set-ntp yes
  
更详细。
 
  
· --version
+
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 手工修改时间 
  
关于mysqld_multi的一些注解:
+
#将系统日期设定成1996年6月10日的命令
 +
date -s 06/22/96
  
· 确保停止mysqld服务器(用mysqladmin程序)的MySQL账户在各个服务器中的用户名和密码相同。并且应确保账户具有SHUTDOWN权限。如果你想要管理的服务器的管理账户有许多不同的用户名或密码,你需要在每个服务器上创建一个账户,并具有相同的用户名和密码。例如,你可以执行下面的命令为每个服务器设置一个普通multi_admin账户:
+
sudo  date  -s 10:20:00
 +
sudo hwclock -w  #当前时间和日期写入BIOS,避免重启后失效 2020用了这种 有效果
  
· shell> mysql -u root -S /tmp/mysql.sock -p''root_password''<br/> · mysql> GRANT SHUTDOWN ON *.*<br/> · -> TO 'multi_admin'@'localhost' IDENTIFIED BY 'multipass';<br/> 参见5.7.2节,“权限系统工作原理”。你必须为每个mysqld服务器执行该操作。当连接时适当更改连接参数。请注意账户名的主机部分必须允许你用multi_admin从你想要运行mysqld_multi的主机进行连接。
+
</pre>
  
· 如果你使用mysqld_safe来启动mysqld(例如,--mysqld=mysqld_safe),--pid-file选项很重要。每个mysqld应有自己的进程ID文件。使用mysqld_safe而不使用mysqld的好处是mysqld_safe“守护”其mysqld进程,如果用kill –9发送的信号或由于其它原因(例如分段故障)进程终止,则重启进程。请注意mysqld_safe脚本需要你从某个位置启动它。这说明运行mysqld_multi前你必须进入某个目录。如果启动时有问题,请参见mysqld_safe脚本。特别是要检查下列行:
+
==我的kali==
 +
本本老是时间不对 
 +
ntpdate cn.pool.ntp.org #发现有效果
  
· ----------------------------------------------------------------<br/> · MY_PWD=`pwd`<br/> · # Check if we are starting this relative (for the binary release)<br/> · if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \<br/> · -x ./bin/mysqld<br/> · ----------------------------------------------------------------<br/> 参见5.1.3节,“mysqld_safe:MySQL服务器启动脚本”。上述行执行的测试应成功,否则你可能遇到了问题。
 
  
· 每个mysqld的Unix套接字文件和TCP/IP端口号必须不同。
+
1,查看debian当前时间
 +
  debian:~# date
 +
2,手动更改系统时间
 +
  debian:~# date 120110552007.12
 +
  顺序为      月 日时分 年 . 秒
  
· 你可能想要为mysqld使用--user选项,但为此你需要用Unix root用户运行mysqld_multi脚本。选项文件中有选项不要紧;如果你不是超级用户,并且你用自己的Unix账户重启mysqld进程,你只会得到警告。
+
3,同步Internet时间,首先安装时间同步软件
 +
  apt-get install ntpdate
  
· 重要:确保mysqld进程启动所用Unix账户可以完全访问数据目录。不要使用Unix root账户,除非你知道你在做什么。
+
4,手动同步系统时间
 +
  ntpdate cn.pool.ntp.org
  
· 非常重要:使用mysqld_multi前,确保理解传递给mysqld服务器的选项的含义以及你为什么想要独立的mysqld进程。应清楚 在相同的数据目录下使用多个mysqld服务器的危险。使用单独的数据目录,除非你知道你在做什么。在线程系统中,在相同的数据目录下启动多个服务器不会得到超性能。参见5.12节,“在同一台机器上运行多个MySQL服务器”。
+
5,如果重新启动后时间不是Internet时间可采取以下方法
 +
<pre>
 +
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"
  
下面的示例显示了你如何设置选项文件来使用mysqld_multi。专门省去第1个和第5个[mysqldN]组来说明你的选项文件可以稍有不同。这样给你更大的灵活性。mysqld程序重启或停止的顺序由它们在选项文件中的顺序决定。
+
</pre>
  
#This file should probably be in your home dir (~/.my.cnf)<br/> # or /etc/my.cnf<br/> # Version 2.1 by Jani Tolonen
+
法2.做一个同步的脚本ntpupdate在开机时调用,内容如下
 +
<pre>
 +
  #!/bin/sh
 +
  /usr/sbin/ntpdate time.nist.gov | logger -t NTP
 +
  /sbin/hwclock -w</pre>
 +
==Linux 修改系统时间成功未生效==
 +
<pre>
 +
使用 date -s “2019-02-02 02:02:02” 命令修改系统时间,提示修改成功但是系统时间未发生变化;
  
[mysqld_multi]<br/> mysqld = /usr/local/bin/mysqld_safe<br/> mysqladmin = /usr/local/bin/mysqladmin<br/> user = multi_admin<br/> password = multipass
 
  
[mysqld2]<br/> socket = /tmp/mysql.sock2<br/> port = 3307<br/> pid-file = /usr/local/mysql/var2/hostname.pid2<br/> datadir = /usr/local/mysql/var2<br/> language = /usr/local/share/mysql/english<br/> user = john
+
使用timedatectl命令查看系统时间
  
[mysqld3]<br/> socket = /tmp/mysql.sock3<br/> port = 3308<br/> pid-file = /usr/local/mysql/var3/hostname.pid3<br/> datadir = /usr/local/mysql/var3<br/> language = /usr/local/share/mysql/swedish<br/> user = monty
+
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
  
[mysqld4]<br/> socket = /tmp/mysql.sock4<br/> port = 3309<br/> pid-file = /usr/local/mysql/var4/hostname.pid4<br/> datadir = /usr/local/mysql/var4<br/> language = /usr/local/share/mysql/estonia<br/> user = tonu
 
  
[mysqld6]<br/> socket = /tmp/mysql.sock6<br/> port = 3311<br/> pid-file = /usr/local/mysql/var6/hostname.pid6<br/> datadir = /usr/local/mysql/var6<br/> language = /usr/local/share/mysql/japanese<br/> user = jani
 
  
<br/>
 
 
</pre>
 
</pre>
  
  http://www.iteedu.com/database/mysql/mysqlmanualcn/database-administration/mysqld-multi.php
+
=see also=
 +
 
 +
 
 +
[https://www.cnblogs.com/zhi-leaf/p/6282301.html Linux下使用timedatectl命令时间时区操作详解]
 +
 
 +
[https://www.fujieace.com/linux/man/ntpdate-hwclock.html Linux时间同步命令:ntpdate(系统时间)、hwclock(硬件时间)]
 +
 
 +
[https://www.iteye.com/blog/justcoding-1962519 Linux查看与修改时区、时间的命令 ]
 +
 
 +
 
 +
 
 +
[[Centos7 NTP网络时间协议]]
 +
 
 +
[https://www.cnblogs.com/tangxiaosheng/p/4986375.html CentOS 7 时间, 日期设置 (含时间同步)]
 +
 
 +
http://cn.linux.vbird.org/linux_server/0440ntp.php
 +
 
 +
[https://blog.itnmg.net/2015/03/03/centos-7-time-date/ CentOS 7 时间, 日期设置]
 +
 
 +
[https://blog.csdn.net/scorpio3k/article/details/49645219 CentOS7 中使用NTP进行时间同步]
 +
 
 +
[http://blog.51cto.com/dadonggg/1947882 使用NTP进行CentOS7时间同步]
 +
 
 +
[http://luijnijei.blog.163.com/blog/static/350245942010913912192/ linux时间同步,ntpd、ntpdate ]
 +
 
 +
[http://151wqooo.blog.51cto.com/2610898/1336282 服务器时间同步]
 +
 
 +
 
 +
[http://xstarcd.github.io/wiki/sysadmin/ntpd.html ntpd时钟同步服务]
 +
 
 +
==chrony==
 +
 
 +
[https://soyoger.blog.csdn.net/article/details/77868337 使用chrony代替ntp同步时间]
 +
 
 +
[https://blog.csdn.net/Linuxprobe18/article/details/80460068 详解:Linux Chrony 设置服务器集群同步时间]
 +
 
 +
[https://blog.csdn.net/openbox2008/article/details/80250027 centos7.4 用chrony代替ntpd时间同步服务器]
 +
 
 +
[https://blog.csdn.net/JIANG123456T/article/details/77966627 Centos 7.3_1611使用chrony做时间同步]
 +
 
 +
[https://blog.csdn.net/weixin_43875013/article/details/86485798 linux中时间同步服务(chronyd服务)以及系统时间设定]
 +
 
 +
[https://blog.csdn.net/hnhuangyiyang/article/details/52711631 CentOS7.2 chrony替代ntp搭建时间服务器]
 +
 
 +
[https://blog.csdn.net/weixin_34290631/article/details/90571832 CentOS 7.x中正确设置时间与时钟服务器同步]
  
= 星期四 05 1月 2017 =
+
[https://blog.csdn.net/hnhuangyiyang/article/details/52711631  CentOS7.2 chrony替代ntp搭建时间服务器]
  
[[Category:Mysql]]
+
[[category:ops]]

2020年10月14日 (三) 07:43的版本

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时间同步命令:ntpdate(系统时间)、hwclock(硬件时间)

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


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搭建时间服务器