zoukankan      html  css  js  c++  java
  • Linux如何修改和查询时区时间

    Linux如何修改和查询时区时间

      我在日常工作中,最近遇到了在解压源码包的时候,提示时间比较旧,解压安装出现问题。原因是,租用的vps所在时区和自己所需要的时区不一致,于是在网上找了相关资料。并亲自实践,将其记录如下,以供日后方便使用。

    一、时区

    参考资料:http://www.cnblogs.com/h2appy/archive/2008/11/27/1342029.html

    # 当前操作系统版本

    [root@erwtd ~]# cat /etc/redhat-release

    CentOS release 6.9 (Final)

    # 1、查看当前时区:东9

    [root@erwtd ~]# date -R
    
    Mon, 11 Dec 2017 12:16:01 +0900
    
    [root@erwtd ~]# 
    View Code

    # 2、修改时区

    [root@erwtd ~]# tzselect
    
    Please identify a location so that time zone rules can be set correctly.
    
    Please select a continent or ocean.
    
     1) Africa
    
     2) Americas
    
     3) Antarctica
    
     4) Arctic Ocean
    
     5) Asia
    
     6) Atlantic Ocean
    
     7) Australia
    
     8) Europe
    
     9) Indian Ocean
    
    10) Pacific Ocean
    
    11) none - I want to specify the time zone using the Posix TZ format.
    
    #? 5
    
    Please select a country.
    
     1) Afghanistan           18) Israel                35) Palestine
    
     2) Armenia               19) Japan                 36) Philippines
    
     3) Azerbaijan            20) Jordan                37) Qatar
    
     4) Bahrain               21) Kazakhstan            38) Russia
    
     5) Bangladesh            22) Korea (North)         39) Saudi Arabia
    
     6) Bhutan                23) Korea (South)         40) Singapore
    
     7) Brunei                24) Kuwait                41) Sri Lanka
    
     8) Cambodia              25) Kyrgyzstan            42) Syria
    
     9) China                 26) Laos                  43) Taiwan
    
    10) Cyprus                27) Lebanon               44) Tajikistan
    
    11) East Timor            28) Macau                 45) Thailand
    
    12) Georgia               29) Malaysia              46) Turkmenistan
    
    13) Hong Kong             30) Mongolia              47) United Arab Emirates
    
    14) India                 31) Myanmar (Burma)       48) Uzbekistan
    
    15) Indonesia             32) Nepal                 49) Vietnam
    
    16) Iran                  33) Oman                  50) Yemen
    
    17) Iraq                  34) Pakistan
    
    #? 9
    
    Please select one of the following time zone regions.
    
    1) Beijing Time
    
    2) Xinjiang Time
    
    #? 1
    
     
    
    The following information has been given:
    
     
    
            China
    
            Beijing Time
    
     
    
    Therefore TZ='Asia/Shanghai' will be used.
    
    Local time is now:      Mon Dec 11 11:21:25 CST 2017.
    
    Universal Time is now:  Mon Dec 11 03:21:25 UTC 2017.
    
    Is the above information OK?
    
    1) Yes
    
    2) No
    
    #? 1
    
     
    
    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
    
    [root@erwtd ~]# 
    View Code

    #3复制相应的时区文件,替换系统时区文件

    # cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime
    
    #在中国可以使用:
    
    [root@erwtd ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
    View Code

    一、时间

    #1、查看当前时间

    [root@erwtd ~]# date
    
    Mon Dec 11 11:28:19 CST 2017
    
    [root@erwtd ~]#
    View Code

    #2、设置时间和日期

    [root@erwtd ~]# date -s "20171211 11:29:30"
    Mon Dec 11 11:29:30 CST 2017
    [root@erwtd ~]# 
    #或者单独修改时间
    [root@erwtd ~]# date  -s 20171220
    Wed Dec 20 00:00:00 CST 2017
    [root@erwtd ~]# date -s 11:30:30
    Wed Dec 20 11:30:30 CST 2017
    [root@erwtd ~]# date
    Wed Dec 20 11:30:34 CST 2017
    [root@erwtd ~]# 
    View Code

    #3、保存设置

    [root@erwtd ~]# hwclock  -w
    View Code

    二、时间同步shell脚本

    [root@erwtd ~]#cat /root/ntp.sh 
    #!/bin/bash
    # ntp.sh
    #NTP服务器数组列表
    ntpServer=(
    [0]=1.cn.pool.ntp.org
    [1]=2.cn.pool.ntp.org
    [2]=3.cn.pool.ntp.org
    [3]=0.cn.pool.ntp.org
    )
    
    #校验#
    serverNum=0
    NUM=0
    for ((i=0; i<=$serverNum; i++)); do
        echo -n "正在和NTP服务器:${ntpServer[$NUM]}校验中..."
        /usr/sbin/ntpdate ${ntpServer[$NUM]} >> /dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo -e "e[1;32m	[成功]e[0m"
            echo -e "e[1;32m同步成功,退出......e[0m"
            break
        else
            echo -e "e[1;31m	[失败]e[0m"
            echo -e "e[1;31m继续同步下一个!!!!!e[0m"
            let NUM++
        fi
        sleep 2
    done
    [root@erwtd ~]#
    [root@erwtd ~]# sh /root/ntp.sh 
    正在和NTP服务器:tw.pool.ntp.org校验中...        [成功]
    同步成功,退出......
    [root@erwtd ~]# 
    View Code
  • 相关阅读:
    《Head First》 MVC运用的设计模式
    unity工具 Animator的使用
    服务器搭建 如果搭建KBE开源服务器
    unity 实战图片挖洞Mask(转载)
    unity博客 推荐(不断补充)
    unity实战 UGUI英雄联盟英雄头顶分段式血条
    unity组成 ToLua
    unity实战 UGUI Text 间距和ContentSizeFitter组件的适配
    unity工具 推荐(不断补充)
    各种单例模式的对比分析
  • 原文地址:https://www.cnblogs.com/bjx2020/p/8028161.html
Copyright © 2011-2022 走看看