linux设置时间服务器

周海汉 /文

2010.3.30

 

对多个linux服务器,时间保持一致是很必要的。根据精确度要求,应该有相应的时间间隔进行时间同步。如果不进行时间同步,时间久了就会差别很大,遇到问题时定位就很困难。因为多台设备的配合,log之间可能有前因后果,时间是同步事件的先后的重要依据。

 

一般来说,对一个机房内的设备,可以设置一台时间服务器,由它定期从一个标准的时间服务器上获取时间。其他的服务器可以通过内网的连接从这台服务器进行同步。这样不仅时间会一致,而且照顾到一些没有公网的设备。

 

本文测试系统:

[root@test ~]# cat /etc/*release
CentOS release 5.2 (Final)

 

[root@test ~]# rpm -qf /usr/sbin/ntpd
ntp-4.2.2p1-8.el5.centos.1

在安装时应该确定ntp包已经安装。

 

启动服务器

如果ntpd已经安装,则可以直接启动:

[root@test ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

 

同时,也需要检查一下配置文件,centos缺省都配置好了。

[root@test ~]# vi /etc/ntp.conf

server 0.centos.pool.ntp.org

driftfile /var/lib/ntp/drift

keys /etc/ntp/keys

 

检查一下时间服务器是否可用:

[root@test ~]# ping 0.centos.pool.ntp.org
PING 0.centos.pool.ntp.org (74.88.39.232) 56(84) bytes of data.
64 bytes from ool-4a5827e8.dyn.optonline.net (74.88.39.232): icmp_seq=1 ttl=54 time=251 ms
如果不可用,则确定一下网络是否能连接到外网。检查一下域名解析是否配置。

[root@test ~]# cat /etc/resolv.conf
nameserver 8.8.8.8

 

设置ntpd自启动

[root@test ~]# find  /etc/rc.d/ -name "*ntpd"
/etc/rc.d/rc6.d/K74ntpd
/etc/rc.d/init.d/ntpd
/etc/rc.d/rc3.d/K74ntpd
/etc/rc.d/rc4.d/K74ntpd
/etc/rc.d/rc5.d/K74ntpd
/etc/rc.d/rc2.d/K74ntpd
/etc/rc.d/rc1.d/K74ntpd
/etc/rc.d/rc0.d/K74ntpd
[root@test ~]# /sbin/chkconfig --level 345 ntpd on
[root@test ~]# !find
find  /etc/rc.d/ -name "*ntpd"
/etc/rc.d/rc6.d/K74ntpd
/etc/rc.d/init.d/ntpd
/etc/rc.d/rc3.d/S58ntpd
/etc/rc.d/rc4.d/S58ntpd
/etc/rc.d/rc5.d/S58ntpd
/etc/rc.d/rc2.d/K74ntpd
/etc/rc.d/rc1.d/K74ntpd
/etc/rc.d/rc0.d/K74ntpd
说明在3,4,5三个级别已经可以自启动。

 

检查防火墙

[root@test ~]# iptables -L

对比较严格的防火墙,应该对ntp端口123进行配置:

[root@test ~]# iptables -A INPUT -p udp --dport 123 -j ACCEPT
[root@test ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ntp

 

客户端配置

客户端采用ntpdate来更新,配置在crontab中。根据需要决定频率。在每一台需要同步时间的设备上设置crontab

[root@test1 ~]# crontab -e

 

00 00 * * * /usr/sbin/ntpdate 192.168.12.31

 

192.168.12.31是test服务器的内网地址。

crontab 设置的是每天0点同步时间。

为了保证时间服务器可用,将命令先在命令行下执行一下。

[root@test1 ~]# ntpdate 192.168.12.31
30 Mar 17:45:24 ntpdate[16495]: step time server 192.168.12.31 offset 0.694312 sec
[root@test1 ~]# date
Tue Mar 30 17:45:37 CST 2010

说明同步时间成功。

Linux系统中设置时间服务器通常用于同步系统的日期和时间,以保持其准确无误。以下是设置时间服务器的基本步骤: 1. **安装ntp软件**:首先,你需要确保已经安装了NTP(Network Time Protocol)客户端工具,可以使用包管理器来安装,如 apt-get 对于基于 Debian 的系统(如 Ubuntu),或者 yum 对于基于 Red Hat 的系统(如 CentOS)。 ``` sudo apt-get install ntpdate ntp 或 sudo yum install ntp ``` 2. **配置ntp服务**:编辑 NTP 服务的配置文件,通常是 `/etc/ntp.conf`。添加你想要的时间服务器地址到该文件中,例如: ``` server time.nist.gov iburst server 0.pool.ntp.org prefer server 1.pool.ntp.org prefer server 2.pool.ntp.org prefer server 3.pool.ntp.org prefer ``` 这里 `time.nist.gov` 是一个常见的全球时间服务器,`pool.ntp.org` 是一个公开可用的NTP服务器池。 3. **启动并启用服务**:更新配置后,你可以通过以下命令启动 NTP 服务,并使其开机自启: ``` sudo service ntp start sudo systemctl enable ntp ``` 4. **检查时间和同步状态**:确认NTP是否正在运行并且成功同步时间,可以使用 `ntpq -p` 命令查看当前的同步情况,或者 `date` 查看当前系统时间。 5. **定期自动同步**:如果你希望定时同步时间,可以在 `/etc/cron.d/ntp` 文件中设置cron job,例如每隔8小时同步一次: ```bash */8 * * * * root ntpdate time.server.address ``` 完成以上设置后,你的Linux系统应该会从指定的时间服务器自动获取最新的时间信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值