欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

centOS 7 时间相关

程序员文章站 2022-04-30 11:30:03
...

http://blog.csdn.net/taokai_110/article/details/73162891?utm_source=itdadao&utm_medium=referral (Centos7 修改硬件时间和系统时间)

http://www.cnblogs.com/rusking/p/7634412.html (Centos7时间服务器安装配置)

http://www.cnblogs.com/Csir/p/6912527.html ( CentOS7搭建时间服务器-chrony)

 

在CentOS 6版本,时间设置有date、hwclock命令,

 

硬件时钟和系统时钟

 

(1) 硬件时钟

 

RTC(Real-Time Clock)或CMOS时钟,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。

 

查看系统时间

[root@localhost ~]# date

Thu Sep 28 17:05:58 CST 2017

 

查看硬件时间

[root@localhost ~]# hwclock --show

Thu 28 Sep 2017 05:42:10 PM CST  -1.026550 seconds

 

(2) 系统时钟

 

一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

 

从CentOS 7开始,使用了一个新的命令timedatectl

 

timedatectl 命令

 

(1) 读取时间

 

timedatectl //等同于 timedatectl status

 

(2) 设置时间

 

timedatectl set-time "YYYY-MM-DD HH:MM:SS"

 

(3) 列出所有时区

 

timedatectl list-timezones

 

(4) 设置时区

 

timedatectl set-timezone Asia/Shanghai

 

(5) 是否NTP服务器同步

 

timedatectl set-ntp yes //yes或者no

 

(6) 将硬件时钟调整为与本地时钟一致

 

timedatectl set-local-rtc 1

hwclock --systohc --localtime //与上面命令效果一致

 

注意: 硬件时钟默认使用UTC时间,因为硬件时钟不能保存时区和夏令时调整,修改后就无法从硬件时钟中读取出准确标准时间,因此不建议修改。修改后系统会出现下面的警告。

 

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) 指在夏天太阳升起的比较早时,将时钟拨快一小时,以提早日光的使用。(中国不使用)

 

GMT + 8 = UTC + 8 = CST

 

 

 二、时间服务器的搭建

 1、 服务器端

centos7下首先确认服务器的防火墙、selinux关闭状态

第一步  为服务器和客户机安装ntp   ntpdate   --默认已安装

# yum install ntp ntpdate -y

第二步 查找时间同步服务器

http://www.pool.ntp.org/zone/asia

第三步  编辑 /etc/ntp.conf

#server time.windows.com

#server s2m.time.edu.cn

server 0.asia.pool.ntp.org

server 1.asia.pool.ntp.org

server 2.asia.pool.ntp.org

server 3.asia.pool.ntp.org

server 127.127.1.0 iburst  local clock 当外部时间不可用时,使用本地时间。

restrict 192.168.137.1 mask 255.255.255.0 nomodify  允许更新的IP地址段

第四步  启动ntp服务

systemctl start ntpd

systemctl enable ntpd.service 设置开机启动服务

第五步  验证服务

# ntpq -p

# date -R

2、客户端

第六步  远程客户端时间同步测试

 # date

 # ntpdate 服务器IP

第七步 客户端设置计划任务,每天晚上1点同步时间

crontab -e

00 01 * * * root /usr/sbin/ntpdate 服务IP; /sbin/hwclock -w

格式 00 01 * * *  五个字符表示  分 时 日 月 年