您好,欢迎访问这里是深圳市硕远科技有限公司!
戴尔服务器价格_IBM联想配置_浪潮代理-深圳市硕远科技有限公司
联系我们
戴尔服务器价格_IBM联想配置_浪潮代理-深圳市硕远科技有限公司
邮箱:2324898850@qq.com
电话:400-080-6079
地址:深圳市龙华区河背工业区108创业园A301
当前位置:主页 > 新闻动态 > 企业新闻 >

企业新闻

Linux 搭建NTP服务器实现自动同步时间-笔记

发布时间:2023-02-17 20:03:06浏览次数:
NTP服务概述:

1、原理

NTP(Network Time Protocol,网络时间协议)是用来使计算机时间同步化的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。

模式: C/S 模式

端口:123

2、端口

[root@opsexm ~]# vim /etc/services

扩展:

NNTP(Network News Transport Protocol),中文释义:(RFC-977)网络新闻传输协议。这是一个主要用于阅读和发布新闻文章(俗称为“帖子”,比较正式的名字是 “新闻组邮件”)到Usenet上的Internet应用协议,也负责新闻在服务器间的传送。

安装NTP

1、安装NTP服务端软件软件包

[root@opsexm Packages]# rpm -ivh /mnt/Packages/ntp-4.2.6p5-1.el6.x86_64.rpm

yum -y install ntp

安装客户端:

[root@opsexm Packages]# rpm -ivh /mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm

配置文件位置:

[root@opsexm ~]# ls /etc/ntp.conf

启动服务

2、启动NTP服务

先查看123端口是否开放:

[root@opsexm ~]# netstat -anutp | grep 123

[root@opsexm ~]# service ntpd start

Starting ntpd: [ OK ]

[root@opsexm ~]# chkconfig ntpd on

再查看:

[root@opsexm ~]# netstat -anutp | grep 123

udp 0 0 192.168.200.63:123 0.0.0.0:* 21055/ntpd

udp 0 0 127.0.0.1:123 0.0.0.0:* 21055/ntpd

客户端同步时间

ntpdate # linux时间同步命令

用法:ntp IP或ntp服务器域名。

例:

[root@opsexm ~]# vim /etc/ntp.conf #找一个可以使用NTP时间服务器

[root@opsexm ~]# ntpdate 0.rhel.pool.ntp.org

[root@opsexm ~]# ntpdate 1.rhel.pool.ntp.org

[root@opsexm ~]# ntpdate 2.rhel.pool.ntp.org

另外,已经运行了ntp服务的机器,执行同步命令会报错:

[root@opsexm ~]# ntpdate 192.168.200.63

6 Feb 19:26:41 ntpdate[3420]: the NTP socket is in use, exiting

问题分析:

出现该错误的原因是系统ntpd服务器正在运行中,通过 ps aux | grep ntpd 查看,会看到ntpd正在运行。

解决方法:

1. #service ntpd stop

修改配置文件,实战举例

搭建一个内网NTP服务器,让内网服务器通过此NTP服务器进行时间同步

ntpd服务的相关设置文件如下:

/etc/sysconfig/clock #这个是 linux 的主要时区设定文件。

每次开机后,Linux 会自动的读取这个文件来设定自己系统所默认要显示的时间。

例:

[root@opsexm ~]# cat /etc/sysconfig/clock

# The time zone of the system is defined by the contents of /etc/localtime.

# This file is only for evaluation by system-config-date, do not rely on its

# contents elsewhere.

ZONE="Asia/Shanghai"

2./etc/ntp.conf:这个是NTP服务器的主要设文件,也是 NTP 唯一的设定文件。

不显示注释,查看配置文件:

[root@opsexm ~]# grep -v "^#" /etc/ntp.conf

-v, --invert-match #反转匹配,显示不匹配的内容

下面重点说说 /etc/ntp.conf文件的设置。在 NTP Server 的设定上面,其实最好不要对 Internet 无限制的开放,尽量仅提供您自己内部的 Client 端联机进行网络校时就好。此外, NTP Server 总也是需要网络上面较为准确的主机来自行更新自己的时间,所以在我们的 NTP Server 上面也要找一部最靠近自己的 Time Server 来进行自我校正。事实上, NTP 这个服务和服务器之间,也是 C/S的一种模式。

NTP服务器实际作用:保证Linux集群中所有所有服务器时间同步。 对于一个Linux集群,集群内的机器保持时间同步是很重要的,不然会出现很多问题。

举例:配置一个本地时间同步服务器

实验前请确保此服务器可以上网

[root@opsexm ~]# vim /etc/ntp.conf #修改以下标记内容,为自己需要的内存即可

driftfile /var/lib/ntp/drift

#restrict default kod nomodify notrap nopeer noquery

#restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict -6 ::1

restrict 192.168.200.0 mask 255.255.255.0 # 允许192.168.200.0这个网段中的服务器,访问ntp服务器同步时间。

restrict 133.100.11.8 #允许单个IP地址方法本服务器。允许上游服务器133.100.11.8访问本服务器

server 133.100.11.8 #指定本NTP服务器的上游NTP服务器为133.100.11.8,并且设置为首先服务器。同步时间为,从上到下,写的越靠上,优先级越高。(写在所有server最前面IP地址为首先服务器,此服务器同步不了时间,寻找下一个NTP服务器)。此IP地址是日本福冈大学NTP服务器。 在原配置文件中添加一个外网NTP服务器

server 0.pool.ntp.org

server 0.rhel.pool.ntp.org

server 1.rhel.pool.ntp.org

server 2.rhel.pool.ntp.org

改:

#取消这两行的注释

server 127.127.1.0 # local clock 如果上面的服务器都无同步时间,就和本地系统时间同步。 127.127.1.0在这里是一个IP地址,不是网段。

fudge 127.127.1.0 stratum 10 # 127.127.1.0 为第10层。 ntp和127.127.1.0同步完后,就变成了11层。 ntp是层次阶级的。同步上层服务器的stratum 大小不能超过或等于16。

#stratum ˈstrɑ:təm

到此配置结束。其它默认即可。

ntp服务的启动与观察

在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间。有时,你的时间和NTP服务器时间相差太大,NTP会以你当前系统时间为准,不作同步。(因为启动服务器,端口会被服务端占用,就不能手动同步时间了)

[root@linux ~] # ntpdate cn.pool.ntp.org

06 Feb 19:33:51 ntpdate[8310]: step time server 80.85.129.2 offset 6.655976 sec

或:

[root@opsexm ~]# date -s "2017-2-6 20:13"

Sun Feb 6 20:13:00 CST 2017

然后,启动ntpd服务:

[root@linux ~] # service ntpd start

[root@linux ~] # /etc/init.d/ntpd start

查看端口:

[root@linux ~] # netstat -ln|grep 123

udp 0 0 192.168.228.153:123 0.0.0.0:*

udp 0 0 127.0.0.1:123 0.0.0.0:*

udp 0 0 0.0.0.0:123 0.0.0.0:*

udp 0 0 :::123 :::*

订阅一下又不会怀孕,赶紧点击订阅吧,关注技术在内的更综合的解决方案! 关注微信公众号:opslab(运维路上) 微博@运维实验室 快来调戏小编吧!码字不易,请注明出处!
400-080-6079