Linux时间服务器的配置
4707 点击·0 回帖
![]() | ![]() | |
![]() | Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time 的角色,让其他机器和他同步时间,配置文件就是/etc/ntp.conf。 现有4台机器,让217做时间服务器,让218、219、200与217进行同步,配置如下: 第一步: [root@eytdb etc]# rpm -qa|grep ntp --是否已经安装了这个软件包: [root@eytdb etc]# ntpstat --NTP服务状态查询 217做time server,217本身不和其他机器时间同步,就是取本地时间,先把217机器的时间调准了: [root@eytdb etc]# date --set "10/26/2011 16:05:00" [root@eytdb etc]# clock -w [root@eytdb etc]# hwclock --systohc 后两个命令是把设置的时间写到硬件时间中去(也就是CMOS里面的时间)。 第二步: 然后将217配置成一个time server,修改/etc/ntp.conf, [root@eytdb etc]# cp ntp.conf ntp-back.conf --先备份 [root@eytdb etc]# vi /etc/ntp.conf 其他的配置不怎么需要改,只需要关注restrict的配置: 1. 注释掉下面2行,意思是不响应任何的ntp更新请求,其实也就是禁用了本机的ntp server的功能,所以需要注释掉。 # restrict default kod nomodify notrap nopeer noquery --对于默认的client拒绝所有的操作 # restrict -6 default kod nomodify notrap nopeer noquery 2. 加入下面3行 -- 让x.x.x.218---200网段上的机器能和本机做时间同步 restrict x.x.x.218 mask 255.255.255.0 nomodify notrap ---x.x.x.218 为需要同步的机器IP地址 restrict x.x.x.219 mask 255.255.255.0 nomodify notrap restrict x.x.x.200 mask 255.255.255.0 nomodify notrap --说明:ignore 关闭所有的NTP服务 , nomodify 客户端不能修改时间服务器的参数,只能进行时间校对 notrap 不提供trap远程事件登陆的功能,notrust 拒绝所有通过认证的客服端, kod kod技术可以阻止Kiss Of Death 包对服务器的破坏,使用此参数将开启该功能。 nopeer 不与同一层的NTP服务器进行时间的同步 3. 把下面2行的注释去掉,这是让本机的ntpd和本地硬件时间同步: server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 当然,也可以添加server xxx.xxx.xxx.xxx,让他和其他的time server时间同步。 4. 重启服务/etc/init.d/ntpd restart 或者:启动/停止/重启动NTP服务的命令: service ntpd start |stop|restart 5. NTP服务开机自动启动的配置及检查命令: chkconfig ntpd on --在运行级别2、3、4、5上设置为自动运行 或 chkconfig --level 35 ntpd on --在运行级别3、5上设置为自动运行 (打开自动启动用“on”;去掉自动启动用“off”) # watch ntpq -p --查看 # chkconfig --list --检查 ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 ntpdate 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭 6. 修改iptables配置,将tcp和udp 123端口开放,这是ntp需要的端口,在/etc/services中可以查到这个端口。 --打开端口 [root@eytdb etc]# iptables -I INPUT -p udp --dport 123 -j ACCEPT 这样217就成为一台time server了。 可以使用下面的命令来检查NTP 服务是否启动,可以得到一个进程ID号: [root@eytdb etc]# pgrep ntpd 26957 26959 使用下面的命令检查时间服务器同步的状态: [root@eytdb etc]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *LOCAL(0) .LOCL. 10 l 46 64 377 0.000 0.000 0.000 第三步: 配置其它几台机器,也是修改/etc/ntp.conf , [root@eytweb ~]# vi /etc/ntp.conf 1. 注释掉server 127.127.1.0, fudge 127.127.1.0 stratum 10这两行,因为这台机器不需要和本地硬件时钟同步了。 2. 加入server x.x.x.217这行,和217机器同步。 配置完成,看时间已经和217同步了。往后默认配置好像是5分钟和time server同步一次。ntpdate命令是显式的和某台机器做时间同步。 [root@eytweb ~]# ntpq -p --检查与217机器是否同步 remote refid st t when poll reach delay offset jitter ============================================================================== *x.x.x.217 .INIT. 16 u 39 128 376 0.150 0.057 0.017 第四步,将ntpdate放到crontab中定期步也是可以的 [root@eytweb ~]#vi ntpupdate.sh /usr/sbin/ntpdate x.x.x.217 [root@eytweb ~]#chmod 755 ntpupdate.sh [root@eytweb ~]#crontab -e 1 * * * * /root/ntpupdate.sh --每小时的第1分钟做一次时间同步 [root@eytweb ~]#/etc/init.d/crond restart | |
![]() | ![]() |