歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux服務 >> Linux服務器時間同步

Linux服務器時間同步

日期:2017/3/1 11:57:37   编辑:Linux服務
Linux服務器運行久時,系統時間就會存在一定的誤差,一般情況下可以使用date命令進行時間設置,但在做數據庫集群分片等復雜操作時對多台機器的時間差是有要求的,此時就需要使用ntpdate進行時間同步。 Linux時間的操作的常見命令 date命令

使用方式 :

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

timeserver
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 ~]#

Copyright © Linux教程網 All Rights Reserved