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

企业新闻

Linux下NTP 服务端和客户端对时方法

发布时间:2022-11-21 01:03:07浏览次数:

NTP:NTP 是网络时间协议(Network Time Protocol),它是用来同步网络

中各个计算机时间的协议。其对时精度在局域网内可达 0.1ms,且相较于

ntpdate 方式,ntp 对时更加平滑,不会造成时间跳跃式突变。

1.操作安系统出厂默认已安装 ntp 包,但 ntp 服务未自启动,需要进行

配置后再行启动,配置文件为/etc/ntp.conf,ntp 服务使用的端口号是 123,

使用 rpm -qa | grep ntp 命令查询当前系统是否已安装 ntp 包。

2. 客户端配置

vi /etc/ntp.conf 文件,找到如下内容

server 0.rhel.pool.ntp.org iburst

server 1.rhel.pool.ntp.org iburst

server 2.rhel.pool.ntp.org iburst

server 3.rhel.pool.ntp.org iburst

将这默认 4 行 server 开头的行添加#注释,并在上面添加一行:

server <时钟设备对时的 IP 地址> iburst 如下图所示

3. 服务端配置

vi /etc/ntp.conf 文件,添加内容

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

//表示允许 192.168.1.0 网段所有的机器与该服务器进行对时

server 127.127.1.0

//在不联网的时候可以把本机时间作为同步时间源

fudge 127.127.1.0 stratum 10

//设置 ntp 优先级。stratum 取值是 0-16,数字越小优先级越高

4. 服务端和客户端都启动 ntp 服务并设置开机自启动

systemctl start ntpd.service

systemctl enable ntpd.service

在客户端查看与服务端的对时状态:ntpq -p

用 date 命令查看客户端和服务端的时间是否一致。

最后使用crontab -e 添加一个定时任务,让系统定时去执行任务。

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# For details see man 4 crontabs

# Example of job definition:

# .---------------- minute (0 - 59)

# | .------------- hour (0 - 23)

# | | .---------- day of month (1 - 31)

# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...

# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * user-name command to be executed

*/300 * * * * /usr/sbin/ntpdate -u 192.168.1.1 >/dev/null 2>&1

400-080-6079