【CentOS服务器时间维护--时间统一,万事之基】

 

 

 

 

 

[标签]: 服务器时间    ntpd守护进程和ntpdate命令使用   Linux的系统时间设置

 

 

问题详情的描述或方案目标的预期:

时间的准确是服务器上所有应用运行的基础,很多人没有发现它的重要性,其实,涉及标准无小事。该文是我这老运维花了不少时间总结的,希望对大家有帮助。

 

 

 

【解决方案】

 

01 确认服务器时区设置是否正确

ll /etc/localtime

  

02 如果不正确修改方法:

timedatectl set-timezone Asia/Shanghai

或者 mv /etc/localtime /etc/localtime.bak && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

-- 不要软链接直接复制覆盖也行

  

03 安装同步工具

查看是否已安装 rpm -qa | grep ntp

如果没有,执行安装 yum -y install ntp ntpdate

  

04 ntpdate同步时间

ntpdate ntp1.aliyun.com // -p打印同步过程但实际不同步

210.72.145.44 #中国国家授时中心(ntpd服务器百度找一个可用的即可,也可以写本地网关192.168.0.1)

2.cn.pool.ntp.org

  

05 如果上一步ntpdate无法获得正确的时间,可以手动设定

系统时间:查看--date;设置时间 date -s "2022-04-08 09:42:00"

硬件时间:查看--hwclock 或 hwclock --show;设定时间 hwclock -set -date="07/07/06 10:19" (月/日/年 时:分:秒)

同步: hwclock --hctosys(硬件同步给系统)、hwclock --systohc(系统同步给硬件)

保存时钟: clock -w

  

06 设定crond定时任务 //每天23:59分同步时间

59 23 * * * * /usr/sbin/ntpdate ntp1.aliyun.com && /sbin/hwclock -w

  

  

  

使用ntpd服务以守护进程方式维护时间,要好于ntpdate加cron的组合。因为,ntpdate同步时间,会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响。比如sleep,timer等。而且,ntpd服务可以在修正时间的同时,修正cpu tick。

理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

要注意的是,ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行。所以新设置的时间服务器一定要先ntpdate从上源取得时间初值, 然后启动ntpd服务。ntpd服务运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程.

  

======== 如果采用ntpd守护进程的方式 =========== >>>

  

修改ntp配置文件/etc/ntp.conf

1)注释以下配置

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#server 0.centos.pool.ntp.org iburst

#server 1.centos.pool.ntp.org iburst

#server 2.centos.pool.ntp.org iburst

#server 3.centos.pool.ntp.org iburst

  

2)新增如下配置

#日志文件

logfile /var/log/ntpd.log

  

#授权172.16.1.0网段上所有机器可以从这台机器上查询和时间同步

restrict 172.16.1.0 mask 225.225.225.0 nomotify notrap

  

#时间服务器列表

server 210.72.145.44 #中国国家授时中心

server ntp1.aliyun.com

server ntp2.aliyun.com

server ntp3.aliyun.com

  

#当外部时间不可用时,使用本地时间

server 127.0.0.1

fudge 127.0.0.1 stratum 10

  

#允许上层时间服务器主动修改本机时间

restrict 210.72.145.44 nomodify notrap noquery

restrict ntp1.aliyun.com nomodify notrap noquery

restrict ntp2.aliyun.com nomodify notrap noquery

restrict ntp3.aliyun.com nomodify notrap noquery

  

  

保存退出,重启ntp服务、加入开机自启动

# systemctl enable ntpd

# systemctl restart ntpd

//如果采用这种方法,最好把crond的时间同步任务取消,因为不取消在ntpd启动时ntpdate命令也是不可用的。

  

  

[常见问题集锦]:

01 ntpd服务器不可用,报错【202.120.2.101: Server dropped: no data】、【10 Aug 21:33:56 ntpdate[19587]: no server suitable for synchronization found】

ntpdate -d 202.120.2.101

  

02 ntpd服务运行的时候执行ntpdate命令会报错:【ntpdate[19659]: the NTP socket is in use, exiting】

/usr/sbin/ntpdate 2.cn.pool.ntp.org

需要先停掉ntpd服务:systemctl stop ntpd 。因为当ntpd以守护进程的方式运行时,不接受ntpdate的时间设置。时间设置通常以crond任务方式定期运行ntpdate来修正时间;或者以守护进程方式让ntpd运行在后台那这时候就不需要同时设定crond任务去执行ntpdate命令了。

  

03 ntpd服务器正常的输出信息:

ntpdate -d 2.cn.pool.ntp.org

"""

[root@BLUBN-IDC-04 ~]# ntpdate -d 2.cn.pool.ntp.org

10 Aug 21:35:07 ntpdate[19588]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)

Looking for host 2.cn.pool.ntp.org and service ntp

host found : 139.199.214.202

transmit(139.199.214.202)

receive(139.199.214.202)

transmit(94.237.64.20)

transmit(94.130.49.186)

transmit(182.92.12.11)

receive(182.92.12.11)

receive(94.130.49.186)

transmit(139.199.214.202)

receive(139.199.214.202)

transmit(94.237.64.20)

transmit(182.92.12.11)

receive(182.92.12.11)

transmit(94.130.49.186)

receive(94.130.49.186)

transmit(139.199.214.202)

receive(139.199.214.202)

transmit(94.237.64.20)

transmit(182.92.12.11)

receive(182.92.12.11)

transmit(94.130.49.186)

receive(94.130.49.186)

transmit(139.199.214.202)

receive(139.199.214.202)

transmit(94.237.64.20)

transmit(182.92.12.11)

receive(182.92.12.11)

transmit(94.130.49.186)

transmit(94.237.64.20)

receive(94.130.49.186)

94.237.64.20: Server dropped: no data

server 139.199.214.202, port 123

stratum 2, precision -23, leap 00, trust 000

refid [139.199.214.202], delay 0.04486, dispersion 0.00661

transmitted 4, in filter 4

reference time: e69ed9fb.e16056ae Wed, Aug 10 2022 21:29:31.880

originate timestamp: e69edb54.c163cce4 Wed, Aug 10 2022 21:35:16.755

transmit timestamp: e69edb54.b73a6ed1 Wed, Aug 10 2022 21:35:16.715

filter delay: 0.09981 0.04947 0.04486 0.07040

0.00000 0.00000 0.00000 0.00000

filter offset: 0.016819 -0.00041 -0.00009 0.017271

0.000000 0.000000 0.000000 0.000000

delay 0.04486, dispersion 0.00661

offset -0.000091

server 94.237.64.20, port 123

stratum 0, precision 0, leap 00, trust 000

refid [94.237.64.20], delay 0.00000, dispersion 64.00000

transmitted 4, in filter 4

reference time: 00000000.00000000 Sun, Dec 31 1899 19:00:00.000

originate timestamp: 00000000.00000000 Sun, Dec 31 1899 19:00:00.000

transmit timestamp: e69edb54.ea2dcd02 Wed, Aug 10 2022 21:35:16.914

filter delay: 0.00000 0.00000 0.00000 0.00000

0.00000 0.00000 0.00000 0.00000

filter offset: 0.000000 0.000000 0.000000 0.000000

0.000000 0.000000 0.000000 0.000000

delay 0.00000, dispersion 64.00000

offset 0.000000

server 94.130.49.186, port 123

stratum 3, precision -25, leap 00, trust 000

refid [94.130.49.186], delay 0.31433, dispersion 0.10988

transmitted 4, in filter 4

reference time: e69ed7e5.907ba925 Wed, Aug 10 2022 21:20:37.564

originate timestamp: e69edb57.009df126 Wed, Aug 10 2022 21:35:19.002

transmit timestamp: e69edb56.5062630c Wed, Aug 10 2022 21:35:18.314

filter delay: 0.65150 0.31433 0.44672 0.85408

0.00000 0.00000 0.00000 0.00000

filter offset: 0.176872 0.007895 0.076638 0.274151

0.000000 0.000000 0.000000 0.000000

delay 0.31433, dispersion 0.10988

offset 0.007895

server 182.92.12.11, port 123

stratum 2, precision -24, leap 00, trust 000

refid [182.92.12.11], delay 0.08434, dispersion 0.03001

transmitted 4, in filter 4

reference time: e69edb17.203f83ce Wed, Aug 10 2022 21:34:15.125

originate timestamp: e69edb55.a99c89ac Wed, Aug 10 2022 21:35:17.662

transmit timestamp: e69edb55.8402fe6a Wed, Aug 10 2022 21:35:17.515

filter delay: 0.23671 0.08434 0.08829 0.19293

0.00000 0.00000 0.00000 0.00000

filter offset: 0.084419 0.009772 -0.00490 0.063198

0.000000 0.000000 0.000000 0.000000

delay 0.08434, dispersion 0.03001

offset 0.009772

10 Aug 21:35:19 ntpdate[19588]: adjust time server 139.199.214.202 offset -0.000091 sec

"""

  

  

  

总结: 时间,这世上最美妙的东西,它是最牛的增强扩效器--强化对的,也放大错的、是毒药、是解药、是酶、是邪恶、是美好,一切都在你怎么把握ta。

 

 

 

百度一下    谷歌翻译

 

 

 

 

 

 

【书君方案】 简洁、完整、漂亮、记忆深刻

 

 

 

 

 

 

©2022 BLUBN.HonL   京ICP备15053931号   书君   bookfooked.com V1.1