ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

centos-ntp服务器设置

2021-01-06 21:00:15  阅读:168  来源: 互联网

标签:centos ntp vm etc NTP 服务器 root


生产环境慎用ntp服务器。
国内建议使用:
vim /etc/profile # 添加TZ变量环境变量 ,把获取的TZ值加入到全局变量中 添加到最末行后,source即可
TZ=‘Asia/Shanghai’; export TZ
source /etc/profile

以下是转载内容
Centos 部署服务分类的最新文章
[NTP] Linux NTP服务器搭建部署与NTP时间同步配置
[LNMP] Linux(centos6)中LNMP环境搭建,LNMP源码安装及LNMP架构原理详解
[Samba] Linux(Centos)samba服务安装,Samba文件共享及Samba配置详解
[Vsftpd] Centos 6下Vsftpd服务安装教程,Vsftpd用户及.conf配置详解
[LAMP] Linux(Centos)LAMP环境搭建,LAMP源码安装及LAMP架构原理详解
热评好文
Skype For Business 2015 综合部署系列二:AD域部署及Skype安装先决条件
[PXE] Linux(centos6)中PXE 服务器搭建,PXE安装、启动及PXE理论详解
Skype For Business 2015综合部署系列五:安装 Skype Server系统 2
Windows server 2012 部署NTP,实现成员服务器及客户端时间与域控制器时间同步
Exchange Server 2016安装部署系列二: 邮箱服务器角色安装
最新评论
[Vsftpd] Centos 6下Vsftpd服务安装教程,Vsftpd用户及.conf配置详解
1003584293:写的实在太好了,亲测成功
[LAMP] Linux(Centos)LAMP环境搭建,LAMP源码安装及LAMP架构原理详解
wx5a1e863bd0b9c:写的有些看不懂,专业
[HTTPD] Linux(Apache)Httpd服务器安装,启动及httpd.conf配置详解
wx5ca1c59746d89:大哥在不
[PXE] Linux(centos6)中PXE 服务器搭建,PXE安装、启动及PXE理论详解
Godspeedlyz:之前遇到过,换了个网口(感觉是驱动的问题),不确定你的问题也能有效
[DNS] Linux(centos6)中dns(bind)服务器安装及dns理论详解
wx5b46b47f34725:刚学习DNS现在完全看不懂了
最新文章
[Zabbix] Linux 环境zabbix 3.4邮件报警配置及zabbix故障处理
[Zabbix] Linux 环境zabbix 3.4监控安装部署及zabbix agent配置
[NTP] Linux NTP服务器搭建部署与NTP时间同步配置
[Vsftpd] Centos 6下Vsftpd服务安装教程,Vsftpd用户及.conf配置详解
[软Raid] Linux(centos 6)software raid服务器安装,配置详解
原创
[NTP] Linux NTP服务器搭建部署与NTP时间同步配置
Wish_亮关注0人评论13165人阅读2018-10-25 14:47:35

本篇blog主要介绍了[NTP] Linux 系统时区设置,NTP时间服务器部署及ntp.conf配置参数详解,并通过案例介绍了系统时区配置及时间同步的多种方法,不同Client客户端的相关配置,最后补充了ntp及ntpdate的相关联系及区别。

Linux系统时区设置

一、什么是NTP

NTP是用来使计算机时间同步化的一种协议,全称是Network Time Protocol。它可以在大规模的设备范围内同步矫正时间到几ms级别的精度,在网络稳定的局域网内,精度甚至可以达到微秒级别。 今天NTP协议已经发展到V4版本,V1版本在1988年6月推出,主要RFC为RFC-1059;今天我们大规模使用的V4版本的NTP,还没有正式的RFC出版,但是简单的协议内容,在RFC2030里已有描述。

二、如何查看系统时区

1、使用date命令查看系统时区

[root@vm ~]# date
Tue Oct 16 16:37:24 CST 2018 # CST代表了中国标准时间(+8区)

[root@vm ~]# date -R
Tue, 16 Oct 2018 16:37:27 +0800 # 内容详细的显示了+0800时区,即东八区中国标准时间
2、查看clock系统时区

[root@vm ~]# more /etc/sysconfig/clock # 查看/etc/localtime 定义的系统时区

The time zone of the system is defined by the contents of /etc/localtime.

ZONE=“Asia/Shanghai”
3、通过zdump命令查看相应时区时间

[root@vm ~]# zdump Africa/Bissau
Africa/Bissau Tue Oct 16 09:23:59 2018 GMT

[root@vm ~]# zdump Asia/Taiwan
Asia/Taiwan Tue Oct 16 09:25:46 2018 Asia
三、如何设置系统时区

1、第一种方法:使用tzselect命令设置系统时区

使用tzselect命令获取TZ值

[root@vm ~]# tzselect # 使用tzselect命令,获取TZ值
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 # 选择Asia
    Please select a country.
  12. Afghanistan 18) Israel 35) Palestine
  13. Armenia 19) Japan 36) Philippines
  14. Azerbaijan 20) Jordan 37) Qatar
  15. Bahrain 21) Kazakhstan 38) Russia
  16. Bangladesh 22) Korea (North) 39) Saudi Arabia
  17. Bhutan 23) Korea (South) 40) Singapore
  18. Brunei 24) Kuwait 41) Sri Lanka
  19. Cambodia 25) Kyrgyzstan 42) Syria
  20. China 26) Laos 43) Taiwan
  21. Cyprus 27) Lebanon 44) Tajikistan
  22. East Timor 28) Macau 45) Thailand
  23. Georgia 29) Malaysia 46) Turkmenistan
  24. Hong Kong 30) Mongolia 47) United Arab Emirates
  25. India 31) Myanmar (Burma) 48) Uzbekistan
  26. Indonesia 32) Nepal 49) Vietnam
  27. Iran 33) Oman 50) Yemen
  28. Iraq 34) Pakistan
    #? 9 # 选择China
    Please select one of the following time zone regions.
  29. Beijing Time
  30. Xinjiang Time
    #? 1 # 选择Beijing Time

The following information has been given:

China
Beijing Time

Therefore TZ=‘Asia/Shanghai’ will be used.
Local time is now: Tue Oct 16 16:46:58 CST 2018.
Universal Time is now: Tue Oct 16 08:46:58 UTC 2018.
Is the above information OK?

  1. Yes
  2. No
    #? 1 # 确认TZ值TZ=‘Asia/Shanghai’

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
在全局配置中添加TZ变量

tzselect命令只告诉你选择的时区的写法,并不会自动生效。需在用户变量/.profile、/~bash_profile或者全局变量/etc/profile中设置并导入获取的TZ变量值。

[root@vm ~]# vi /etc/profile # 添加TZ变量环境变量 ,把获取的TZ值加入到全局变量中
TZ=‘Asia/Shanghai’; export TZ
“/etc/profile” 79L, 1871C written

[root@vm ~]# source /etc/profile # 使能TZ变量生效

[root@vm ~]# date -R # 验证时区
Tue, 16 Oct 2018 17:08:05 +0800

[root@vm ~]# more /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”
2、第二种方法:复制相应的时区文件,替换系统默认时区文件或者创建链接文件,设置系统时区

复制时区文件,替换系统时区默认文件

[root@vm ~]# ls /usr/share/zoneinfo/ # 在/usr/share/zoneinfo/ 文件下有很多时区文件
Africa Asia Canada Cuba EST GB GMT-0 HST iso3166.tab Kwajalein Mexico NZ Portugal PST8PDT Singapore Universal W-SU
America Atlantic CET EET EST5EDT GB-Eire GMT+0 Iceland Israel leapseconds MST NZ-CHAT posix right Turkey US zone1970.tab
Antarctica Australia Chile Egypt Etc GMT Greenwich Indian Jamaica Libya MST7MDT Pacific posixrules ROC tzdata.zi UTC zone.tab
Arctic Brazil CST6CDT Eire Europe GMT0 Hongkong Iran Japan MET Navajo Poland PRC ROK UCT WET Zulu

[root@vm ~]# rm -rf /etc/localtime # 删除系统默认时区文件

[root@vm ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 复制时区文件到默认路径 或者
或者通过创建链接文件,/usr/share/zoneinfo/Asia/ShangHai文件链接到/etc/localtime文件

[root@vm ~]# rm -rf /etc/localtime

[root@vm ~]# ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
/etc/localtime' ->/usr/share/zoneinfo/Asia/Shanghai’
通过实验,在设置系统时区过程中,TZ变量优先级高于本地/etc/localtime文件优先级。也就是说,若TZ变量及/etc/localtime 都存在的情况下 ,TZ变量会优先生效 。若想要设置timezone(系统时区),故建议直接在全局变量配置文件中添加TZ变量即可,即使用第一种配置方式设置linux系统时区。

Linux NTP服务器部署安装及配置

一、linux NTP服务器部署安装

1、验证服务器端NTP服务是否已安装

[root@vm ~]# rpm -qa ntp*
ntpdate-4.2.6p5-12.el6.centos.2.x86_64
ntp-4.2.6p5-12.el6.centos.2.x86_64
2、若系统为安装ntp服务 ,可通过rpm或yum进行安装

[root@vm ~]# yum install ntp -y

[root@vm ~]# rpm -ql ntp
/etc/ntp.conf # ntp服务器的主配置文件
/etc/rc.d/init.d/ntpd # 开机启动ntpd脚本文件
3、国内稳定NTP时间同步服务器域名

times.aliyun.com

ntp.aliyun.com

cn.pool.ntp.org

0.cn.pool.ntp.org

1.cn.pool.ntp.org

4、使用ntpdate同步时间服务器

[root@vm ~]# ntpdate times.aliyun.com
17 Oct 10:05:27 ntpdate[26878]: step time server 120.25.115.19 offset 4.115593 sec

[root@vm ~]# ntpdate cn.pool.ntp.org
17 Oct 10:05:47 ntpdate[26908]: adjust time server 85.199.214.100 offset 0.037943 sec

[root@vm ~]# ntpdate 0.cn.pool.ntp.org
17 Oct 10:06:00 ntpdate[26915]: adjust time server 120.25.115.20 offset -0.003371 sec
二、linux NTP服务配置

1、NTP服务 ntp.conf 参数详解

默认ntp.conf内容如下

[root@vm ~]# vi /etc/ntp.conf

#系统时间与BIOS事件的偏差记录
driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery # 拒绝所有IPv4的client连接此NTP服务器
restrict -6 default kod nomodify notrap nopeer noquery # 拒绝所有IPv6的client连接此NTP服务器

restrict 127.0.0.1 # 放行本机localhost对NTP服务的访问
restrict -6 ::1

Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 放行192.168.1.0网段主机与NTP服务器进行时间同步

Use public servers from the pool.ntp.org project.

Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 0.centos.pool.ntp.org iburst # 代表的同步时间服务器
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

Key file containing the keys and key identifiers used when operating

with symmetric key cryptography.

keys /etc/ntp/keys
各项参数详解

利用restrict 来管理权限控制
Restrict [IP] mask [netmask_IP] [parameter]

Parameter 的
ignore :拒绝所有类型的NTP联机。
nomodify: 客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进行网络校时;
noquery:客户端不能够使用ntpc与ntpq等指令来查询时间服务器,不提供NTP的网络校时。
notrap:不提供trap 这个运程事件登入的功能。
notrust:拒绝没有认证的客户端。
Kod:kod技术可以阻止“Kiss of Death “包对服务器的破坏。
Nopeer:不与其他同一层的NTP服务器进行时间同步。

利用server 设定上层NTP服务器,格式如下:
server [IP or hostname] [prefer]

参数主要如下:
perfer:表示优先级最高
burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
2、配置NTPserver

调整NTP配置文件

[root@vm ~]# vi /etc/ntp.conf

Hosts on local network are less restricted.

允许局域网内设备与这台服务器进行同步时间.但是拒绝让他们修改服务器上的时间

restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

允许上层时间服务器修改本机时间

restrict times.aliyun.com nomodify
restrict ntp.aliyun.com nomodify
restrict cn.pool.ntp.org nomodify

定义要同步的时间服务器

server times.aliyun.com iburst prefer # prefer表示为优先,表示本机优先同步该服务器时间
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst

logfile /var/log/ntpstats/ntpd.log # 定义ntp日志目录
pidfile /var/run/ntp.pid # 定义pid路径

Key file containing the keys and key identifiers used when operating

with symmetric key cryptography.

keys /etc/ntp/keys
ntp服务启动

[root@vm ~]# chkconfig ntpd on

[root@vm ~]# service ntpd start
Starting ntpd: [ OK ]

[root@vm ~]# ss -tunlp | grep ntp
udp UNCONN 0 0 192.168.23.100:123 : users:((“ntpd”,24250,19))
udp UNCONN 0 0 127.0.0.1:123 : users:((“ntpd”,24250,18))
udp UNCONN 0 0 :123 : users:((“ntpd”,24250,16))
udp UNCONN 0 0 fe80::92b1:1cff:fe10:3a44:123 :::
users:((“ntpd”,24250,21))
udp UNCONN 0 0 ::1:123 ::

标签:centos,ntp,vm,etc,NTP,服务器,root
来源: https://blog.csdn.net/weixin_43186648/article/details/112297312

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有