CentOS系統時間同步ntp



---centos7.2上搭建ntp服務器,並實現時間同步


對於容器編排系統,前段時間主要研究kubernetes,現在實驗室要用dcos,所以在實驗室集羣上搭建了該系統。搭建dcos集羣需要各機器時間同步,而實驗室集羣並沒有聯網,這裏講述下怎麼搭建ntp服務器並進行時間同步。 
使用的系統爲centos7.2,機器使用情況如下表所示,這裏以10.107.18.35爲ntp server,其他的爲client對時間進行同步,本文系統使用的是timedatectl命令。


機器ip 作用
10.107.18.35 單獨一臺boot節點
10.107.18.37 單獨一臺master節點
10.107.13.150 4臺slave節點
10.107.18.34 4臺slave節點
10.107.18.38 4臺slave節點
10.107.18.39 4臺slave節點
2. 具體流程


1.在集羣中所有節點上安裝ntp 
yum install ntp 
2.所有節點設置時區,這裏設置爲中國所用時間timedatectl set-timezone Asia/Shanghai 
3.在server節點上啓動ntp服務


systemctl start ntpd
systemctl enable ntpd
4.在server節點上設置現在的準確時間 
timedatectl set-time HH:MM:SS 
5.在server節點上設置其ntp服務器爲其自身,同時設置可以接受連接服務的客戶端,是通過更改/etc/ntp.conf文件來實現的,其中server設置127.127.1.0爲其自身,新增加一個restrict段爲可以接受服務的網段 
這裏寫圖片描述 
6.重啓ntpd服務 
systemctl restart ntpd 
7.在client節點上設置ntp服務器爲server節點 
這裏寫圖片描述 
8.在client節點上同步server的時間 
ntpdate 10.107.18.35 
9.client節點啓動ntpd服務


systemctl start ntpd
systemctl enable ntpd
10.所有節點啓動時間同步 
timedatectl set-ntp yes




--解決上面的時間設置報錯問題


Centos7中增加了timedate命令來時間對日期、時間和時區的控制。
[root@node1 ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...


Query or change system time and date settings.


  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode


Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled


一、顯示日期、時間、時區等相關信息
[root@node1 ~]# timedatectl
      Local time: 日 2016-04-17 00:20:06 CST
  Universal time: 六 2016-04-16 16:20:06 UTC
        RTC time: 六 2016-04-16 16:20:06
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a


二、修改時間
執行如下命令修改時間:
timedatectl set-time HH:MM:SS
備註:同時修改hardware clock和system clock.
例子:
[root@node1 ~]# timedatectl set-time 09:57:30
[root@node1 ~]# date
2016年 04月 17日 星期日 09:57:32 CST


三、修改日期
執行如下命令修改日期:
timedatectl set-time YYYY-MM-DD
 備註:修改日誌不同時修改時間,將重置現在的時間爲00:00:00.
例子:
[root@node1 ~]# timedatectl set-time 2016-04-18
[root@node1 ~]# date
2016年 04月 18日 星期一 00:00:01 CST


四、同時修改日期和時間
執行如下命令同時修改日期和時間:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'


例子:
[root@node1 ~]# timedatectl set-time '2016-04-18 10:01:20'
[root@node1 ~]# date
2016年 04月 18日 星期一 10:01:21 CST


五、修改時區
執行如下命令顯示可用的時區:
timedatectl list-timezones
[root@node1 ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
...
執行如下命令設置使用的時區:
timedatectl set-timezone time_zone
[root@node1 ~]# timedatectl set-timezone Asia/Shanghai
[root@node1 ~]# date
2016年 04月 18日 星期一 10:05:28 CST
[root@node1 ~]# ls -lrt /etc/localtime
lrwxrwxrwx 1 root root 35 4月  17 00:08 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai


六、同步時間到一個遠程服務器
timedatectl命令可以用來控制是否開啓NTP,開啓NTP將啓動chronyd或者ntpd服務,依賴於被安裝的那個。
timedatectl set-ntp boolean
開啓:
timedatectl set-ntp yes
關閉:
timedatectl set-ntp no
備註:
執行set-ntp時會同時開啓或關閉ntpd或者chronyd服務。但是ntpd服務和chronyd可以通過systemctl命令來單獨控制,不是必須使用timedatectl來進行控制。
如果使用set-ntp是yes的狀態(即:timedatectl命令中NTP enabled狀態顯示爲yes,那麼將不能同時使用set-time來修改時間。
[root@node3 ~]# timedatectl
      Local time: 一 2016-04-18 10:25:48 CST
  Universal time: 一 2016-04-18 02:25:48 UTC
        RTC time: 六 2016-04-16 17:09:34
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@node3 ~]# timedatectl set-time '2016-04-18 10:25:58'
Failed to set time: Automatic time synchronization is enabled
[root@node3 ~]# timedatectl set-ntp no
[root@node3 ~]# timedatectl
      Local time: 一 2016-04-18 10:26:16 CST
  Universal time: 一 2016-04-18 02:26:16 UTC
        RTC time: 六 2016-04-16 17:10:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@node3 ~]# timedatectl set-time '2016-04-18 10:26:28'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章