两台linux机器时间同步
2014-02-27 10:30
387 查看
Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time server的角色,让其他机器和他同步时间。
配置文件就是/etc/ntp.conf。
为了测试,设置让node2 -- 192.168.1.102和node1 -- 192.168.1.101做时间同步。
第一步,
node1做time server,node1本身不和其他机器时间同步,就是取本地时间。
所以,先把node1机器的时间调准了:
[root@node1 ~]date -s 08/03/2011 [root@node1 ~]date -s 11:12:00[root@node1 ~]clock -w
[root@node1 ~]hwclock --systohc
后两个命令是把设置的时间写到硬件时间中去(也就是CMOS里面的时间)。
第二步,
然后将node1配置成一个time server,修改/etc/ntp.conf,
[root@node1 ~]vi /etc/ntp.conf
其他的配置不怎么需要改,只需要关注restrict的配置:
1. 注释掉原来的restrict default ignore这一行,这一行本身是不响应任何的ntp更新请求,其实也就是禁用了本机的ntp server的功能,所以需要注释掉。
2. 加入:restrict 192.168.1.0 mask 255.255.255.0 -- 让192.168.1.0/24网段上的机器能和本机做时间同步
3. 这样就可以了,记得下面的:
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
这两行需要,这是让本机的ntpd和本地硬件时间同步。
当然,我们也可以添加server xxx.xxx.xxx.xxx,让他和其他的time server时间同步。
4. /etc/init.d/ntpd restart
5. chkconfig ntpd on
6. 修改iptables配置,将tcp和udp 123端口开放,这是ntp需要的端口,在/etc/services中可以查到这个端口。
第三步,
这样node1就成为一台time server了,现在我们配置node2这台机器,也是修改/etc/ntp.conf ,
[root@node2 ~]vi /etc/ntp.conf
1. restrict default ignore这行保留为注释状态,因为sales不需要做time server
2. 注释掉server 127.127.1.0, fudge 127.127.1.0 stratum 10这两行,因为这台机器不需要和本地硬件时钟同步了。
3. 加入server 192.168.1.101这行,和node1机器同步。
这样就OK了。看看时间,已经和node1同步了。往后默认配置好像是5分钟和time server同步一次。ntpdate命令是显式的和某台机器做时间同步,以前将ntpdate放到crontab中定期同步也是可以的,但是既然ntpd本身就可以做这个时间
第四步,将ntpdate放到crontab中定期步也是可以的
[root@node2 ~]#vi ntpupdate.sh
/usr/sbin/ntpdate 192.168.1.101
[root@node2 ~]#chmod 755 ntpupdate.sh
[root@node2 ~]#crontab -e
*/1 * * * * /root/ntpupdate.sh
[root@node2 ~]#/etc/init.d/crond restart
来源: <http://blog.chinaunix.net/uid-12115233-id-1995280.html>
来自为知笔记(Wiz)
配置文件就是/etc/ntp.conf。
为了测试,设置让node2 -- 192.168.1.102和node1 -- 192.168.1.101做时间同步。
第一步,
node1做time server,node1本身不和其他机器时间同步,就是取本地时间。
所以,先把node1机器的时间调准了:
[root@node1 ~]date -s 08/03/2011 [root@node1 ~]date -s 11:12:00[root@node1 ~]clock -w
[root@node1 ~]hwclock --systohc
后两个命令是把设置的时间写到硬件时间中去(也就是CMOS里面的时间)。
第二步,
然后将node1配置成一个time server,修改/etc/ntp.conf,
[root@node1 ~]vi /etc/ntp.conf
其他的配置不怎么需要改,只需要关注restrict的配置:
1. 注释掉原来的restrict default ignore这一行,这一行本身是不响应任何的ntp更新请求,其实也就是禁用了本机的ntp server的功能,所以需要注释掉。
2. 加入:restrict 192.168.1.0 mask 255.255.255.0 -- 让192.168.1.0/24网段上的机器能和本机做时间同步
3. 这样就可以了,记得下面的:
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
这两行需要,这是让本机的ntpd和本地硬件时间同步。
当然,我们也可以添加server xxx.xxx.xxx.xxx,让他和其他的time server时间同步。
4. /etc/init.d/ntpd restart
5. chkconfig ntpd on
6. 修改iptables配置,将tcp和udp 123端口开放,这是ntp需要的端口,在/etc/services中可以查到这个端口。
第三步,
这样node1就成为一台time server了,现在我们配置node2这台机器,也是修改/etc/ntp.conf ,
[root@node2 ~]vi /etc/ntp.conf
1. restrict default ignore这行保留为注释状态,因为sales不需要做time server
2. 注释掉server 127.127.1.0, fudge 127.127.1.0 stratum 10这两行,因为这台机器不需要和本地硬件时钟同步了。
3. 加入server 192.168.1.101这行,和node1机器同步。
这样就OK了。看看时间,已经和node1同步了。往后默认配置好像是5分钟和time server同步一次。ntpdate命令是显式的和某台机器做时间同步,以前将ntpdate放到crontab中定期同步也是可以的,但是既然ntpd本身就可以做这个时间
第四步,将ntpdate放到crontab中定期步也是可以的
[root@node2 ~]#vi ntpupdate.sh
/usr/sbin/ntpdate 192.168.1.101
[root@node2 ~]#chmod 755 ntpupdate.sh
[root@node2 ~]#crontab -e
*/1 * * * * /root/ntpupdate.sh
[root@node2 ~]#/etc/init.d/crond restart
来源: <http://blog.chinaunix.net/uid-12115233-id-1995280.html>
来自为知笔记(Wiz)
相关文章推荐
- 两台linux机器 系统时间的同步
- 两台linux机器时间同步
- 两台linux机器时间同步 NTP 配置
- 两台linux机器时间同步
- linux 两台机器时间同步
- 两台linux机器时间同步
- 两台linux机器 系统时间的同步
- 配置Linux下的时间服务器,让一批机器和一台机器时间同步
- 配置Linux下的时间服务器,让一批机器和一台机器时间同步【设置一台专门用来同步时间的服务器名为timeserver,很实用】
- 如何同步两台linux服务器的时间
- (11)Linux 服务器安装配置rsync服务,并实现两台机器同步文件
- linux下两台服务器文件实时同步方案设计和实现
- Linux 服务器时间同步
- Linux 服务器时间同步
- linux系统基本命令之系统日志和时间同步
- linux时间同步,ntpd、ntpdate
- linux时间同步命令
- Linux时间同步处理
- Linux下集群时间同步
- 修改linux系统时间与php同步问题