码迷,mamicode.com
首页 > 系统相关 > 详细

Linux NTP Server 安装配置客户端时间同步

时间:2018-01-24 00:46:08      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:linu   ble   主机名   客户端   auth   成功   过程   参数   一个   

NTP 概述:

NTP 是网络时间协议(Network Time Protocol),供客户端同步时间使用。
采用类似分级架构来处理时间的同步化,使用类似一般Server / Client的主从架构。
端口: UDP 123

国内常用NTP服务器信息:

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 四川大学网络管理中心
阿里云的NTP:
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com

ntp服务的安装

yum install ntp 

相关配置:
/etc/ntp.conf 唯一的配置文件;
/usr/share/zoneinfo/ 各时区的时间格式对应文件。例如 Asia/Shanghai 对应的文件是: /usr/share/zoneinfo/Asia/Shanghai. 此目录文件与clock 、localtime 有关系。
/etc/sysconfig/clock 设置时区与是否使用UTC时钟的配置文件。 ZONE="Asia/Shanghai"
/etc/localtime 本地的时间配置文件。

主要命令:
/bin/date: 显示与修改时间
/sbin/hwclock:用于BIOS时钟(硬件时钟)修改与显示的命令。root才能执行。Linux上BIOS时间与系统时间是分开的,使用data修改之后,还需要使用hwclock才能将修改过后的时间写入BIOS。
/usr/sbin/ntpd:ntp服务的程序,配置文件为/etc/ntp.conf
/usr/sbin/ntpdate:用于客户端时间校正,Client如果不启用ntp而仅想使用ntp Client功能的话,使用这个命令。

如何修改系统时区:
[root@msyql ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 修改时区为亚洲/上海

主要配置文件 ntp.conf

        主要配置参数:
                    restrict   [源IP]  mask  [netmask_IP ]  [parameter]

                    parameter参数 :
                                    ignore:拒绝ALL
                                    nomodify:客户端不能使用ntpc与ntpq来 修改服务器时间,但可以进行网络校时;
                                    noquery:客户端不能用ntpq、ntpc来查询时间服务器,等于不提供服务;
                                    notrap:不提供trap远程事件登陆(remote event logging)的功能;
                                    notrust:拒绝没有认证的客户端;
                                    空:该IP或网段无任何限制。

配置样例:

                            ```
                            # For more information about this file, see the man pages
                            # ntp.conf(5), ntpacc(5), ntpauth(5), ntpclock(5), ntpmisc(5), ntpmon(5).

                            #时间差异记录文件路径
                            driftfile /var/lib/ntp/drift

                            # Permit time synchronization with our time source, but do not  permit the source to query or modify the service on this system.

                            #拒绝IPV4用户
                            restrict default kod nomodify notrap nopeer noquery

                            #拒绝IPV6用户
                            restrict -6 default kod nomodify notrap nopeer noquery

                            #放行 外部NTP Server 进入
                            restrict 202.112.10.60
                            restrict 202.112.1.34
                            restrict 202.112.26.37

                            #以下2个为默认值,放行本机来源
                            restrict 127.0.0.1 
                            restrict -6 ::1

                            #放行客户端
                            restrict 192.168.1.0 mask 255.255.255.0 nomodify

                            #设置上层NTP服务器地址,prefer 为优先使用
                            server s1a.time.edu.cn prefer 
                            server s1b.time.edu.cn
                            server s2g.time.edu.cn

                            includefile /etc/ntp/crypto/pw

                            # Key file containing the keys and key identifiers used when operating
                            # with symmetric key cryptography. 
                            keys /etc/ntp/keys

                            ```*

NTP服务器启动
[root@pubsrv ~]# chkconfig ntpd on
[root@pubsrv ~]# service ntpd start
[root@pubsrv ~]# netstat -tlunp |grep ntp
udp 0 0 192.168.1.101:123 0.0.0.0: 2498/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0: 2498/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0: 2498/ntpd
udp 0 0 fe80::20c:29ff:fe02:7a7e:123 ::: 2498/ntpd
udp 0 0 ::1:123 ::: 2498/ntpd
udp 0 0 :::123 ::: 2498/ntpd

一般需要5分钟左右与上层服务器进行数据同步。

查看命令:

        ntpstat

        ntpq -p

  字段说明:

                remote:ntp主机的IP或主机名。左边的符号:  * 代表正在作用中的上层NTP,+代表连接成功,可以作为下一个时间更新的候选。
                refid:上层NTP主机的地址;
                st:stratum阶层;
                when:几秒钟之前做过数据同步的操作;
                poll:下一次更新在多少秒之后;
                reach:已经向上层NTP请求的次数;
                delay:网络传输过程中的延迟时间,单位为10的负6次方秒;
                offset:时间补偿的结果,单位为10的负3次方秒;
                jitter:系统时间与BIOS时间的差异,单位为10的负6次方秒。

防火墙端口开放:

         iptables -A INPUT -i $EXTIF -p -udp -s 192.168.1.0/24 --dport 123 -j ACCEPT
         /etc/init.d/iptables save

Linux Client

        计划任务自动校时:
             echo "0 1 * * * root (/usr/sbin/ntpdate 192.168.1.101 && /sbin/hwclock -w) &>/dev/null" >>/etc/crontab

        利用脚本自动安装ntp 并设置ntp服务器为上层时间服务器:

                                #!/bin/bash 
                                # ntp service config 
                                #时间同步
                                        ntpdate 192.168.1.101 
                                #ntp 服务安装
                                        yum install ntp -y
                                #时区配置   /etc/sysconfig/clock, 默认为 +8, Asia/Shanghai
                                    sed -i ‘s#ZONE="*.*"#ZONE="Asia/Shanghai"#‘ /etc/sysconfig/clock     

                            #/etc/localtime ,时区文件替换
                                    \cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

                        # ntp服务器配置文件写入
                                    mv /etc/ntp.conf /etc/ntp.conf.bak

                                cat >/etc/ntp.conf <<ENDF
                                # For more information about this file, see the man pages
                                # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

                                driftfile /var/lib/ntp/drift

                        restrict default kod nomodify notrap nopeer noquery
                            restrict -6 default kod nomodify notrap nopeer noquery

                            restrict 192.168.1.101
                            restrict 127.0.0.1 
                            restrict -6 ::1

                            server 192.168.1.101

                            includefile /etc/ntp/crypto/pw

                            keys /etc/ntp/keys

                            ENDF

                            chkconfig ntpd on
                            /etc/init.d/ntpd start  && netstat -ntulp  

    要让NTP Server / Client能够正常工作需要注意:
         1、能够正常连接NTP Server ;
             2、与NTP Server的时间不能差异太多;
             3、注意端口(UDP 123)的开放;

Linux NTP Server 安装配置客户端时间同步

标签:linu   ble   主机名   客户端   auth   成功   过程   参数   一个   

原文地址:http://blog.51cto.com/66585/2064382

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!