阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux服务器时间同步

29次阅读
没有评论

共计 2014 个字符,预计需要花费 6 分钟才能阅读完成。

导读 Linux 服务器运行久时,系统时间就会存在一定的误差,一般情况下可以使用 date 命令进行时间设置,但在做数据库集群分片等复杂操作时对多台机器的时间差是有要求的,此时就需要使用 ntpdate 进行时间同步。
Linux 时间的操作的常见命令
date 命令

使用方式 :

date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]

Linux 服务器时间同步
1.date : 查看当前时间,结果如下:

[root@linuxprobe ~]# date
2016 年 04 月 29 日 星期五 23:04:30 CST

2.date ‘+%Y%M%D’ 按照格式显示当前日期, 结果如下:

[root@linuxprobe ~]# date "+%Y-%m-%d %H:%M:%S"
2016-04-29 23:30:12

3.date -s “xxx xx:xx:xx” 设置系统时间

[root@linuxprobe ~]# date -s "20160429 15:30:00"
2016 年 04 月 29 日 星期五 15:30:00 CST

以上的命令为 linux 系统时间的常见操作,具体命令见:《linux 就该这样学》第 2 章 新手必须掌握的 Linux 命令。

linux 的时间同步

下面我们来看一下我们在复杂环境下怎么把多台 linux 设置统一的正确环境:对 - 时间同步
命令:utpdate 时间服务器 ip
ntpdate -u xx.xx.xx.xx 结果如下:

[root@linuxprobe ~]# ntpdate -u ntp.api.bz
29 Apr 15:26:27 ntpdate[2667]: step time server 61.153.197.226 offset -311.570389 sec
[root@linuxprobe ~]# date
2016 年 04 月 29 日 星期五 15:26:32 CST

ps:若不加上 - u 参数,会出现以下提示:no server suitable for synchronization found
-u:从 man ntpdate 中可以看出 - u 参数可以越过防火墙与主机同步;

ntp 常用服务器:

 中国国家授时中心:210.72.145.44
NTP 服务器 (上海):ntp.api.bz
美国:time.nist.gov
复旦:ntp.fudan.edu.cn
微软公司授时主机 (美国):time.windows.com
北京邮电大学 : s1a.time.edu.cn
清华大学 : s1b.time.edu.cn
北京大学 : s1c.time.edu.cn
台警大授时中心 (台湾):asia.pool.ntp.org
时区操作
查看时区:

1.date “+%Z”

[root@linuxprobe ~]# date "+%Z"
[root@linuxprobe ~]# CST

2. 使用 cat /etc/sysconfig/clock 查看当前时区

[root@linuxprobe ~]# cat /etc/sysconfig/clock
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"
设置时区:

复制相应的时区文件,替换系统默认时区 文件链接法
[root@linuxprobe ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@linuxprobe ~]#

ps:/usr/share/zoneinfo 为所有不通地区的时区位置
[root@linuxprobe ~]# ls /usr/share/zoneinfo/Asia/
Aden Bahrain Chungking Harbin Jerusalem Kuala_Lumpur Nicosia Qyzylorda Seoul Thimphu Yekaterinburg
……
Almaty Baku Colombo
Baghdad Chongqing Gaza Jayapura Krasnoyarsk Muscat Qatar Samarkand Thimbu Yakutsk

将系统时间写入到硬件(bios)

1.hwclock

[root@linuxprobe ~]# hwclock
2016 年 04 月 29 日 星期五 15 时 39 分 30 秒 -0.375972 seconds

2.clock -w

[root@linuxprobe ~]# clock -w
[root@linuxprobe ~]#

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2014字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中