1.环境信息
Linux:CentOS Linux release 7.1.1503 (Core)
2. 安装ntp包
包名称如下(存在依赖关系,按顺序安装,yum 就无所谓了):
autogen-libopts-5.18-5.el7.x86_64.rpm
ntpdate-4.2.6p5-25.el7.centos.2.x86_64.rpm
ntp-4.2.6p5-25.el7.centos.2.x86_64.rpm
3.安装
[root@master yum.repos.d]# yum install yum-downloadonly ntp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: centos.ustc.edu.cn
* updates: mirrors.sohu.com
No package yum-downloadonly available.
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-25.el7.centos.2 will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-25.el7.centos.2 for package: ntp-4.2.6p5-25.el7.centos.2.x86_64
--> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-25.el7.centos.2.x86_64
--> Running transaction check
---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed
---> Package ntpdate.x86_64 0:4.2.6p5-25.el7.centos.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================================================================================================
Installing:
ntp x86_64 4.2.6p5-25.el7.centos.2 base 547 k
Installing for dependencies:
autogen-libopts x86_64 5.18-5.el7 base 66 k
ntpdate x86_64 4.2.6p5-25.el7.centos.2 base 86 k
Transaction Summary
============================================================================================================================================================================================================================================
Install 1 Package (+2 Dependent packages)
Total size: 699 k
Total download size: 152 k
Installed size: 1.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): autogen-libopts-5.18-5.el7.x86_64.rpm | 66 kB 00:00:00
(2/2): ntpdate-4.2.6p5-25.el7.centos.2.x86_64.rpm | 86 kB 00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 331 kB/s | 152 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : autogen-libopts-5.18-5.el7.x86_64 1/3
Installing : ntpdate-4.2.6p5-25.el7.centos.2.x86_64 2/3
Installing : ntp-4.2.6p5-25.el7.centos.2.x86_64 3/3
Verifying : ntp-4.2.6p5-25.el7.centos.2.x86_64 1/3
Verifying : ntpdate-4.2.6p5-25.el7.centos.2.x86_64 2/3
Verifying : autogen-libopts-5.18-5.el7.x86_64 3/3
Installed:
ntp.x86_64 0:4.2.6p5-25.el7.centos.2
Dependency Installed:
autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-25.el7.centos.2
Complete!
4.查看服务器时区
[root@master ~]# date -R
Wed, 20 Sep 2017 19:12:49 +0800
如果时区不正确,同步的时间也是不对的,先修改时区:
[root@master ~]# 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
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Wed Sep 20 19:14:23 CST 2017.
Universal Time is now: Wed Sep 20 11:14:23 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
添加环境变量并使之生效:
[root@master ~]# source /etc/profile
[root@master ~]# echo $TZ
Asia/Shanghai
[root@master ~]#
时区配置完成
5.时间同步
[root@master ~]# ntpdate -u 202.120.2.101
20 Sep 19:19:10 ntpdate[30211]: adjust time server 202.120.2.101 offset 0.026881 sec
[root@master ~]# date #时间正常
Wed Sep 20 19:19:34 CST 2017
[root@master ~]#
6.其他服务器同步
其他节点可以同步改服务器时间,以这个服务器作为自己局域网时间服务器,可以写个crontab 任务没分中同步一次
ntpdate -u master #master 为改主机名,可以IP, 要安装上面的安装包,时间服务相关配置在/etc/ntp.con