基础服务管理篇(二)——ntp时间同步
ntp时间同步服务
1.介绍
NTP是网络时间协议(Network Time Protocol),是用来同步网络中各个计算机的时间的协议。
2.原理
标准时间是由原子钟报时的国际标准时间UTC(世界协调时),因此NTP获得UTC的时间来源可以是原子钟、天文台、卫星,Internet。在NTP中,定义了时间按照服务器的等级传播,Stratum层的总数限制在15以内。
3.时间服务器地址
NTP授时网站:http://www.ntp.org.cn/pool
4.timedatectl
4.1显示当前系统的时间、日期
CST:北京时间,China Standard Time,中国标准时间
UTC:世界标准时间,又称世界协调时间,Coordinated Universal Time,
整个地球分为二十四时区,每个时区都有自己的本地时间,为了统一起见,使用一个统一的时间
DST:夏令时指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用,中国不使用
4.2列出机器上支持的所有时区
# 将本地时区从上海(Asia/Shanghai)设置为阿姆斯特丹(Europe/Amsterdam)
[root@localhost ~]# timedatectl set-timezone "Europe/Amsterdam"
[root@localhost ~]# timedatectl status
Local time: Mon 2022-03-21 09:31:52 CET
Universal time: Mon 2022-03-21 08:31:52 UTC
RTC time: Mon 2021-12-13 22:23:07
Time zone: Europe/Amsterdam (CET, +0100)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2021-10-31 02:59:59 CEST
Sun 2021-10-31 02:00:00 CET
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2022-03-27 01:59:59 CET
Sun 2022-03-27 03:00:00 CEST
# 将本地时区恢复为亚洲/上海
[root@localhost ~]# timedatectl set-timezone "Asia/Shanghai"
[root@localhost ~]# timedatectl status
Local time: Mon 2022-03-21 16:32:50 CST
Universal time: Mon 2022-03-21 08:32:50 UTC
RTC time: Mon 2021-12-13 22:24:05
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
4.3其他用法
[root@localhost ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
指令
Commands:
status Show current time settings 查看当前状态
set-time TIME Set system time 设置当前的时间
set-timezone ZONE Set system time zone 设置当前的时区
list-timezones Show known time zones 查看系统支持哪些时区
# 修改时区为UTC(世界标准时间)
timedatectl set-timezone UTC
# 设置系统时间(HH:MM:SS)
timedatectl set-time "16:40:46"
# 设置系统日期(YY-MM-DD)
timedatectl set-time "2022-03-21"
# 设置NTP服务开启(需要安装ntp时间同步服务)
timedatectl set-ntp true/false
# 查看系统支持多少个时区
[root@localhost ~]# timedatectl list-timezones | wc -l
425
# 找出关于上海时区的具体名字
[root@localhost ~]# timedatectl list-timezones | grep -i 'shanghai'
Asia/Shanghai
5.ntp时间同步
- 手动同步:时间校准,ntpdate命令,强制性更新整个系统的时间,不友好
- 自动同步:时间同步,搭建ntpd服务,自动友好地更新
ntpd在实际同步时间时是一点点的校准的,最终把时间慢慢校正对,而ntpdate不会考虑其他程序,直接调整时间,许多应用程序依赖连续的时钟,使用ntpdate这样的时钟跃变,有时候会导致很严重的安全问题。因此,企业服务器里一般会部署ntpd服务,让服务器自动、定期进行时间同步,保证服务器集群的时间正确且一致。
5.1手动同步
#1.安装ntpdate命令
yum install ntpdate -y
#2.找到时间服务器地址,强制更新即可
[root@localhost ~]# ntpdate cn.ntp.org.cn
21 Mar 16:58:53 ntpdate[2368]: step time server 120.197.116.202 offset 0.811401 sec
-u 指定使用无特权的端口发送数据包。 当在一个对特权端口的输入流量进行阻拦的防火墙后是很有益的, 并希望在防火墙之外和主机同步。防火墙是一个系统或者计算机,它控制从外网对专用网的访问。
[root@localhost ~]# ntpdate -u ntp.aliyun.com
21 Mar 17:01:41 ntpdate[2382]: adjust time server 203.107.6.88 offset 0.003789 sec
!!报错原因:1.机器无法上网
2.NTP服务器有问题
5.2自动同步(推荐)
1.安装ntp软件
yum install ntp -y
2.查看ntp软件信息
[root@localhost ~]# ls /usr/lib/systemd/system/ |grep ntp
ntpdate.service
ntpd.service
3.修改ntp软件的配置文件/etc/ntp.conf
driftfile /var/lib/ntp/drift
#ntp运行日志
logfile /var/log/my_ntp.log
#记录程序的运行进程号
pidfile /var/run/ntpd.pid
# 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 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
server ntp.aliyun.com iburst prefer
server cn.pool.ntp.org iburst
4.将机器时间修改为错误时间
[root@localhost ~]# timedatectl set-time '2018-08-08'
[root@localhost ~]# timedatectl
Local time: Wed 2018-08-08 00:00:04 CST
Universal time: Tue 2018-08-07 16:00:04 UTC
RTC time: Tue 2018-08-07 16:00:05
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
5.启动ntpd服务,等待时间同步
[root@localhost ~]# systemctl start ntpd
6.查看ntp是否和上游服务器同步(该机器成为一个时间服务器,可以作为上游机器使用)
[root@localhost ~]# ntpstat
unsynchronised
polling server every 64 s
7.查看时间同步的状态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*203.107.6.88 100.107.25.114 2 u 45 64 1 14.592 0.897 0.384
+ntp5.flashdance 192.36.143.130 2 u 106 64 2 304.089 24.838 14.938
8.查看同步后的时间
[root@localhost ~]# timedatectl
Local time: Mon 2022-03-21 17:24:55 CST
Universal time: Mon 2022-03-21 09:24:55 UTC
RTC time: Tue 2018-08-07 16:04:20
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a