ntp服务器搭建与客户端设置

服务器对时间要求非常严格,linux服务器文件的生成,日志,以及数据库的连接都是根据时间的逻辑

进行任务处理,如果时间不同步,那么,处理的任务,以及出问题的分析日志,时间不对,很难分析

直接更改linux服务器时间,影响特别大,大家都知道,服务器群启动是有规律的,如果随便更改linux服务器时间

很有可能造成时间逻辑大站,造成其它模块报警,以及数据库报警等,所以建服务器之前,先配置ntp服务器,

部分服务器为了安全不能上网,所以不能同步网络时间,所以我们让它同步我局域网的ntp服务器,以下是设置相关命令

 

[root@centos01 ~]# cat /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"

[root@centos01 ~]# ls /usr/share/zoneinfo/America/      #查看有哪些时区

[root@centos01 ~]# yum list ntp\*             #查看是否有ntp rpm包

[root@centos01 ~]# more /etc/ntp.conf                    # 查看ntp服务器配置文件,也可以修改网上ntp服务器教育网,很多

[root@centos01 ~]# chkconfig ntpd on        #启动ntp 服务

[root@centos01 ~]# netstat -ntulp |grep 123    #查看ntp服务端口号upd 123 是否启动
udp 0 0 192.168.1.7:123 0.0.0.0:* 1659/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 1659/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 1659/ntpd
udp 0 0 ::1:123 :::* 1659/ntpd
udp 0 0 fe80::20c:29ff:fe79:38bb:123 :::* 1659/ntpd
udp 0 0 :::123 :::* 1659/ntpd

[root@centos01 ~]# iptables -I INPUT 1 -p udp --dport 123 -j ACCEPT     #防火强打开upd123 端口,让其它服务器来同步时间
[root@centos01 ~]# service iptables save                    #保存防火墙配置
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:[确定]

[root@centos01 ~]# ntpq -p                    #打印现在的ntp 连接信息                  
remote refid st t when poll reach delay offset jitter
==============================================================================
*dns1.synet.edu. 202.118.1.46 2 u 106 128 257 60.571 2.864 4.041
+gus.buptnet.edu 202.112.10.60 3 u 319 128 174 64.985 10.104 36.470
+dns2.synet.edu. 202.118.1.46 2 u 130 128 267 51.183 8.805 54.378

客户端设置同步ntp服务器命令如下

-bash-4.1# ntpdate 192.168.1.7               #同步自己建的192.168.1.7 这台ntp服务器,结果报错
26 Apr 13:28:21 ntpdate[27058]: the NTP socket is in use, exiting
-bash-4.1# service ntpd stop                   #关闭客户端ntpd服务器,让他不要以ntp服务器身份同步,让它以ntp客户端身份同步你192.168.1.7 ntp服务器
Shutting down ntpd: [ OK ]
-bash-4.1# ntpdate 192.168.1.7              #同步时间,ntp服务器为192.168.1.7 现在同步,返回值表示同步成功
26 Apr 13:29:02 ntpdate[27076]: adjust time server 192.168.1.7 offset 0.005925 sec

-bash-4.1# vi /etc/crontab                    #加入循环执行任务里面,每天2点15分自动同步时间

15 2 * * * root /usr/sbin/ntpdate 192.168.0.20 &&/sbin/hwclock -w

 备注:国内最常见的NTP服务器:

国内常用NTP服务器地址及IP

 

[root@desk ~]# vim /etc/ntp.conf

# 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

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap   #设置哪些网段可以使用这台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
server 202.120.2.101
server s2f.time.edu.cn
server s2g.time.edu.cn
server s2k.time.edu.cn
server s2m.time.edu.cn


#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

posted @ 2015-04-26 13:53  wz0314  阅读(1661)  评论(0编辑  收藏  举报