我的centos改如何配置ntp同步

2024-05-13

1. 我的centos改如何配置ntp同步

一、服务器配置
1.安装ntp
yum install ntp -y
2.修改ntp配置文件
vi /etc/ntp.conf
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#server 2.centos.pool.ntp.org
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
注:服务端的配置文件内网的配置不略有不同,如果内网有很多机器,只需要配置一台外网机器服务端,然后配置内网的,安装方式都一样,只是内网的配置文件/etc/ntp.conf略有不同。
nomodify - 用户端不能更改ntp服务器的时间参数
noquery - 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap - 不提供trap远端登陆
3.启动
/etc/rc.d/init.d/ntpd start
4.设置开机启动
 chkconfig ntpd on
5.查询网络中的NTP服务器,同时显示客户端和每个服务器的关系
ntpq -p  其中,
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差
6.内网客户端配置
安装及启动方式都一样,只是server的配置不一样,把server的配置,修改成连接外网的那台内网IP
  server  192.168.1.8

我的centos改如何配置ntp同步

2. 如何在 CentOS 中设置 NTP 服务器

1
确认已经ntp程序包:# yum install ntp
2
配置时间源# vi /etc/ntp.conf server time.lib.tsinghua.edu.cn server 0.pool.ntp.org server 1.pool.ntp.org server 2.pool.ntp.org
3
配置是否为其他PC提供时间服务# vi /etc/ntp.conf restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
4
配置开机时自动运行时间服务# chkconfig ntpd on
5
启动或停止时间服务# service ntpd start # service ntpd stop # service ntpd restart
6
验证ntp服务已经运行# pgrep ntpd
7
初始同步# ntpdate -u time.lib.tsinghua.edu.cn
8
确认同步成功# ntpq -p
9
如果要提供时间服务,还必须相应的设置iptable防火墙的配置。CentOS的时间服务使用udp 123端口。

--天下数据--

3. centos怎么设置时间与另一台服务器时间同步

一,用ntpdate从时间服务器更新时间
如果你的linux系统根本没有ntpdate这个命令
yum install ntp


安装完了之后,你不要做什么配置,也不需要,直接测试一下
[root@localhost ~]# ntpdate time.nist.gov 
22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec


如果出去上面的内容说明,同步成功了。然后在crontab里面加上以下内容。
*/10 * * * * ntpdate time.nist.gov   #域名或IP  
每隔十分钟同步一次。推荐几个时间服务器。
time.nist.gov
time.nuri.net
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
二,用ntp搭建自己的时间服务器
上面我们是利用别人的时间服务器来同步时间,这些时间服务器都是比较权威的。当我们自己搭建时间服务器就不用crontab来定时去跑。
1,安装时间服务器ntp
yum install ntp  
2,配置ntp
查看复制打印?
[root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
restrict default ignore   //默认不允许修改或者查询ntp,并且不接收特殊封包
restrict 127.0.0.1        //给于本机所有权限
restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify  //给于局域网机的机器有同步时间的权限
server time.nist.gov prefer      //设置时间服务器,加prefer表示优先
server 0.asia.pool.ntp.org 
server 1.asia.pool.ntp.org 
server 2.asia.pool.ntp.org 
server  127.127.1.0     # local clock 
fudge   127.127.1.0 stratum 10 
driftfile /var/lib/ntp/drift 
keys /etc/ntp/keys

 

3,启动 ntp
[root@localhost ~]# /etc/init.d/ntpd start  
4,查看并测试
[root@localhost ~]# netstat -upnl |grep ntpd   //查看时程
[root@localhost ~]# ntpq -pn    //查看同步的服务器IP
 remote           refid      st t when poll reach   delay   offset  jitter 
============================================================================== 
 50.77.217.185   .INIT.          16 u    -   64    0    0.000    0.000   0.000 
 202.90.158.4    .INIT.          16 u    -   64    0    0.000    0.000   0.000 
 202.71.100.89   .INIT.          16 u    -   64    0    0.000    0.000   0.000 
 202.134.1.10    .INIT.          16 u    -   64    0    0.000    0.000   0.000 
*127.127.1.0     .LOCL.          10 l   18   64  377    0.000    0.000   0.001 
[root@localhost ~]# ntpstat   //同步的结果
synchronised to local net at stratum 11 
 time correct to within 12 ms 
 polling server every 512 s

 
remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。
refid:参考的上一层NTP主机的地址
st:即stratum阶层
when:几秒前曾做过时间同步更新的操作
poll:下次更新在几秒之后
reach:已经向上层NTP服务器要求更新的次数
delay:网络传输过程钟延迟的时间
offset:时间补偿的结果
jitter:Linux系统时间与BIOS硬件时间的差异时间

centos怎么设置时间与另一台服务器时间同步

4. 如何在 CentOS 中设置 NTP 服务器

1.LinuxNTP客户端主机需要ntpupdate软件包来和服务器同步时间。可以轻松地使用yum或apt-get安装这个软件包。安装完软件包之后,用服务器的IP地址运行下面的命令。#ntpdate基于RHEL和Debian的系统命令都相同。2.Windows如果你正在使用Windows,在日期和时间设置(DateandTimesettings)下查找网络时间(InternetTime)。3.Cisco设备如果你想要同步Cisco设备的时间,你可以在全局配置模式下使用下面的命令。#ntpserver来自其它厂家的支持NTP的设备有自己的用于网络时间的参数。如果你想将设备和NTP服务器同步时间,请查看设备的说明文档。

5. 如何在CentOS中搭建NTP服务器

一、搭建时间服务器
1、在一台linux服务器安装ntp server

tar zxvf ntp-4.2.6.tar.gz
cd ntp-4.2.6
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install
2、修改ntp.conf配置文件
vi  /etc/ntp.conf
# 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 10.58.26.0 mask 255.255.255.0 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 127.0.0.1 
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool 
#server 0.rhel.pool.ntp.org(默认时间服务器)
#server 1.rhel.pool.ntp.org(默认时间服务器)
#server 2.rhel.pool.ntp.org(默认时间服务器)
server 10.128.14.25  (手工设置的时间服务器)
(如果是可以直连外网,可以使用LINUX默认提供的三组标准时间服务器,否则可以自己指定一个同步时间源)

#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 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  127.127.1.0     # local clock
fudge   127.127.1.0    stratum 10

3、以守护进程启动ntpd
 #/etc/rc.d/init.d/ntpd  -c  /etc/ntp.conf  -p  /tmp/ntpd.pid
 #/etc/rc.d/init.d/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 10.128.14.25; /sbin/hwclock -w
如果同步不正常,可以加输出日志或看系统日志
输出日志的方法:
10 1 ,9,17* * *  root  /usr/sbin/ntpdate 10.128.14.25>>/tmp/1.txt; /sbin/hwclock -w
在1.txt中可查看时间同步时的输出结果。
或者看/var/mail/root系统日志
Subject: Cron  /usr/sbin/ntpdate 10.128.14.25;/sbin/hwclock -w
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
Message-Id: 
Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)
27 Nov 18:29:59 ntpdate[6917]: step time server 10.128.14.25 offset -1.361968 sec
可以看到同步成功了,如果未成功会报出错误。

三、无法同步的问题
检查ntp server主机的防火墙。可能是ntp server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop

如何在CentOS中搭建NTP服务器

6. 如何在 CentOS 中设置 NTP 服务器

1、首先,需要保证正确设置了服务器的时区。在 CentOS 中,可以使用 timedatectl 命令查看和更改服务器的时区。
# timedatectl set-timezone Australia/Adelaide
2、继续并使用 yum 安装需要的软件
# yum install ntp

3、然后会添加全球 NTP 服务器用于同步时间。
# vim /etc/ntp.conf

4、默认情况下,NTP 服务器的日志保存在 /var/log/messages。如果你希望使用自定义的日志文件,那也可以指定。
logfile /var/log/ntpd.log

5、如果你选择自定义日志文件,确保更改了它的属主和 SELinux 环境。
# chown ntp:ntp /var/log/ntpd.log# chcon -t ntpd_log_t /var/log/ntpd.log

6、现在初始化 NTP 服务并确保把它添加到了开机启动。
# systemctl restart ntp# systemctl enable ntp

7. 如何在 CentOS 中设置 NTP 服务器

一、搭建时间服务器
1、在一台linux服务器安装ntp server
tar zxvf ntp-4.2.6.tar.gz
cd ntp-4.2.6
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install
2、修改ntp.conf配置文件
vi  /etc/ntp.conf
# 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 10.58.26.0 mask 255.255.255.0 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 127.0.0.1 
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool 
#server 0.rhel.pool.ntp.org(默认时间服务器)
#server 1.rhel.pool.ntp.org(默认时间服务器)
#server 2.rhel.pool.ntp.org(默认时间服务器)
server 10.128.14.25  (手工设置的时间服务器)
(如果是可以直连外网,可以使用LINUX默认提供的三组标准时间服务器,否则可以自己指定一个同步时间源)

#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 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  127.127.1.0     # local clock
fudge   127.127.1.0    stratum 10

3、以守护进程启动ntpd
 #/etc/rc.d/init.d/ntpd  -c  /etc/ntp.conf  -p  /tmp/ntpd.pid
 #/etc/rc.d/init.d/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 10.128.14.25; /sbin/hwclock -w
如果同步不正常,可以加输出日志或看系统日志
输出日志的方法:
10 1 ,9,17* * *  root  /usr/sbin/ntpdate 10.128.14.25>>/tmp/1.txt; /sbin/hwclock -w
在1.txt中可查看时间同步时的输出结果。
或者看/var/mail/root系统日志
Subject: Cron  /usr/sbin/ntpdate 10.128.14.25;/sbin/hwclock -w
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
Message-Id: 
Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)
27 Nov 18:29:59 ntpdate[6917]: step time server 10.128.14.25 offset -1.361968 sec
可以看到同步成功了,如果未成功会报出错误。

三、无法同步的问题
检查ntp server主机的防火墙。可能是ntp server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop

如何在 CentOS 中设置 NTP 服务器

8. 如何在 CentOS 中设置 NTP 服务器

配置的环境及要求:
1.假设在192.168.0.0网段内,要以IP为192.168.0.240的Linux机器时间服务器。
2.192.168.0.240服务器能上外网,能与比较权威的公网时间服务器同步
3.同网段内的其他机器每小时自动向192.168.0.240同步时间
一、安装
yum -y install ntp ntpdate
二、配置
2.1.NTP server的主配置文件为/etc/ntp.conf 现对/etc/ntp.conf的各项进行说明
cp /etc/ntp.conf /etc/ntp.conf.bak
vim /etc/ntp.conf
2.vi /etc/ntp.conf(以下是ntp.conf文件的内容)
#设置此服务器同上层服务器做时间同步的IP地址,prefer意味着首选IP地址
#经试验,下面的几个时间服务器速度还不错(默认配置即可)
server 210.72.145.44 prefer
server 218.21.130.42
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
#记录上次的NTP server与上层NTP server联接所花费的时间
driftfile /etc/ntp/drift
#设置默认策略为允许任何主机进行时间同步
restrict default ignore
#设置允许访问此时间服务器的时间服务的IP地址
#根据自己实际情况配置
restrict 127.0.0.1
restrict 192.168.10.2  # 指定某台机器时间同步
restrict 192.168.0.0 mask 255.255.255.0 #允许192.168.0.0/254子网内主机时间同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap #允许任何主机跟进行时间同步
#指定阶层编号为10,降低其优先度。
fudge 127.127.1.1 stratum 10
#设置ntp日志的path
statsdir /var/log/ntp/
#设置ntp日志文件
logfile /var/log/ntp/ntp.log
三、维护
----添加为服务
chkconfig --level 345 ntpd on
----启动
service ntpd start
或
/etc/rc.d/init.d/ntpd start
----停止
service ntpd stop
或
/etc/rc.d/init.d/ntpd stop
----管理命令
ntpq –p    #查看本机和上层服务器的时间同步结果
ntptrace    #可以用来追踪某台时间服务器的时间对应关系
ntpdate IP  #客户端要和NTP server进行时钟同步。
/var/log/ntp/ntp.log  #查看ntp日志
----------------------------------
开启防火墙端口
iptables -A INPUT -p udp -dport 123 -j ACCEPT
/etc/init.d/iptables save
service iptables restart
四、配置客户端
测试同步:
ntpdate ip地址
设置自动同步:
echo “00 */1 * * * root /usr/sbin/ntpdate 192.168.0.240;/sbin/hwclock -w”>>/etc/crontab
每小时同NTP server进行一次时钟同步,并写入本机BIOS
最新文章
热门文章
推荐阅读