Linux -CentOS-时间同步-记录

在性能测试过程中,经常会遇到时钟不同步的问题,需要同步时间,如果没有运维的支持,测试自己也需要1.  安装ntpdate工具

# yum -y install ntp ntpdate

2.  设置系统时间与网络时间同步

# ntpdate cn.pool.ntp.org

3.  将系统时间写入硬件时间

# hwclock --systohc

能够进行操作。这里记录一下一个完整的时区同步操作例子,希望能帮助自己和需要的同学。

1.查看当前服务器时区&列出时区并设置时区

# timedatectl

# timedatectl list-timezones

# timedatectl set-timezone Asia/Shanghai

2.时间时区概念理解:

GMT、UTC、CST、DST

UTC:

整个地球分为二十四时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC:Universal Time Coordinated)。

GMT:

格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线(UTC与GMT时间基本相同)。

CST:

中国标准时间 (China Standard Time)

GMT + 8 = UTC + 8 = CST

DST:

夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用(中国不使用)。

2.使用 NTP 公共时间服务器池(NTP Public Pool Time Servers)http://www.pool.ntp.org 同步你的服务器时间。

查看是否安装:

# rpm -q ntp

ntp-4.2.6p5-25.el7.centos.2.x86_64

如已安装请略过此步,否则请执行以下命令安装:

# yum install ntpdate ntp -y

修改NTP配置:

# vim /etc/ntp.conf

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

#新增:日志目录.

logfile /var/log/ntpd.log

# 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 127.0.0.1

restrict ::1

#这一行的含义是授权172.16.128.0网段上的所有机器可以从这台机器上查询和同步时间.

restrict 172.16.128.0 mask 255.255.255.0 nomodify notrap

# Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#server 0.centos.pool.ntp.org iburst

#server 1.centos.pool.ntp.org iburst

#server 2.centos.pool.ntp.org iburst

#server 3.centos.pool.ntp.org iburst

#新增:时间服务器列表.

server 0.cn.pool.ntp.org iburst

server 1.cn.pool.ntp.org iburst

server 2.cn.pool.ntp.org iburst

server 3.cn.pool.ntp.org iburst

#新增:当外部时间不可用时,使用本地时间.

server 172.16.128.171 iburst

fudge 127.0.0.1 stratum 10

#broadcast 192.168.1.255 autokey # broadcast server

#broadcastclient # broadcast client

#broadcast 224.0.1.1 autokey # multicast server

#multicastclient 224.0.1.1 # multicast client

#manycastserver 239.255.254.254 # manycast server

#manycastclient 239.255.254.254 autokey # manycast client

#新增:允许上层时间服务器主动修改本机时间.

restrict 0.cn.pool.ntp.org nomodify notrap noquery

restrict 1.cn.pool.ntp.org nomodify notrap noquery

restrict 2.cn.pool.ntp.org nomodify notrap noquery

# Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating

# with symmetric key cryptography.

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.

#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.

#requestkey 8

# Specify the key identifier to use with the ntpq utility.

#controlkey 8

# Enable writing of statistics records.

#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc

# monlist command when default restrict does not include the noquery flag. See

# CVE-2013-5211 for more details.

# Note: Monitoring will not be disabled with the limited restriction flag.

disable monitor

3.设置系统开机自启动:

# systemctl enable ntpd

# systemctl enable ntpdate

# systemctl is-enabled ntpd

在ntpd服务启动时,先使用ntpdate命令同步时间:

# ntpdate -u 1.cn.pool.ntp.org

原因之一:当服务端与客户端之间的时间误差过大时,此时修改时间可能对系统或应用带来不可预知问题,NTP会停止时间同步。若检查NTP启动后时间未同步时,应考虑可能是时间误差过大所致,此时需要先手动进行时间同步。

启动NTP服务器:

# systemctl start ntpdate

# systemctl start ntpd

4.加入防火墙:

# firewall-cmd --permanent --add-service=ntp

# firewall-cmd --reload

5.查看ntp连接状态如果没有问题,将正确时间写入硬件:

# ss -tlunp | grep ntp

# ntpq -p

# hwclock -w

5.1.系统时间与硬件时间

硬件时间:

RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。

系统时间:

一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

6.客户端:

以服务进程方式实时同步(需安装NTP):

# vim /etc/ntp.conf

server 172.16.128.171

重要:修改任意节点服务器的NTP配置文件都需要重起ntpd服务:

# systemctl restart ntpd

以crontab任务计划同步时间(需安装ntpdate,每天24点更新同步时间):

# crontab -e

0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w

已部署完毕。这样集群会自动定期进行服务的同步,如此以来集群的时间就保持一致了。

备注:如果时间未同步可以通过 ntpdate  ntpserver 的方式 手动同步一下(需要停止ntpd 服务)

ntpdate -u 182.190.180.123  (时间服务器)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,192评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,186评论 1 303
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,844评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,471评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,876评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,891评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,068评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,791评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,539评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,772评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,250评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,577评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,244评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,146评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,949评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,995评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,812评论 2 276

推荐阅读更多精彩内容