linuxntp服务
‘壹’ linux ntp是什么服务
服务器时间同步。NTP是网络时间同步协议,就是用来同步网络中各个计算机的时间的协议。Linux配置NTP时间同步,常用命令有
启动NTP时间服务器:service ntpd start
设置NTP开机自动启动:chkconfig ntpd on
查看NTP是否正常运行:netstat -tlunp | grep ntp 你试试看吧 看看《Linux就该这么学》 里面有个专栏是 Linux命令大全(手册
‘贰’ linux怎么卸载ntp服务包
1. 使用yum或rpm命令查看ntp服务包:
```
# yum list installed | grep ntp
# rpm -qa | grep ntp
```
2. 使用yum或rpm命圆隐蠢令卸携腊载ntp服橘陪务包:
```
# yum remove ntp
# rpm -e ntp
```
‘叁’ Linux里面ntp端口是多少
NTP默认使用UDP端口号123;
NTP在TCP中使用端口号123
NTP是用来使计算机时间同步化的一种协议。NTP默认使用UDP端口号123。 由于UDP的简单性和执行行为,NTP服务在UDP协议上运行。由于UDP并非完全可靠的协议,因此在使用过程中可能会产生一些问题。 TCP提供了更可靠的协议包传输。 因此,TCP端口号123可用于更可靠和较少问题的NTP传输。
‘肆’ linux怎么添加ntp服务器
个人linux系统比如ubuntu、deepin等都已经安装了,只需要勾选同步网络时间即可。
如果是服务器可以参考:
[root@localhost /]# yum install ntp -y
2.修改NTP配置文件,添加NTP服务器的网络位置 /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default 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 ::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 (http://www.pool.ntp.org/join.html).
server 192.168.1.1 iburst #目标服务器网络位置
#server 1.CentOS.pool.ntp.org iburst #一下三个是CentOS官方的NTP服务器,我们注释掉
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
保存退出
3.启动服务并设置开启自启
[root@localhost /]# systemctl start ntpd.service#启动服务
[root@localhost /]# systemctl enable ntpd.service#设置为开机启动
‘伍’ Linux系统下的NTP配置
第一步,选择最好的NTP服务地址
具体命令为ntpdate -q IP地址或域名
广东地区NTP优选结果如下:
①time4.cloud.tencent.com
②server time.asia.apple.com
③server cn.ntp.org.cn
④server ntp.aliyun.com
⑤server cn.pool.ntp.org
检查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客户端配置——https://www.cnblogs.com/paul8339/p/10059364.html
NTP服务、客户端配置详解——https://blog.51cto.com/u_11392081/1784080
‘陆’ linux怎么添加ntp服务器
执行ntpdate xxx.xxx.xxx.xxx(ntp服务器地址)。
Linux下配置NTP服务器
配置方法:
1、ntp服务安装
NTP服务在RHEL5.x中式默认安装的软件包,可用rpm -qa |grep
ntp检查是否安装,如果未安装可使用如下命令安装与删除NTP服务器软件包
#使用rpm方式安装
rpm -ivh ntp-4.2.2p1-8.el5.i386.rmp
#使用yum方式安装
yum -y install ntp.i*
#使用rpm方式删除
rpm -e ntp-4.2.2pl-8.el5.i386.rpm
#使用yum方式删除
yum -y remove ntp.i*
2、ntp服务配置
在ntp服务安装完成后,需要修改ntp服务配置文件。ntp服务主配置文件/etc/ntp.conf
2.1 ntp服务的启动、暂停
NTP属于system
V服务,其启动、暂停相关命令如下:
#启动
service ntpd
start
#停止
Service ntpd
stop
#重新加载
service ntpd
reload
#查看当前启动状态
service ntpd
status
2.2 ntp服务自动加载
设置ntp服务下一次开机运行状态可通过命令配置
#在运行级别2、3、4、5上设置为自动运行
chkconfig
ntpd on
#在运行级别2、3、4、5上设置为不自动运行
chkconfig
ntpd off
#在运行级别3、5上设置为自动运行
chkconfig
ntpd --level 35 on
#在运行级别3、5上设置为不自动运行
chkconfig
ntpd --level 35 off
2.3 配置信息
配置ntp,将Internet上的时间服务器作为内部标准时间来源,过程如下
1)修改/etc/ntp.conf文件
restrict default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6::1
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#指定Internet上的时间服务器
restrict 207.46.232.182 mask 255.255.255.255 nomodify notrap noquery
server 207.46.232.182
server 127.127.1.0
fudge 127.127.1.0 stratum 10
keys /etc/ntp/keys
#指定NTP服务器日志文件
logfile /var/log/ntp
2)修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对)
207.46.232.182
127.127.1.0
3)修改/etc/sysconfig/ntpd文件,内容如下:
#允许BIOS与系统时间同步,也可以通过hwclock -w 命令
SYNC_HWCLOCK=yes
4)在配置完成并重新启动服务后,可通过ntpstat
命令显示本机上一次与上层ntp服务器同步时间的情况,也可以使用ntpq -p 查看本机与上层ntp服务器通信情况,
2.4 图形界面配置NTP
图形界面下直接用system-config-date, 在“Network Time Protocol”选项页指定NTP服务器后勾选“Synchronize system clock before startingservice”,将ntpd服务设置为自动运行即可。
2.5 客户端访问
1)Linux客户端可通过图形界面进行配置,也可以通过ntpdate命令立即与NTP服务器及行时间校对。
ntpdate 192.168.0.10
2)Windows客户端,只需双击任务栏右下角的时钟,选择“Internet时间”选项页,输入NTP服务IP地址或FQDN,并勾选“自动与Internet时间服务器同步”即可。
Windows默认7天自动更新一次,可通过修改注册表HKEY_LOCAL_MACHINE\SYSTEM
\CurrentControlSet\services\W32Time\TimeProviders\NtpClient\SpecialPollInterval 默认单位为妙,修改后重新启动即可生效。
‘柒’ linux上ntp时钟服务器搭建
配置的时钟服务器的ip地址为192.168.100.116
第一步 为服务器和客户机安装ntp ntpdate
[root@sql-proxy1 app]#yum install ntp ntpdate -y第二步 查找时间同步服务器
http://www.pool.ntp.org/zone/asia #在里面可以找到中国第三步 编辑 /etc/ntp.conf
[root@sql-proxy1 app]#vim /etc/ntp.conf server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org #以上四个是时间同步服务器server 127.127.1.0 iburst local clock #当外部时间不可用时,使用本地时间restrict 192.168.10.1 mask 255.255.255.0 nomodify #允许更新的IP地址段第四步 启动ntp服务
[root@sql-proxy1 app]#systemctl start ntpd第五步 验证服务
[root@sql-proxy1 app]#ntpq -p remote refid st t when poll reach delay offset jitter
==============================================================================
120.25.115.19 .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp6.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000
time5.aliyun.co .INIT. 16 u - 64 0 0.000 0.000 0.000
85.199.214.101 .INIT. 16 u - 64 0 0.000 0.000 0.000
biisoni.miuku.n .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp5.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000
leontp.ccgs.wa. .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp1.ams1.nl.le .INIT. 16 u - 64 0 0.000 0.000 0.000
*LOCAL(0) .LOCL. 5 l 7 64 1 0.000 0.000 0.000
第六步 远程客户端时间同步测试
[root@sql-mysql2 ~]#ntpdate 192.168.10.116 17 Dec 20:55:08 ntpdate[11904]: adjust time server 172.18.68.31 offset -0.000067 sec
第七步 客户端设置计划任务,每天晚上1点同步时间
crontab -e
00 01 * * * root /usr/sbin/ntpdate 192.168.10.116; /usr/sbin/clock -w
EDT 时间即美国东部时间。这里要改为北京时间即可:
命令如下:
root@ubuntu# mv /etc/localtime /etc/localtime.bak
root@ubuntu# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@ubuntu# date
修改116服务器本机的ntp时间同步
systemctl stop ntpd
ntpdate cn.pool.ntp.org
systemctl start ntpd
rpm -qa |grep ntp #检查ntp版本
service ntpd status #查询ntp服务状态
service ntpd start #启动
service ntpd stop #停止
service ntpd restart #重启
ntpq -p #查看ntp服务器与上层ntp的状态
‘捌’ 如何在linux 上配置NTP 时间同步
一:NTP是网络时间同步协议,就是用来同步网络中各个计算机的时间的协议。
二:NTP服务端配置
2.1、检查系统是否安装了NTP包(linux系统一般自带NTP4.2),没有安装我们直接使用yum命令在线安装: yum install ntp
2.2、NTP服务端配置文件编辑: vim /etc/ntp.conf
结果:
restrict 控制相关权限。
语法为: restrict IP地址 mask 子网掩码 参数
其中IP地址也可以是default ,default 就是指所有的IP
参数有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
kod : 访问违规时发送 KoD 包。
restrict -6 表示IPV6地址的权限设置。
root@www ~]#vim /etc/ntp.conf# 1. 先处理权限方面的问题,包括放行上层服务器以及开放区网用户来源:restrict default kod nomodify notrap nopeer noquery <==拒绝 IPv4 的用户restrict -6 default kod nomodify notrap nopeer noquery <==拒绝 IPv6 的用户restrict 220.130.158.71 <==放行 tock.stdtime.gov.tw 进入本 NTP 服务器restrict 59.124.196.83 <==放行 tick.stdtime.gov.tw 进入本 NTP 服务器restrict 59.124.196.84 <==放行 time.stdtime.gov.tw 进入本 NTP 服务器restrict 127.0.0.1 <==底下两个是预设值,放行本机来源restrict -6 ::1restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行区网来源# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定注解掉:server 220.130.158.71 prefer <==以这部主机为最优先server 59.124.196.83server 59.124.196.84# 3.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:driftfile /var/lib/ntp/driftkeys /etc/ntp/keys
ntpd、ntpdate的区别
下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:
使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。
时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。