设置Linux系统服务器时间(时区、同步网络时间)
一、查看当前系统时间和时区
1.1 date命令查看时间
date
#显示当前系统时间
输出如下:
Mon Mar 1 10:22:13 CST 20211.2 timedatectl查看时区
timedatectl
#显示当前系统时区设置信息
输入出如下:
# timedatectl
Local time: Fri 2021-03-12 23:26:08 CST
Universal time: Fri 2021-03-12 15:26:08 UTC
RTC time: Fri 2021-03-12 23:26:08
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes将硬件时钟(RTC,实时时钟)设置为本地时间
timedatectl set-local-rtc 1
#使用 timedatectl set-local-rtc 1 命令后,系统会将硬件时钟配置为使用本地时间,而如果使用 timedatectl set-local-rtc 0,则会将其配置为使用UTC时间二、设置时区
2.1 使用tzselect选择时区
tzselect是一个交互式命令,根据提示依次选择洲、国家、城市。
输出如下:
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
#这里可以看到,tzselect命令提供了使用timedatectl命令设置时区的配置:Asia/Shanghai。
#这个值会在接下来设置时区时用到。2.2 使用timedatectl命令设置时区
timedatectl set-timezone 'Asia/Shanghai'
#设置当前时区为亚洲/上海。2.3 通过配置文件修改时区
/usr/share/zoneinfo/Asia/Shanghai
#系统内的时区配置文件目录
rm -f /etc/localtime
#先删除当前的时区配置文件
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#建立软连接到/etc/localtime三、同步网络时间
即便时区设置正确,若时间未与网络同步,则仍然存在时间不正确的可能。
3.1 Chronyd网络时间同步方式
CentOS 7中默认使用的时间同步服务为Chronyd。
其配置文件为/etc/chrony.conf,其中配置了默认的时间服务器。
确保chronyd服务为启动状态,并开机启动。
#阿里云时间同步服务器地址
server ntp.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp10.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp11.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp12.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp2.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp2.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp3.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp3.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp4.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp4.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp5.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp5.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp6.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp6.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp7.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp8.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp9.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst立即手动同步
chronyc makestep
#chrony客户端,立即进行同步
输出如下:
200 OK
#成功同步查看各校时服务器的同步信息
chronyc sources查看在线的校时服务器
chronyc activity3.2 NTP时间同步服务
安装ntp服务
yum install -y ntp
#通过yum安装ntp服务手动NTP时间同步
ntpdate ntp1.aliyun.com
#即使不启动NTP服务,也可以使用NTP同步命令。定时任务同步系统时间
*/10 * * * * /usr/sbin/ntpdate ntp1.aliyun.com
每10分钟从阿里云时间服务器同步一次时间四、关于硬件时间和系统时间
4.1 硬件时间与系统时间的区别
硬件时间为计算机硬件时钟芯片中的时间。
系统时间为Linux Kernel中的时间。
4.2 使用hwclock命令查看硬件时间
hwclock
#使用hwclock命令
输出如下:
2021-03-12 23:59:14.840710+08:004.3 设置系统时间与硬件时间
设置系统时间
date --set '03/13/21 12:00:00'
#将系统时间设置为2021年3月13日 12时0分0秒设置硬件时间
hwclock --set --date='01/01/24 17:30'
#将硬件时间设置为2024年1月1日 17时30分
#命令格式与设置系统时间略有不同4.4 同步系统时间至硬件时间
hwclock --systohc
#将系统时间同步到硬件时间五、局域网内部时间同步
主机无法访问公网,或只有一台可以访问公网。
使用能够访问公网的这台主机为局域网内的其他主机提供校时。
5.1 服务器配置
编辑配置文件
vim /etc/chrony.conf
#服务器使用公网校时
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
#取消注释,允许内网网段进行访问
allow 10.10.10.0/24重启服chronyd服务
systemctl restart chronyd5.2 客户端配置
编辑配置文件
vim /etc/chrony.conf
#服务器使用内网的校时服务器,填写服务端的ip地址或域名。
server 10.10.10.20 iburst重启服chronyd服务
systemctl restart chronyd重启后会自动校时。或使用本文上面提到的手动校时命令。
建议添加定时任务,自动校时。