linux校验时间

时间:2023-12-28 16:03:08

一. 基本方法:
date -s "2018-09-02 04:23:45"
clock -w 把时间写入cmos(一个RAM芯片)

hwclock 查看硬件时间

hwclock -w :将系统时间写入硬件时间
hwclock --hctosys: 将系统时间调整为和硬件时间一样
hwclock --systohc: 将硬件时间调整为和系统时间一样

例如:在设置中国时区使用亚洲/上海(+8)

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

将UTC时间改为CST格式

/etc/profile末尾添加一行

export TZ='CST-8'
然后执行 source /etc/profile 使文件生效

或者  dpkg-reconfigure tzdata

二. 其他方法:
1. Linux中有个ntp包可以自动校准时间,用来同步网络时间
Debian系统安装NTP校时包:
   apt-get install ntpdate
CentOS系统安装NTP校时包:
   yum install ntp
校时命令:
ntpdate cn.pool.ntp.org
如果想每隔一定时间自动校时,只需将上面的命令加入至Cron就行了:
   00 12 * * * /sbin/ntpdate cn.pool.ntp.org
* cn.pool.ntp.org是ntp网络授时组织的中国授时源,类似的还有ntpdate asia.pool.ntp.org 
 
2. 有时候方法一过段时间又不正常了,可以用下面的方法,快速校对Linux服务器时间至北京时间,参考http://blog.path8.net/archives/6397.html
服务器采用ntp更新时间,经常牵扯到UTC是否开启的问题,开启了时间就会快8个小时,前段时间朋友给我了下面的命令,一条命令解决之前的所有问题。
rdate -t  -s stdtime.gov.hk    #使用rdate将stdtime.gov.hk服务器的时间抓取回来
hwclock -w #将获得的时间写入硬件

rdate的命令使用方法介绍

  语  法:rdate [-ps][主机名称或IP地址...]
  补充说明:执行rdate指令,向其他主机询问系统时间并显示出来。
  参  数:
  -p  显示远端主机的日期与时间。
  -s  把从远端主机收到的日期和时间,回存到本地主机的系统时间。
  -t  Set timeout in seconds for every attempt to retreive date. ubuntu下没这个参数。