如何在 CentOS 中搭建 NTP 服务器

如何在 CentOS 中搭建 NTP 服务器,第1张

第一步、通过yum安装ntp

# yum install ntp

第二步、配置NTP服务器

编辑vim /etc/ntpconf

server 192168110 (centos7IP地址)

server s2mtimeeducn 

server 0asiapoolntporg 

server 1asiapoolntporg 

server 2asiapoolntporg 

server 3asiapoolntporg

server 12712710 iburst  local clock 当外部时间不可用时,使用本地时间。 

restrict 19216811 mask 2552552550 nomodify  允许更新的IP地址段

第三步、启动NTP

systemctl start ntpd 

systemctl enable ntpdservice 设置开机启动服务  

第四步、验证NTP

# ntpq -p

配置客户端

1:linux

NTP 客户端主机需要 ntpupdate 软件包来和服务器同步时间。可以轻松地使用 yum 安装。

# ntpdate

具体可以参考网页链接

首先,需要在centos7上面配置网站所需要的环境。

如果说你不会配置网站环境,那可以安装webmin、kloxo、cp等面板来使用。

因为这样的面板在配置好之后都是配置好网站所需要的阿帕奇、mysql等环境的。

一、搭建时间服务器

1、在一台linux服务器安装ntp server

tar zxvf ntp-426targz

cd ntp-426

/configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks

make && make install

2、修改ntpconf配置文件

vi /etc/ntpconf

# 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 default nomodify

(允许任何IP的客户机都可以进行时间同步,如果是只允许某个网段的客户机进行时间同步可以这样写

restrict 1058260 mask 2552552550 nomodify)

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 127001

restrict -6 ::1

# Hosts on local network are less restricted

#restrict 19216810 mask 2552552550 nomodify notrap

# Use public servers from the poolntporg project

# Please consider joining the pool

#server 0rhelpoolntporg(默认时间服务器)

#server 1rhelpoolntporg(默认时间服务器)

#server 2rhelpoolntporg(默认时间服务器)

server 101281425 (手工设置的时间服务器)

(如果是可以直连外网,可以使用LINUX默认提供的三组标准时间服务器,否则可以自己指定一个同步时间源)

#broadcast 1921681255 key 42 # broadcast server

#broadcastclient # broadcast client

#broadcast 224011 key 42 # multicast server

#multicastclient 224011 # multicast client

#manycastserver 239255254254 # manycast server

#manycastclient 239255254254 key 42 # manycast client

# Undisciplined Local Clock This is a fake driver intended for backup

# and when no outside source of synchronized time is available

server 12712710 # local clock

fudge 12712710 stratum 10

3、以守护进程启动ntpd

#/etc/rcd/initd/ntpd -c /etc/ntpconf -p /tmp/ntpdpid

#/etc/rcd/initd/ntpd start

#ps -ef|grep ntpd

4、在ntp server上启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令进行同步时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

# watch ntpq -p

出现如下画面:

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒5=320秒的时间。

二、配置时间同步客户机

vi /var/spool/cron/root(或crontab -e)

增加一行,在每天的1点10分、9点10分、17点10分与时间同步服务器进行同步并写入BIOS

10 1 ,9,17 root /usr/sbin/ntpdate 101281425; /sbin/hwclock -w

如果同步不正常,可以加输出日志或看系统日志

输出日志的方法:

10 1 ,9,17 root /usr/sbin/ntpdate 101281425>>/tmp/1txt; /sbin/hwclock -w

在1txt中可查看时间同步时的输出结果。

或者看/var/mail/root系统日志

Subject: Cron <root@tyzssq8> /usr/sbin/ntpdate 101281425;/sbin/hwclock -w

X-Cron-Env: <SHELL=/bin/sh>

X-Cron-Env: <HOME=/root>

X-Cron-Env: <PATH=/usr/bin:/bin>

X-Cron-Env: <LOGNAME=root>

X-Cron-Env: <USER=root>

Message-Id: <20121127103001076FF2090E@tyzssq8site>

Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)

27 Nov 18:29:59 ntpdate[6917]: step time server 101281425 offset -1361968 sec

可以看到同步成功了,如果未成功会报出错误。

三、无法同步的问题

检查ntp server主机的防火墙。可能是ntp server的防火墙屏蔽了upd 123端口。

可以用命令

#service iptables stop

直接输入命令 yum install httpd -y

我们来解析一下这个命令,yum是yellowdog updater modified的缩写。它使用中心仓库(repository)管理应用程序之间的相互关系,根据计算出来的软件依赖关系对软件进行升级、安装、删除等操作,减少了CentOS用户一直头痛的依赖关系的问题。一般这类软件通过一个或者多个配置文件描述的仓库的网络地址,通过http或者ftp协议在需要的时候从仓库获取软件信息,下载相关的软件包。这样,本地用户通过建立不同仓库描述说明,在有网络连接时就能方便的进行系统的升级维护工作。

二,配置防火墙

回到我们的教程,安装完成后如果您的系统自带了防火墙功能,那么就需要您执行如下命令 /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

那我们再来解析一下这个命令,-I INPUT所指的是允许数据传入服务器,-p tcp是指允许tcp协议,而后面的dport 80 -j ACCEPT应该很容易理解。整条命令的意思就是允许用户通过80端口的tcp协议传入连接,以达到访问页面的效果。

三,启动服务

接着,我们重启防火墙以使我们刚添加的防火墙规则生效 service iptables restart

然后接着启动apache服务 service httpd start

同样,我不厌其烦的再解析一下service指令,service的英文意思为服务(不是你们想的那个服务),而这个指令的作用就是负责管理CentOS上你安装的或者系统自带的服务。而上面的两条指令如果要解释就是 服务iptables重启 服务httpd开启。多么简单对不对。

接着你就可以将你写好的网页传到目录/var/www/html下了。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » 如何在 CentOS 中搭建 NTP 服务器

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情