linux中ntp时钟服务器怎么配

linux中ntp时钟服务器怎么配,第1张

ntpd -x -- 这里的这个-x代表加一个参数,x 可以是c, f, k, p, P, r, s, t, U等等中的一个字母。
详细说明,可以看一下ntpd的man page 如:
-bash-300$ man ntpd
Reformatting page Please Wait done
System Administration Commands ntpd(1M)
NAME
ntpd - Network Time Protocol daemon Version 4
SYNOPSIS
/usr/lib/inet/ntpd [-46aAbdDgLmnNqvx] [-c conffile]
[-f driftfile] [-k keyfile] [-l logfile] [-p pidfile]
[-P priority] [-r broadcastdelay] [-s statsdir]
[-t trustedkey] [-U interface_update_time]
DESCRIPTION
The ntpd program is an operating system daemon that syn-
chronises the system clock with remote NTP time servers or
local reference clocks It is a complete implementation of
the Network Time Protocol (NTP) version 4, but also retains
compatibility with version 3, as defined by RFC 1305, and
versions 1 and 2, as defined by RFC 1059 and RFC 1119,
respectively
。。。
man page中还有对各参数的详细介绍。
OPTIONS
-4, --ipv4
Force DNS resolution of following host names on the
command line to the IPv4 namespace Cannot be used with
the --ipv6 option
-6, --ipv6
Force DNS resolution of following host names on the
command line to the IPv6 namespace Cannot be used with
the --ipv6 option
-a, --authreq
Require cryptographic authentication for broadcast
client, multicast client and symmetric passive associa-
tions This is the default This option must not
appear with authnoreq option
-A, --authnoreq
Do not require cryptographic authentication for broad-
cast client, multicast client and symmetric passive
associations This is almost never a good idea This
option must not appear with the authreq option
-b, --bcastsync
Enable the client to sync to broadcast servers
SunOS 510 Last change: 5
System Administration Commands ntpd(1M)
-c string, --configfile=string
The name and path of the configuration file,
/etc/inet/ntpconf by default
-d, --debug-level
Increase output debug message level This option may
appear an unlimited number of times
-D string, --set-debug-level=string
Set the output debugging level Can be supplied multi-
ple times, but each overrides the previous value(s)
-f string, --driftfile=string
The name and path of the frequency file,
/var/ntp/ntpdrift by default
-g, --panicgate
Allow the first adjustment to exceed the panic limit
Normally, ntpd exits with a message to the system log
if the offset exceeds the panic threshold, which is
1000s by default This option allows the time to be set
to any value without restriction; however, this can
happen only once If the threshold is exceeded after
that, ntpd will exit with a message to the system log
This option can be used with the -q and -x options
See the tinker configuration file directive for other
options
-k string, --keyfile=string
Specify the name and path of the symmetric key file
/etc/inet/ntpkeys is the default
-l string, --logfile=string
Specify the name and path of the log file The default
is the system log file
-L, --novirtualips
Do not listen to virtual IPs The default is to listen
-n, --nofork
Do not fork
-N, --nice
To the extent permitted by the operating system, run
ntpd at the highest priority
-p string, --pidfile=string
Specify the name and path of the file used to record
ntpd's process ID
SunOS 510 Last change: 6
System Administration Commands ntpd(1M)
-P number, --priority=number
To the extent permitted by the operating system, run
ntpd at the specified sched_setscheduler(SCHED_FIFO)
priority
-q, --quit
Set the time and quit ntpd will exit just after the
first time the clock is set This behavior mimics that
of the ntpdate program, which is to be retired The -g
and -x options can be used with this option Note: The
kernel time discipline is disabled with this option
-r string, --propagationdelay=string
Specify the default propagation delay from the
broadcast/multicast server to this client This is
necessary only if the delay cannot be computed automat-
ically by the protocol
-s string, --statsdir=string
Specify the directory path for files created by the
statistics facility This is the same operation as the
statsdir statsdir command
-t number, --trustedkey=number
Add a key number to the trusted key list This option
can occur more than once This is the same operation as
the trustedkey key command
-U number, --updateinterval=number
interval in seconds between scans for new or dropped
interfaces This option takes an integer number as its
argument
Give the time in seconds between two scans for new or
dropped interfaces For systems with routing socket
support the scans will be performed shortly after the
interface change has been detected by the system Use
0 to disable scanning 60 seconds is the minimum time
between scans
--var=nvar
make ARG an ntp variable (RW) This option may appear
an unlimited number of times
--dvar=ndvar
make ARG an ntp variable (RW|DEF) This option may
appear an unlimited number of times
-x, --slew
Slew up to 600 seconds
SunOS 510 Last change: 7
System Administration Commands ntpd(1M)
Normally, the time is slewed if the offset is less than
the step threshold, which is 128 ms by default, and
stepped if above the threshold This option sets the
threshold to 600 s, which is well within the accuracy
window to set the clock manually Note: Since the slew
rate of typical Unix kernels is limited to 05 ms/s,
each second of adjustment requires an amortization
interval of 2000 s Thus, an adjustment as much as 600
s will take almost 14 days to complete This option
can be used with the -g and -q options See the tinker
configuration file directive for other options Note:
The kernel time discipline is disabled with this
option
-, --help
Display usage information and exit
-!, --more-help
Extended usage information passed thru pager
--version
Output version of program and exit

为了避免Linux系统的主机,在长时间运行下所导致的时间偏差。因此我们需要对时间进行时间同步(synchronize)。我们一般使用ntp服务来同步不同机器的时间。NTP 是网络时间协议(Network Time Protocol)的简称,他是通过UDP协议,对时间进行同步的。

编辑NTP Server的主要配置文件为/etc/ntpconf ,如下:

配置含义:

配置完毕,重启NTP服务,使配置更改生效。

查看NTP服务和上级连通状态

内网其他设备作为NTP的客户端配置,相对就比较简单,而且所有设备的配置都相同。
首先需要安装NTP服务(与NTP-Server完全一样)。然后找其中一台配置/etc/ntpconf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。
注释掉之前的上层服务,以下几行

添加本地的NTP服务器即可完成配置

可以用date命令查看时区
如果时区是EST需要改成CST

选择亚洲 /中国 /北京 /然后确认
改完后执行如下复制语句

使用

1721730100为你的NTP服务器的ip地址,显示adjust time server 1921681135 offset 0004882 sec
这里有可能出现同步失败,一般情况下原因都是本地的NTPD服务器还没有正常启动起来,一般需要几分钟时间后才能开始同步。
也有报错 no server suitable for synchronization found,这种问题由一下两种情况造成:
Server dropped: strata too high:
并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
这种问题往往是由ntp服务启动后还没有和上层服务同步完成,可以等五分钟再次查看。
Server dropped: strata no data:
这种问题一般是因为访问不到NTP服务,需要检查NTP服务是否启动成功,或者是否是被防火墙拦截。
启动NTP_Client

启动后,查看同步情况

由于是内网,NTP服务很快会同步上,可以使用下面命令查看时间是否和服务器时间相同

本机客户端配置完成后,需要同步的客户端机器使用SCP拷贝/etc/ntpconf,命令如下,启动NTP服务即可。

Linux的NTP配置总结
NTP服务及时间同步问题

官网已经提供了具体的安装指导,下面仅仅是简单的翻译了一下(安装的 *** 作系统:Win10_lstc_2019_x64):

不用修改,关闭txt即可

注意:修改了ntpconf文件以后需要重启ntp服务,才能生效。

NTP服务启动后需要几分钟左右的时间才会对外提供服务。

1默认权限设定

restrict default noquery nopeer nomodify notrap

restrict -6 default noquery nopeer nomodify notrap

2本地信任

restrict 127001
restrict -6 ::1

3授权远程服务权限

restrict [ 客户端IP ] mask [ IP掩码 ] [参数 ]

restrict 19216801 mask 2552552550 noquery nomodify

19216801-1921680255 这个网段的IP,都可以对时。单个IP只需要mask 255255255255即可,19216801是网关地址。2552552550是子网掩码。

经过实验发现:

4上层授时服务器

server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

server IP1/Domain Name1 iburst minpoll 4 maxpoll 6 prefer
server IP2/Domain Name2

NTP消息的最大轮询间隔默认为10(1024秒=17分钟),可以通过maxpoll增加到17(364小时)的上限。最小轮询间隔默认为6(64秒),但可以通过minpoll降低到4(16秒的)下限。

server 1202511520 prefer minpoll 4 maxpoll 6 iburst

5本地时钟源

server 12712710
fudge 12712710 stratum 12

当没有时间同步来源的时候以自身的硬件时钟为准,这里的stratum是代表层级,创建时默认是12,0-15层。

ntpd 对下层 client 来说是 service server,对于上层 server 来说它是 client,也就是说新版的 NTP 服务程序已经不对服务端和客户端进行区分了,统一叫做 ntpd。ntpd 根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。

所以只需要将本机的stratum设置成server数+1即可。

6其他

driftfile "C:\NTP\etc\ntpdrift"

用于记录主机在与上层授时服务器之间沟通时所花费的时间

ntpd 服务并不是在任何情况下都会进行同步的。当时钟服务器时间和本地时间相差大于1024s 时,ntp 服务就会认为是人为调整了时钟或出现了硬件故障,例如 CMOS 电池损坏等。此时,NTP服务就会退出,需要人工使用 ntpdate进行时钟同步。

采用 -g 选项可以让 ntpd 忽略 1000s 或更大误差

7本地防护墙

ntp服务器默认UDP协议,123端口,需要在入站防火墙上进行端口UDP协议123的允许访问即可。

ntpq -p 列出本机与所有的server的状态

需要以管理员启动才能执行 start,restart,stop *** 作。

Allow initial big time step:大于1024秒,作为客户端是否还同步NTP的时间。 不建议勾上 ,作为对外授时的服务器,使用来自上层授时服务器的时间,如果时间与上层时间超过了1024秒,还是需要人员手动检查。

Set multimedia timer to highest resolution:多媒体定时器优化,使其最优能提供1ms延迟

Restart NTP service if stopped:如果NTP服务挂了是否自动重启。守护进程

本次安装的版本是“NetTimeSetup-320a3_NTP客户端”,安装步骤较简单,省略。

每次设置完,点击一次Update Now 确认运行状态

方法一
采用Windows、linux等 *** 作系统自带的时间同步功能
对于Windows *** 作系统,自身已经集成了自动对时功能,在此仅以windows *** 作系统为例介绍一下。

双击系统托盘下方的时间在服务器地址栏输入NTP服务器的IP地址(1cnpoolntporg),然后点击“确定”按钮保存下来就行了。
方法二
修改注册表,提高时间同步精度
由于系统默认的时间同步间隔是7天,我们无法自由选择,使得这个功能在灵活性方面大打折扣。其实,我们也可以通过修改注册表来手动修改它的自动同步间隔以提高同步精度,以下以XP系统为例。
1 在“开始”菜单→“运行”项下输入“Regedit”进入注册表编辑器
2 展开[
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/W32Time/TimeProviders/NtpClient
] 分支,并双击 SpecialPollInterval 键值,将对话框中的“基数栏”选择到“十进制”上
3
对话框中显示的数字正是自动对时的间隔(以秒为单位),比如默认的604800就是由7(天)×24(时)×60(分)×60(秒)计算来的。设定时间同步周期(建议设为900=15分钟或3600=1小时等周期值),填入对话框,点击确定保存关闭对话框。
4
在Parameters列表中,将NtpServer键值修改为NTP服务器的IP地址(1cnpoolntporg),然后点击“确定”按钮保存。
方法三,采用其他NTP客户端程序时间同步

可以在互联网上找到其他客户端程序实现电脑时间同步。

只是从网上整理了一下文档,还没有实践。感觉整理的还不错。

GMT时间是以太阳通过格林威治的那一刻来作为计时的标准,地球共有24 个时区,而以格林威治时间(GMT) 为标准时间,台湾本地时间为GMT + 8 小时。不准确,但是方便记忆与理解。
UTC时间是使用『原子震荡周期』所计算的物理时钟。最准确。
两个时间计时的方式不同,GMT与UTC时间有差不多16分钟的误差!

因为时区资料档在/usr/share/zoneinfo 内,在该目录内会找到/usr/share/zoneinfo/America/New_York 这个时区档。而时区设定档在/etc/sysconfig/clock ,且目前的时间格式在/etc/localtime ,所以你应该这样做:

其中parameter 的参数主要有底下这些:

那如果你没有在parameter的地方加上任何参数的话,这表示『该IP或网段不受任何限制』的意思喔!一般来说,我们可以先关闭NTP的使用权限,然后再一个一个的启用允许登入的网段。

常见的配置如下:

常见的配置如下:

因为预设的NTP Server 本身的时间计算是依据BIOS 的晶片震荡周期频率来计算的,但是这个数值与上层 Time Server 不见得会一致。所以NTP 这个daemon (ntpd) 会自动的去计算我们自己主机的频率与上层 Time server的频率,并且将两个频率的误差记录下来,记录下来的档案就是在driftfile 后面接的完整档名当中了!
关于档名你必须要知道:

driftfile 后面接的档案会被ntpd 自动更新,所以他的权限一定要能够让ntpd 写入才行。在CentOS 6x 预设的NTP 伺服器中,使用的ntpd 的owner 是ntp ,这部份可以查阅/etc/sysconfig/ntpd 就可以知道啦!

常见的配置如下:

除了以restrict 来限制用户端的连线之外,我们也可以透过金钥系统来给用户端认证, 如此一来可以让主机端更放心了。可以参考ntp-keygen 这个指令的相关说明。

ntp这个daemon是以port 123为连结的端口(使用UDP封包)

设定完ntpconf 之后就可以启动ntp 服务器了。启动与观察的方式如下:

这样就表示我们的NTP伺服器已经启动了,不过要与上层NTP服务器连线则还需要一些时间, 通常启动NTP后约在15分钟内才会和上层NTP伺服器顺利连接上。

请自行等待数分钟后再以下列指令查阅:

这个指令可以列出我们的NTP 服务器有跟上层连线否。由上述的输出结果可以知道,时间有校正约 538 10^(-3) 秒(538ms),且每隔128 秒会主动去更新时间!

这个ntpq -p 可以列出目前我们的NTP 与相关的上层NTP 的状态,上头的几个栏位的意义为:

差异都在0001 秒以内, 可以符合我们的一般使用了。另外,你也可以检查一下你的BIOS 时间与Linux 系统时间的差异, 就是/var/lib/ntp/drift 这个档案的内容,就能了解到咱们的Linux 系统时间与BIOS 硬体时钟到底差多久。

ntpdate这个方式仅适合不要启动NTP 的情况。如果你的机器数量太多了,那么用户端最好也启动一下NTP 服务。通过NTP 去主动的更新时间。

然后取消掉crontab 的更新程序,这样你的client 电脑就会主动的到NTP 伺服器去更新。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10396914.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-08
下一篇2023-05-08

发表评论

登录后才能评论

评论列表(0条)

    保存