上海锐呈电气有限公司作者
随着服务器的增加和应用对时间的依赖性,在一个大的中时间服务器应该是*的,我们应该尽力的使所有的服务器都是同一种配置,这样即有利我们配置更有利于我们维护。
[问题]
如果时间差别太大,会产生很多的问题:
1、系统时间旧于软件创建的时间,造成软件无法安装
2、系统时间不稳定,造成数据同步将出现问题
3、多台计划任务不能统一准时执行
4、对时间依赖较大的软件会出现混乱,如论坛贴子的发布等
[方案]
时间的同步这里采用NTP,NTP类似于DNS可以实现分布式的同步,避免网络延时造成的影响。服务器可以单独直接和上层公共时间服务器同步,这样的缺点时网络的延时可能造成各主机的时间并不一定统一和准确。是在内部自己建立一个对外同步的时间服务器,其它主机都跟内部这台时间服务器进行同步。
环境:
1、系统:CentOS 4 CentOS 5.2 slackware 12 slackware 11 slackware 10 (我们的主机较多、 版本较多)
2、时间服务IP:192.168.0.3 192.168.0.4
安装:
不管是服务器端还是客户端都需要安装ntp的软件。建议:像这种辅助性的服务安装建议用系统自带的,没有必要弄得很复杂。
CentOS: yum -y install ntp
Slackware: slackpkg install ntp (这个是安装有slackpkg这个包管理工具的)
当然也可以下相应的rpm和tgz包
主要配置:
ntp只有一个配置文件就是/etc/ntp.conf 还有些相关的有用的配置文件和命令在后面会有提及。
服务器端ntp.conf
| # /etc/ntp.conf: Configuration file for ntpd. # server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 1、以上为原本内定的一个内部时间资料,不需要更动他 server 1.asia.pool.ntp.org prefer server 0.asia.pool.ntp.org server 2.asia.pool.ntp.org server 3.asia.pool.ntp.org 2、上面四条为设置上层来源的四个地址,注意这个地址是各个国家和地区有不同 而且还有优劣性, 这四个地址都是亚洲地区的,可以自己在服务器上PING四个地址判断其优劣性,我的服务器 1.asia.pool.ntp.org速度快,并且给了prefer的优先级。 # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()’ing # it to the file. 3、调整时间差异的功能,保持默认值就可以了 driftfile /etc/ntp/drift #multicastclient # listen on default 224.0.1.1 broadcastdelay 0.008 # Access Controls # Keys file. If you want to diddle your server at run time, make a # keys file (mode 600 for sure) and define the key number to be # used for making requests. # PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote # systems might be able to reset your clock at will. #keys /etc/ntp/keys #trustedkey 65535 #requestkey 65535 #controlkey 65535 # Don’t serve time or stats to anyone else by default (more secure) restrict default ignore 4、以下都是访问控制 # Trust ourselves. 允许自己 restrict 127.0.0.1 # restrictions for ntp pools 限制的NTP池 restrict 0.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 1.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 2.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 3.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery # permit all the internal private network servers to query the time 允许访问的网段,对客户端的 restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap |
restrict 其中 parameter 的参数:
那如果没有任何参数的话,这表示『该 IP 或网段不受任何限制』的意思!
客户端ntp.conf
| # /etc/ntp.conf # server 192.168.0.3 server 192.168.0.4 driftfile /etc/ntp/drift restrict default ignore restrict 127.0.0.1 restrict 192.168.0.3 mask 255.255.255.255 nomodify notrap noquery restrict 192.168.0.4 mask 255.255.255.255 nomodify notrap noquery |
客户端的ntp.conf跟服务器端一样的语法 ,只是将内部设置的时间服务器做为上层伺服务器源主机。
次要配置:
1、修改 /etc/sysconfig/ntpd
| # Drop root to id ‘ntp:ntp’ by default. OPTIONS=”-u ntp:ntp -p /var/run/ntpd.pid” # Set to ‘yes’ to sync hw clock after successful ntpdate SYNC_HWCLOCK=yes 将上面的no 改为yes 这个是系统时间跟BIOD时间同步的一个设置 注意:这个文件在slackware是没有的,我也单独添加了这个文件不知道是否是正常生效了。 # Additional options for ntpdate NTPDATE_OPTIONS=”" |
2、添加启动项
如果是用系统的包安装的:
CentOS: 1)用setup 选择 system service 2) chkconfig ntpd on
Slackware: *步chmod +x /etc/rc.d/rc.ntpd 第二步添加到启动中
/etc/rc.d/rc.M
| # Start the Network Time Protocol daemon: if [ -x /etc/rc.d/rc.ntpd ]; then sh /etc/rc.d/rc.ntpd start fi |
3、查看和修改时区
查看时区
cat /etc/sysconfig/clock (slackware没有)显示的是地区
date -R 显示的是时区号
修改时区
# tzselect
# timeconfig (于RedHat Linux 和 CentOS)
3. 复制相应的时区文件,替换系统时区文件;或者创建链接文件
cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime
在中国可以使用:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
启动服务
CentOS: 1)service ntpd restart 2) /etc/init.d/ntpd restart
Slackware:1)/etc/rc.d/rc.ntpd restart
查看结果
注意:一般服务启动15分钟内才会同步时间,这个时间不是很固定所以要注意,不过还是有判断的标准的。
1、(只适用于centos redhat这种) ntpstat
| synchronised to NTP server (192.168.0.4) at stratum 4 <==同步的服务器 time correct to within 174 ms <==服务器与客户端的时间差 polling server every 1024 s <==下次同步的时间 |
这个可以很清楚的看到同步的情况,可惜slackware下没有看到这个命令
2、ntpq -p
| remote refid st t when poll reach delay offset jitter ============================================================= +192.168.0.3 61.129.66.79 3 u 328 1024 377 1.142 -62.973 0.257 *192.168.0.4 61.129.66.79 3 u 763 1024 377 1.188 22.298 3.615 |
这个 ntpq -p 可以列出目前我们的 NTP 与相关的上层 NTP 的状态,上头的几个栏位的意义为:
3、ntptrace -n 127.0.0.1
| #ntptrace -n 127.0.0.1 127.0.0.1: stratum 4, offset -0.019501, synch distance 0.298589 192.168.0.4: stratum 3, offset 0.005055, synch distance 0.200525 61.129.66.79: stratum 2, offset 0.006232, synch distance 0.123439 209.81.9.7: stratum 1, offset -0.000008, synch distance 0.000465, refid ‘GPS’ |
其它:
时间设置:
如果时间差别太大、或者在没有时间同步服务器的情况下可以用手动设置的方法
时间
1、查看时间和日期
date
2、设置时间和日期
date -s “dd/mm/yyyy hh:mm:ss”
将系统日期设定成1996年6月10日的命令
date -s
将系统时间设定成下午1点52分0秒的命令
date -s 13:52:00
3. 将当前时间和日期写入BIOS,避免重启后失效
hwclock -w
简单的客户端同步法:
如果主机不算太多的话 加一条到计划任务就可以了。
| #crontab -e 10 5 * * * root /usr/sbin/ntpdate 1.asia.pool.ntp.org && /sbin/hwclock -w |
公司名称:上海锐呈电气有限公司
上一篇:冷热冲击试验机参数
下一篇:纸箱抗压试验机有哪些方面的用途?
上海锐呈电气有限公司作者