ntp时间服务器

一、基础知识

1、linux系统时间设置

date        #查看或者设置当前系统的时间
例如: date -s "2020-01-03 14:17:00"
ntpdate    #手工临时同步系统时间
ntpd        #作为守护进程,按照一定的算法进行时间同步,即使你启动了该进程,ntpd也不会立刻进行时间同步
Linux里面用Asia/Shanghai表示东八区,等价于Windows中的 北京、重庆、香港特别行政区,乌鲁木齐的时区设置。
如果安装时选错了时区,或者使用了默认的纽约时区,可以通过以下命令进行修改:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2、linux硬件系统设置

 hwclock --show   #查看硬件时间
 hwclock --set --date="1/25/09 00:00" <== 月/日/年时:分:秒     #设置硬件时间

3、系统时间与硬件时间同步

#以系统时间为基准,修改硬件时间
hwclock --systohc<== sys(系统时间)to(写到)hc(Hard Clock)
hwclock -w

#以硬件时间为基准,修改系统时间
hwclock --hctosys
hwclock -s

二、安装ntp服务

ntp-server IP地址11.11.11.90 为例

yum install ntp

[root@localhost ~]# rpm -qa|grep ntp
ntp-4.2.6p5-29.el7.centos.x86_64
ntpdate-4.2.6p5-29.el7.centos.x86_64

systemctl start ntpd.service
systemctl enable ntpd.service
ntpdate time1.aliyun.com  #手动同步互联网时间服务器

三、编辑ntp配置文件

cp /etc/ntp.conf{,.bak}

[root@localhost ~]# egrep -v "^$|#" /etc/ntp.conf
driftfile /var/lib/ntp/drift    
restrict 11.11.11.90 nomodify notrap nopeer noquery  #当前节点IP地址,需要修改的地方********
restrict 127.0.0.1 
restrict ::1
restrict 11.11.11.0 mask 255.255.255.0 nomodify notrap  #允许11.11.11.0/24网段的所有主机使用该时间服务器进行时间同步,需要修改的地方******
server 127.127.1.0 prefer  #以本机作为时间服务器,需要新增的地方*****
fudge 127.127.1.0 stratum 10    #设置服务器层级,需要新增的地方******
driftfile /var/lib/ntp/         #记录当前时间服务器,与上游服务器的时间差的文件,需要新增的地方******
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

注释:如果时间服务器可以连接互联网,可以把server 部分换成互联网时间服务器。

#例如:
server cn.pool.ntp.org prefer
server asia.pool.npt.org

四、启动ntp服务

systemctl stop ntpd.service
ntpdate -u cn.pool.ntp.org    #启动之前先保证本机时间与远程互联网时间同步,-u越过本机防火墙同步远程主机
systemctl start ntpd.service
systemctl enable ntpd.service
ntpq -p
ntpstat   #这个需要10分钟左右

五、其他设置

防火墙开启ntp端口:udp 123
-A INPUT -p udp -m udp --sport 123 -j ACCEPT
-A INPUT -p udp -m udp --dport 123 -j ACCEPT

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes
[root@localhost ~]# cat /etc/sysconfig/ntpd
# Command line options for ntpd
SYNC_HWCLOCK=yes
OPTIONS="-g"

 

六、客户端配置

1、第一种

*/30 * * * * /usr/sbin/ntpdate 11.11.11.90

2、第二种

1、安装ntp
# yum install ntp -y

2、打开ntp配置文件
# vim /etc/ntp.conf
添加以下内容:
  server 11.11.11.90 iburst

3、重启ntp
# systemctl restart ntpd
# systemctl enable ntpd

4、查看时间同步状态
# ntpstat         //查看时间同步状态,这个一般需要3~5分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
# ntpq -p         //查看上层时间服务器(时间服务器:11.11.11.90)
[root@localhost ~]# ntpq -p
  remote         refid         st t  when    poll   reach    delay    offset    jitter
=============================================================================================
 11.11.11.90     84.16.73.33   2 u   231    512      376      0.797    -0.026    0.095
 NTP主机  11.90参考上一层主机地址  阶层  几秒前  下次几秒后  请求次数  延迟时间  时间补偿  系统与BIOS硬件时间差
说明:
remote:如果是“
+”则代表目前正在使用中的NTP,如果是“*”则表示有连上线,不过是作为次要联机的NTP主机。

 

参看其他博文摘录:

# 1. 先处理权限方面的问题,包括放行上层服务器以及开放局域网用户来源:
restrict default kod nomodify notrap nopeer noquery     <==拒绝 IPv4 的用户
restrict -6 default kod nomodify notrap nopeer noquery  <==拒绝 IPv6 的用户
restrict 220.130.158.71   <==放行 tock.stdtime.gov.tw 进入本 NTP 的服务器
restrict 59.124.196.83    <==放行 tick.stdtime.gov.tw 进入本 NTP 的服务器
restrict 59.124.196.84    <==放行 time.stdtime.gov.tw 进入本 NTP 的服务器
restrict 127.0.0.1        <==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行局域网用户来源,或者列出单独IP

# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
server 220.130.158.71 prefer  <==以这部主机为最优先的server
server 59.124.196.83
server 59.124.196.84

# 3.默认的一个内部时钟数据,用在没有外部 NTP 服务器时,使用它为局域网用户提供服务:
# server    127.127.1.0     # local clock
# fudge     127.127.1.0 stratum 10

# 4.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
driftfile /var/lib/ntp/drift
keys      /etc/ntp/keys

 

国内常用ntp服务器地址

210.72.145.44 (国家授时中心服务器IP地址)  
133.100.11.8 日本 福冈大学  
time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland   
time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland   
time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado   
time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado   
time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado   
utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder   
time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado   
time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington   
nist1.symmetricom.com 69.25.96.13 Symmetricom, San Jose, California   
nist1-dc.glassey.com 216.200.93.8 Abovenet, Virginia   
nist1-ny.glassey.com 208.184.49.9 Abovenet, New York City   
nist1-sj.glassey.com 207.126.98.204 Abovenet, San Jose, California   
nist1.aol-ca.truetime.com 207.200.81.113 TrueTime, AOL facility, Sunnyvale, California   
nist1.aol-va.truetime.com 64.236.96.53 TrueTime, AOL facility, Virginia  
————————————————————————————————————  
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)  
s1a.time.edu.cn 北京邮电大学  
s1b.time.edu.cn 清华大学  
s1c.time.edu.cn 北京大学  
s1d.time.edu.cn 东南大学  
s1e.time.edu.cn 清华大学  
s2a.time.edu.cn 清华大学  
s2b.time.edu.cn 清华大学  
s2c.time.edu.cn 北京邮电大学  
s2d.time.edu.cn 西南地区网络中心  
s2e.time.edu.cn 西北地区网络中心  
s2f.time.edu.cn 东北地区网络中心  
s2g.time.edu.cn 华东南地区网络中心  
s2h.time.edu.cn 四川大学网络管理中心  
s2j.time.edu.cn 大连理工大学网络中心  
s2k.time.edu.cn CERNET桂林主节点  
s2m.time.edu.cn 北京大学
posted @ 2020-01-03 15:19  无敌仙人掌  阅读(391)  评论(0编辑  收藏  举报
AmazingCounters.com