在CentOS 7上同步服务器时间的方法与CentOS 6类似。以下是在内网中同步CentOS 7服务器时间的步骤:
-
在内网中选择一台计算机作为NTP服务器。确保该计算机已连接到可靠的网络,并且可以通过TCP/IP协议与其他计算机通信。
-
在NTP服务器上安装并配置NTP软件。在CentOS 7上,您可以使用以下命令安装NTP软件:
yum install ntp
- 编辑NTP配置文件
/etc/ntp.conf
以配置NTP服务器。以下是一个简单的示例配置文件:
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
server 3.centos.pool.ntp.org
restrict default kod nomodify notrap noquery
restrict -6 default kod nomodify notrap noquery
driftfile /var/lib/ntp/drift
在这个示例配置文件中,我们将使用四个公共NTP服务器来同步时间。您也可以将其替换为其他NTP服务器。
- 启动NTP服务并启用自动启动:
systemctl start ntpd
systemctl enable ntpd
- 在所有需要同步时间的计算机上,编辑NTP配置文件
/etc/ntp.conf
。将其中的server
指令修改为NTP服务器的IP地址或主机名。例如:
server 192.168.1.100
- 启动NTP服务并启用自动启动:
systemctl start ntpd
systemctl enable ntpd
- 确保所有计算机都能够连接到NTP服务器。如果NTP服务器被防火墙阻止,请打开UDP端口123。
完成这些步骤后,所有计算机都应该使用NTP服务器进行时间同步。请注意,NTP服务器应该是可靠的,而且在整个内网中应该只有一个NTP服务器。
CentOS 7 更新时间时区
一、修改系统时间
1、查看系统时间
1|date
2、查看时区 +800 表示东八区
1|date -R
3、修改系统时间
1| sudo date -s “20220528 11:30”
二、修改时区
1、读取时间
timedatectl //等同于 timedatectl status
2、设置时间
timedatectl set-time “YYYY-MM-DD HH:MM:SS”
3、罗列所有时区
timedatectl list-timezones
他罗列的是 /usr/share/zoneinfo 文件夹下的文件
4、设置时区
timedatectl set-timezone Asia/Shanghai
三、GMT、UTC、CST、DST 时间
(1) UTC
整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
(2) GMT
格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)
(3) CST
中国标准时间 (China Standard Time)
(4) DST
夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时钟拨快一小时,以提早日光的使用。(中国不使用)
巨坑例子:系统时间是美国时间,系统往前快一个小时,影响系统运行。
美国纽约时间:
[root@localhost ~]# timedatectl
Local time: Wed 2023-03-15 10:01:59 EDT
Universal time: Wed 2023-03-15 14:01:59 UTC
RTC time: Wed 2023-03-15 02:03:29
Time zone: America/New_York (EDT, -0400)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2023-03-12 01:59:59 EST
Sun 2023-03-12 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2023-11-05 01:59:59 EDT
Sun 2023-11-05 01:00:00 EST
上海时间:
[root@localhost ~]# timedatectl
Local time: Wed 2023-03-15 10:07:12 CST
Universal time: Wed 2023-03-15 02:07:12 UTC
RTC time: Wed 2023-03-15 02:07:14
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
GMT + 8 = UTC + 8 = CST
四、系统硬件时间统一
1、vi /etc/sysconfig/clock #编辑文件
ZONE=“Asia/Shanghai”
UTC=false #设置为false,硬件时钟不于utc时间一致
ARC=false
2、ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #linux的时区设置为上海
3、
ntpdate asia.pool.ntp.org
#对准时间,需要先安装ntp服务器yum install ntp
4、/sbin/hwclock --systohc #设置硬件时间和系统时间一致并校准
linux系统时间和计算机硬件时间都是cst时间了,并且为上海时区。