阅读量:1
问题
Linux服务器上获取时间总是和本地电脑windows系统时间相差8小时。
解决
# 原因:由于时区不同,校准时间后导致服务器时间与实际时间之前相差8小时,给出如下解决方案 # 第一步:在 /etc/profile 文件中增加一行 export TZ='CST-8' vim /etc/profile export TZ='CST-8' #第二步:使文件立即生效:source /etc/profile 或者 . /etc/profile source /etc/profile # 第三步:同步网络时间:ntpdate time.nist.gov ntpdate time.nist.gov # 第四步:同步成功后调整硬件时间:hwclock -w hwclock -w # 第五步:最后,可以date一下,看时间是否准确? date
原因
硬件时钟不与UTC时间一致问题,时区不一样所引起的。
CST应该是指(China Shanghai Time,东八区时间) UTC应该是指(Coordinated Universal Time,标准时间) 所以,这2个时间实际上应该相差8个小时。
详细
用ntpdate命令来让时间同步
# 1 安装 ntpdate apt-get -y install ntpdate # 2 执行ntpdate时间同步命令 ntpdate 0.asia.pool.ntp.org # 如果连不上服务器 可以用下面 阿里 ntpdate ntp1.aliyun.com # 查看时间 date # 或者 timedatectl
改硬件时间
# 查看硬件的时间 hwclock --show # 设置硬件时间 hwclock --set --date '2023-12-12 12:12:12' # 设置 系统时间和硬件时间同步 hwclock --hctosys # 设置 系统时间和硬件时间保持一致 hwclock -s
重设系统时区
方法一:当地时间替换为上海
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo 'Asia/Shanghai' >/etc/timezone # 或者 # 第一步:删除/etc/localtime(最好做备份,不要删除) rm -rf /etc/localtime # 第二步:复制上海时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/locatime # 注意:需要重启服务器才会生效
方法二:环境变量/etc/profile
# 1 在 /etc/profile 文件中增加一行: export TZ='CST-8' # 2 生效 source /etc/profile