一、 ntp源码安装
因为安全需求的原因,公司要求使用ntp keys认证以防止被用来做反射攻击。需要注意的是,一般系统默认自带的版本会比较老,都存在安全漏洞的,如果OS官方没有推出更新patch的,可以使用源码包编译。具体操作如下:
yum -y remove ntp ntpdate wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p12.tar.gz【会很慢】 yum -y install gcc gcc-c++ openssl-devel libstdc++* libcap* tar -zxvf ntp-4.2.8p12.tar.gz cd ntp-4.2.8p12/ ./configure --prefix=/usr --bindir=/usr/sbin --enable-all-clocks --enable-parse-clocks --docdir=/usr/share/doc/ntp-4.2.8p12 make && make install ln -s /usr/local/ntp-4.2.8p12 /usr/local/ntp
二、防火墙配置
rhel6和rhel7会有不同,主要是firewalld与iptables的不同,操作命令如下:
/sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT (同时需要/etc/sysconfig/iptables中配置) iptables -nL 查询 firewall-cmd --zone=public --add-port=123/udp --permanent firewall-cmd --reload firewall-cmd --query-port=123/udp firewall-cmd --zone=public --list-ports
三、key配置
生成的MD5 keys会在家目录下,通常命名为ntpkey_MD5key_+主机名+随机数字
[root@361way.com ~]# ntp-keygen -M [root@361way.com ~]# cp ntpkey_md5_361way.com /etc/ntp/keys [root@361way.com ~]# chmod 600 /etc/ntp/keys
第一列是ID号,后面会用到,一条一个ID号。
如果使用的不是ntp,而是chronyd,也可以使用chrond自带的工具进行key生成。命令如下:
chronyc keygen 3 SHA1
上面的3是id号,可以使用任一数字,后面的sha1是加密算法,也可以使用md5。chronyc生成的ntp-keygen生成的会有不同,ntp-keygen生成的直接是文件,多条,而chronyc根据id只会生成一条,而且不输出到文件,只是在屏幕上打印。chronyc的使用用例如下:
# generate key id #1 with a 160-bit SHA1 key $ chronyc keygen 1 SHA1 HEX:57545218761536EE5FCBCEF67D9F720DE462FB4B # generate key id #3 with a 160-bit SHA1 key $ chronyc keygen 3 SHA1 3 SHA1 HEX:C47254E85E4FBE1FD2D01FE3BFEA742B32CFB5A2 # generate key id #16 with a 128-bit SHA256key $ chronyc keygen 16 SHA256 128 16 SHA256 HEX:9001F203D6333523E320864C04259B20 # generate key id #27 with a 512-bit SHA512 $ chronyc keygen 27 SHA512 512 27 SHA512 HEX:B08BAAB8DED064D2C2351ED8C9EE5AABE784C80482809C5329187A2BE9D80A0B1E6E18C4164946F6D8E36F1C4A2A966B3B754B1FDE89A0E66FE92CC1E65364E5
四、ntp.conf配置
配置文件内容如下:
# grep -v ^# /etc/ntp.conf|grep -v ^$ driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery notrust restrict -6 default kod nomodify notrap nopeer noquery notrust restrict 127.0.0.1 restrict -6 ::1 restrict 10.211.57.78 restrict ntp.aliyun.com includefile /etc/ntp/crypto/pw keys /etc/ntp/keys trustedkey 1 2 3 4 5 6 7 8 9 10 requestkey 8 controlkey 8 server ntp.aliyun.com iburst server 10.211.57.78 burst iburst
权限的设定主要以 restrict 这个参数来设定,主要的语法为:
restrict IP地址 mask 子网掩码 参数
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP。配置keys认证关键在于notrust参数,该参数的意思是只有trust的才可以访问该ntp服务。如果不加该参数就是表示既允许keys认证的访问,也允许不加keys的访问。
ignore :关闭所有的 NTP 联机服务 nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。 notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网 noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器 notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。 nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟 kod : 访问违规时发送 KoD 包。 restrict -6 表示IPV6地址的权限设置
这里还有一个问题点就是要,如果上面restrict default两条配置并增加notrust后,发现无法和上级对时服务器进行对时了,使用ntpq -p查询,会发现如下情况。
解决该问题比较简单,还是使用restrict参数,放行上级对时服务器,因为这个参数实际上是双向控制,不但对下级进行控制,对上级对时也有控制,试想你默认所有的都不信任的不通过、且nomodify,你和上级对时服务器怎么通信,怎么对时?
restrict ntp.aliyun.com
可以看上面我的配置文件中,加了下面这样的行。再重启ntp对时服务就好了,这是网上很多资源不对的地方,网上的东西大多是搬运工,不加思索的照搬。restrict 的用法这里给几个示例:
#掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间 restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap #此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。 restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery #设置默认策略为允许任何主机进行时间同步 restrict default ignore
另外还要确保自身有足够的权限,如下两条:
restrict 127.0.0.1 restrict -6 ::1
另外还有一个文件/etc/ntp/stpe-tickers会配置上级服务器的地址,这个我觉得是和ntp.conf中的server的配置作用是相同的,这个一般不做修改。在使用server配置中还会遇到server 192.168.7.49 prefer这样的配置文件,其中prefer参数是指定那台上级对时服务器优先级最高。
如果需要同步时间的时候,也把硬件时钟进行同步,可以配置/etc/sysconfig/ntpd文件,在该文件中增加SYNC_HWCLOCK=yes行。
五、客户端配置
可以先使用ntpdate进行验证:
[root@itohost ntp]# ntpdate 10.212.149.204 03 Mar 10:41:13 ntpdate[9964]: no server suitable for synchronization found [root@itohost ntp]# ntpdate -a 4 -k /etc/ntp/keys 10.212.149.204 03 Mar 10:42:52 ntpdate[10156]: adjust time server 10.212.149.204 offset -0.003181 sec
上面可以看出,指定了key的情况下可以进行同步,不指定key的无法进行同步。同样进行ntp.conf配置如下:
[root@itohost ~]# cat /etc/ntp.conf |grep -v ^#|grep -v ^$ driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 restrict -6 ::1 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys trustedkey 4 server 10.212.149.204 key 4
这里的keys文件可以是从ntp服务端复制过来的,也可以只写一条记录,比如上面我的是id为4的key进行同步的,里面只写一条id为4的记录也是可以的。
[root@itohost ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *10.212.149.204 10.211.57.78 4 u - 64 1 1.462 -1.666 0.000
上面的输出结果如下:
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先 refid - 参考上一层ntp主机地址 st - stratum阶层 when - 多少秒前曾经同步过时间 poll - 下次更新在多少秒后 reach - 已经向上层ntp服务器要求更新的次数 delay - 网络延迟 offset - 时间补偿 jitter - 系统时间与bios时间差
同样,和上层服务器的连接也可以使用ntpstat命令查看。
六、有关chrony
1、ntp.conf与 chrony.conf配置
chrony和ntp的配置方法基本是一样的,只不过chrony对时的效率而好,而且提供的指令在交互式和查询上更精度。chrony服务对应的配置文件为:/etc/chrony.conf ,这里的配置方法和ntp.conf基本可以直接互用。chrony.conf的访问控制相对于ntp比较好用一些,使用的方式如下:
allow 192.168.2 allow 10.0.0.0/8
表示允许192.168.2.0/24和10.0.0.0/8两个字网进行访问。
2、chronyc指令
服务启动使用指令是chronyd查询和交互相关的指令主要就是chronyc,具体如下:
$ chronyc activity 200 OK 8 sources online //对时服务器个数 0 sources offline 0 sources doing burst (return to online) 0 sources doing burst (return to offline) 0 sources with unknown address $ chronyc tracking Reference ID : AB42617E (srcf-ntp.stanford.edu) Stratum : 2 Ref time (UTC) : Thu Apr 05 18:27:33 2018 System time : 0.000669840 seconds slow of NTP time Last offset : -0.000506939 seconds RMS offset : 0.001261410 seconds Frequency : 28.552 ppm slow Residual freq : -0.000 ppm Skew : 88.846 ppm Root delay : 0.031207338 seconds Root dispersion : 0.001206590 seconds Update interval : 65.2 seconds Leap status : Normal $ chronyc sources -v 210 Number of sources = 8 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ chilipepper.canonical.com 2 7 377 39 +1245us[+1245us] +/- 102ms ^+ pugot.canonical.com 2 6 377 37 -554us[ -554us] +/- 99ms ^+ golem.canonical.com 2 6 377 39 -1135us[-1135us] +/- 98ms ^+ alphyn.canonical.com 2 6 377 44 -87us[ +62us] +/- 74ms ^* clock.xmission.com 2 6 377 41 -4641ns[ +144us] +/- 41ms ^+ duffman.whackertech.com 2 6 377 40 -14ms[ -14ms] +/- 89ms ^+ palpatine.steven-mcdonal> 2 6 177 40 +2565us[+2565us] +/- 46ms ^+ paladin.latt.net 2 7 377 106 -746us[ -573us] +/- 41ms $ chronyc sourcestats -v 210 Number of sources = 8 .- Number of sample points in measurement set. / .- Number of residual runs with same sign. | / .- Length of measurement set (time). | | / .- Est. clock freq error (ppm). | | | / .- Est. error in freq. | | | | / .- Est. offset. | | | | | | On the -. | | | | | | samples. \ | | | | | | | Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== chilipepper.canonical.com 19 9 1170 +1.015 2.938 +166us 1095us pugot.canonical.com 7 4 456 -0.639 11.812 +1362us 710us golem.canonical.com 9 4 519 -0.315 7.244 +1891us 693us alphyn.canonical.com 9 4 518 -0.919 9.460 +1134us 880us clock.xmission.com 6 3 390 -1.257 20.127 +1978us 823us duffman.whackertech.com 6 3 323 +2.404 25.543 -13ms 680us palpatine.steven-mcdonal> 7 3 390 -0.421 15.058 +1776us 742us paladin.latt.net 21 14 20m +1.002 2.846 +1085us 1373us $ chronyc ntpdata 91.189.94.4 //直接对时了 Remote address : 91.189.94.4 (5BBD5E04) Remote port : 123 Local address : 192.168.1.203 (C0A801CB) Leap status : Normal Version : 4 Mode : Server Stratum : 2 Poll interval : 9 (512 seconds) Precision : -23 (0.000000119 seconds) Root delay : 0.015991 seconds Root dispersion : 0.023285 seconds Reference ID : 8CCBCC4D () Reference time : Fri Apr 06 15:55:56 2018 Offset : +0.004150821 seconds Peer delay : 0.144625142 seconds Peer dispersion : 0.000000157 seconds Response time : 0.000040157 seconds Jitter asymmetry: -0.50 NTP tests : 111 111 1111 Interleaved : No Authenticated : No TX timestamping : Daemon RX timestamping : Kernel Total TX : 38 Total RX : 38 Total valid RX : 38 $ chronyc clients Hostname NTP Drop Int IntL Last Cmd Drop Int Last =============================================================================== viper.maas 390 0 0 - 0 0 0 - - localhost 0 0 - - - 1 0 - 2
认证同步还可以进行交互式操作,如下:
chronyc> authhash SHA1 chronyc> password HEX:A6CFC50C9C93AB6E5A19754C246242FC5471BCDF 200 OK
而且可以查询是否使用的keys认证:
$ chronyc ntpdata 192.168.2.12 | grep Authenticated Authenticated : Yes