linux ntp时间服务器及客户端安装配置


linux ntp时间服务器及客户端安装配置
 
1、主要文件及命令:
    /etc/ntp.conf 服务器配置文件
    /etc/sysconfig/clock 时区配置文件
 
    /bin/date 显示和设置时间的命令
    /sbin/hwclock 用于 BIOS 时钟 (硬件时钟) 的修改与显示的命令
    /usr/sbin/ntpd 主要提供 NTP 服务的程序
    /usr/sbin/ntpdate 用于客户端的时间校正
  www.2cto.com  
2、服务端:查软件是否已安装:
    [root@localhost ~]# rpm -qa | grep ntp
    ntp-4.2.2p1-9.el5_4.1   (这个就是已经安装的RPM包,或者其他版本的ntp)
 
    如果没有安装,可从http://support.ntp.org/bin/view/Main/SoftwareDownloads下载,安装RPM包:
    [root@localhost ~]# rpm -ivh ntp-4.2.2p1-9.el5_4.1.rpm
 
3、备份一下配置文件
    cp /etc/ntp.conf /etc/ntp.conf_bak
 
4、ntp.conf文件主要参数说明:
    4.1、利用 restrict 来管理权限控制
        restrict [你的IP] mask [netmask_IP] [parameter]
        其中 parameter 的参数主要有底下这些:
 
        ignore: 拒绝所有类型的 NTP 联机; 
        nomodify: 客户端不能使用 ntpc 与 ntpq 这两支程序来修改服务器的时间参数, 但客户端仍可透过这部主机来进行网络校时的; 
        noquery: 客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时啰; 
        notrap: 不提供 trap 这个远程事件登录 (remote event logging) 的功能。 
        notrust: 拒绝没有认证的客户端。
 
    4.2、利用 server 设定上层 NTP 服务器
        server [IP or hostname] [prefer]  
        perfer 表示‘优先使用’
 
5、具体配置如下:
    原来的上层服务器及对上层的限制注释掉,然后直接加上这几项
    server 210.72.145.44 prefer
    server time.nist.gov
    restrict 210.72.145.44 mask 255.255.255.255 nomodify notrap noquery
    restrict time.nist.gov mask 255.255.255.255 nomodify notrap noquery
    restrict 192.168.1.0 mask 255.255.255.0 nomodify  #对内网访问开放,不同网段设置要改一下
  www.2cto.com  
6、重启服务:/etc/init.d/ntpd restart
    
7、加入开机启动:chkconfig ntpd on
 
8、查看系统日志看有没有报错:tail /var/log/messages
 
9、查看启动的端口:netstat -tlunp | grep ntp
 
10、检查与上层服务器是否同步成功(约15分钟后才会和上层连接):
    ntpstat 或 ntpq -p
 
11、先对服务器进行手动较正,要不误差超过1000秒的话服务会自动停掉:
    date MMDDHHMMCCYY.SS  #设置时间,格式:月月日日时时分分年年.秒秒 
    /etc/init.d/ntpd stop #要把服务停掉,因为ntp与ntpdate不能同时生效
    ntpdate 210.72.145.44
    hwclock -w  #写入硬件时间,-r是显示硬件时间
    /etc/init.d/ntpd restart
  www.2cto.com  
12、别的客户端要进行同步,则这样处理:
    vim /etc/crontab
    */10 * * * * root (/usr/sbin/ntpdate 210.72.145.44 && /sbin/hwclock -w) &> /dev/null
    
    /etc/init.d/crond restart
 
13、要点:
    Linux 系统本来就有两种时间,一种是 Linux 以 1970/01/01 开始计数的系统时间,一种则是 BIOS 记载的硬件时间; 
    Linux 可以透过网络校时,最常见的网络校时为使用 NTP 服务器,这个服务启动在 udp port 123; 
    时区档案主要放置于 /usr/share/zoneinfo/ 目录下,而本地时区则参考 /etc/localtime; 
    NTP 服务器为一种阶层式的服务,所以 NTP 服务器本来就会与上层时间服务器作时间的同步化, 因此 nptd 与 ntpdate 两个指令不可同时使用; 
    NTP 服务器的联机状态可以使用 ntpstat 及 ntpq -p 来查询; 
    NTP 提供的客户端软件为 ntpdate 这个指令; 
    在 Linux 下想要手动处理时间时,需以 date 设定时间后,以 hwclock -w 来写入 BIOS 所记录的时间。 
    NTP 服务器之间的时间误差不可超过 1000 秒,否则 NTP 服务会自动关闭。

相关内容

    暂无相关文章