轉自 https://blog.csdn.net/u010226454/article/details/80896959 node
---centos7.2上搭建ntp服務器,並實現時間同步centos
對於容器編排系統,前段時間主要研究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. 具體流程ide
1.在集羣中全部節點上安裝ntp
yum install ntp
2.全部節點設置時區,這裏設置爲中國所用時間timedatectl set-timezone Asia/Shanghai
3.在server節點上啓動ntp服務ui
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服務this
systemctl start ntpd
systemctl enable ntpd
10.全部節點啓動時間同步
timedatectl set-ntp yescentos7
--解決上面的時間設置報錯問題.net
Centos7中增長了timedate命令來時間對日期、時間和時區的控制。
[root@node1 ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...rest
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
1、顯示日期、時間、時區等相關信息
[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
2、修改時間
執行以下命令修改時間:
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
3、修改日期
執行以下命令修改日期:
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
4、同時修改日期和時間
執行以下命令同時修改日期和時間:
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
5、修改時區
執行以下命令顯示可用的時區:
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
6、同步時間到一個遠程服務器
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'
-------------------------------------------------------------------------------------
CentOS6
1.時區修改
[root@gisserver3 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
……
2.時間同步
ntpdate 0.arch.pool.ntp.org