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命令service ntpd start
命令ntpstat
查看ntp状态时,可能会出现如下所示情况
① unsynchronised time server re-starting polling server every 8 s
② unsynchronised polling server every 8 s
这种情况属于正常,ntp服务器配置完毕后,需要等待5-10分钟才能与/etc/ntpconf中配置的标准时间进行同步。
等一段时间之后,再次使用ntpstat命令查看状态,就会变成如下正常结果:
命令ntpq -p
remote:本机和上层ntp的ip或主机名,“+”表示优先,“”表示次优先
refid:参考上一层ntp主机地址
st:stratum阶层
when:多少秒前曾经同步过时间
poll:下次更新在多少秒后
reach:已经向上层ntp服务器要求更新的次数
delay:网络延迟
offset:时间补偿
jitter:系统时间与bios时间差
4)查看ntpd进程的状态
命令watch "ntpq -p"
终止按 Ctrl+C 停止查看进程。
第一列中的字符指示源的质量。星号 ( ) 表示该源是当前引用。
remote:列出源的 IP 地址或主机名。
when:指出从轮询源开始已过去的时间(秒)。
poll:指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
reach:是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
offset:是源时钟与本地时钟的时间差(毫秒)。当你配置正确,你可以用Windows Time(W32Time)service 建一个SNTP/NTP服务器为Windows或非windows客户端提供时间服务。
步骤如下:
1打开开始菜单,点击运行输入regedit打开Windows注册表。 2找到如下位置
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config 3找到AnnounceFlags 双击修改值为5保存。 4 开启NTPServer
1在注册表中找到如下位置:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer\ 2找到Enable并修改其值为1
5打开开始菜单输入CMD在命令行模式下输入:
net stop w32time && net start w32time 重启一下win32time服务。 ----------------------------- 需要注意的问题:
1 在系统服务中设置W32Time服务启动模式为自动。 2确定防火墙允许UDP123端口访问。
这样你可以在NTP客户端上测试从这台服务器校对系统时间啦。
也可参考下面的资料:>第一步,选择最好的NTP服务地址
具体命令为ntpdate -q IP地址或域名
广东地区NTP优选结果如下:
①time4cloudtencentcom
②server timeasiaapplecom
③server cnntporgcn
④server ntpaliyuncom
⑤server cnpoolntporg
检查BIOS主板时间的命令
hwclock -r
NTP服务启停命令
sudo systemctl start/stop ntpd
检查查看ntp服务器有无和上层ntp连通
ntpstat
查看ntp服务器与上层ntp的状态
ntpq -pn
===server选项格式===
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 如果有多个server选项,具有该参数的服务器优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
===同步硬件时钟===
ntp服务,默认只会同步系统时间。
如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,
在/etc/sysconfig/ntpd文件中,添加SYNC_HWCLOCK=yes这样,就可以让硬件时间与系统时间一起同步。
允许BIOS与系统时间同步,也可以通过hwclock -w 命令。
hwclock命令用来查询和设置硬件时钟。
hwclock -r 读取并打印硬件时钟
hwclock -s 将硬件时钟同步到系统时钟
hwclock -w 将系统时钟同步到硬件时钟
系统时钟与硬件时钟
在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。
参考文档:
Linux服务器NTP客户端配置——>1 在xCAT管理节点上,编辑/opt/xcat/etc/ntpservers文件,添加NTP服务器的IP地址,每行一个,如:19216811
2 在xCAT管理节点上,运行以下命令,将NTP服务器的IP地址推送到所有节点:# chdef -t node ntpservers="19216811"
3 在xCAT管理节点上,运行以下命令,将NTP服务器的IP地址推送到指定节点:# chdef -t node -o node1 ntpservers="19216811"
4 在xCAT管理节点上,运行以下命令,将NTP服务器的IP地址推送到指定网络:# chdef -t net ntpservers="19216811"我就是这样用的,很好。
Windows(2003)下配置NTP时间服务器
一、服务端设置:
因为默认情况下,WINDOWS SERVER 2003 是作为NTP客户端工作的 ,所以必须通过修改注册表,以使系统作为NTP服务器运行。注意,工作之前请先备份注册表文件。
1、通过开始菜单,输入regedit命令后打开注册表设定画面,此时请一定备份注册表文件。
2、修改以下选项的键值
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\
NtpServer内的「Enabled」设定为1,打开NTP服务器功能
3、修改以下键值
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\
AnnounceFlags设定为5,该设定强制主机将它自身宣布为可靠的时间源,从而使用内置的互补金属氧化物半导体(CMOS) 时钟。
4、在dos命令行执行以下命令,确保以上修改起作用
net stop w32time
net start w32time
推荐计划;如果该服务器和internet连接,那么为了避免服务器和internet上的ntp同步,最好追加以下配置:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\
NtpClient的「enable」设定为0 以防止作为客户端自动同步外界的时间服务
二、客户设置:
1、指定主时间服务器。在DOS方式输入“net time /setsntp:ntpsjtueducn”,这里我们指定ntpsjtueducn是主时间服务器,也可以是其它地址(2107214544)。
2、自由设定XP时间同步间隔:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\
NtpClient的「SpecialPollInterval」默认设定为604800(或86400)
一、安装ntp和ntpdate
二、主机设置为NTP服务器(时间基准来源)
三、从机1配置NTP客户端
四、从机2配置NTP客户端
五、手动将客户端时间与NTP服务器端同步
sudo apt-get install ntp ntpdate
两个都得装
sudo gedit /etc/ntpconf
第一行是为了能让 1921681xxx 网段上的机器能和本机进行时间同步(具体看你的ip是不是 1921681xxx ,如果不一样的话,改成你实际的格式),第二行和第三行是为了让本机的硬件时间和本机的ntp服务进行时间同步。
sudo /etc/initd/ntp restart
主机服务器端设置完成
sudo gedit /etc/ntpconf
server 1921681xxx
把ip改成主机服务器端的ip地址即可
sudo /etc/initd/ntp restart
从机1客户端设置完成
sudo gedit /etc/ntpconf
server 1921681xxx
把ip改成主机服务器端的ip地址即可
sudo /etc/initd/ntp restart
从机2客户端设置完成
sudo ntpdate -q 192168158
service ntp stop
sudo ntpdate 1921681058
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)