修改linux服务器时区及硬件时间

每次在date查看时时间是北京时间 可是有时候服务提供打印的时间却不准 如果是客户打款过来 在财务对账时会显示时间错误导致出现问题  是因为linux有两个时间 date和hwclock 软件和硬件时间

一般自己安装的linxu系统 在安装时会选择时区  选择后不会出现这种情况 但是云服务器一般都是时间不对的  这里主要以阿里云来为例。

在网上比较多的两种更改硬件时间的方法我都试过 不知道为什么我是无法修改的 现在贴上这俩种大众方式 仅供参考(因为我使用这两种方式没成功过)

修改 ECS 实例时区

此处以 Centos 6.5 实例为例,列举两种修改时区的方法,您可以选其中任意一种方法。

注意:修改时区后,请切记您需要运行 hwclock -w 更新实例硬件时钟。

方法 1. 通过命令 tzselect 修改时区

  1. 远程连接 Linux 实例。

  2. 执行命令 tzselect

    
    
    1. $ tzselect
    2. Please identify a location so that time zone rules can be set correctly.
    3. Please select a continent or ocean.
    4. 1) Africa
    5. 2) Americas
    6. 3) Antarctica
    7. 4) Arctic Ocean
    8. 5) Asia
    9. 6) Atlantic Ocean
    10. 7) Australia
    11. 8) Europe
    12. 9) Indian Ocean
    13. 10) Pacific Ocean
    14. 11) none - I want to specify the time zone using the Posix TZ format.
  3. 输入上述洲际列表中的数字,如本示例中的 5

    
    
    1. #? 5 # 输入 5,选择亚洲。
    2. Please select a country.
    3. 1) Afghanistan 18) Israel 35) Palestine
    4. 2) Armenia 19) Japan 36) Philippines
    5. 3) Azerbaijan 20) Jordan 37) Qatar
    6. 4) Bahrain 21) Kazakhstan 38) Russia
    7. 5) Bangladesh 22) Korea (North) 39) Saudi Arabia
    8. 6) Bhutan 23) Korea (South) 40) Singapore
    9. 7) Brunei 24) Kuwait 41) Sri Lanka
    10. 8) Cambodia 25) Kyrgyzstan 42) Syria
    11. 9) China 26) Laos 43) Taiwan
    12. 10) Cyprus 27) Lebanon 44) Tajikistan
    13. 11) East Timor 28) Macau 45) Thailand
    14. 12) Georgia 29) Malaysia 46) Turkmenistan
    15. 13) Hong Kong 30) Mongolia 47) United Arab Emirates
    16. 14) India 31) Myanmar (Burma) 48) Uzbekistan
    17. 15) Indonesia 32) Nepal 49) Vietnam
    18. 16) Iran 33) Oman 50) Yemen
    19. 17) Iraq 34) Pakistan
  4. 输入上述国家列表中的数字,如本示例中的 9

    
    
    1. #? 9 # 输入 9,选择中国。
    2. Please select one of the following time zone regions.
    3. 1) east China - Beijing, Guangdong, Shanghai, etc.
    4. 2) Heilongjiang (except Mohe), Jilin
    5. 3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.
    6. 4) most of Tibet & Xinjiang
    7. 5) west Tibet & Xinjiang
  5. 输入上述城市列表中的数字,如本示例中的 1。并输入确认信息 Yes/No,如本示例中的 1

    
    
    1. #? 1 # 输入 1,选择北京时间。
    2. The following information has been given:
    3. China
    4. east China - Beijing, Guangdong, Shanghai, etc.
    5. Therefore TZ='Asia/Shanghai' will be used.
    6. Local time is now: Mon Nov 9 13:40:51 CST 2015.
    7. Universal Time is now: Mon Nov 9 05:40:51 UTC 2015.
    8. Is the above information OK?
    9. 1) Yes
    10. 2) No
    11. #? 1 # 输入 1 确认。
    12. You can make this change permanent for yourself by appending the line
    13. TZ='Asia/Shanghai'; export TZ
    14. to the file '.profile' in your home directory; then log out and log in again.
    15. Here is that TZ value again, this time on standard output so that you
    16. can use the /usr/bin/tzselect command in shell scripts:
    17. Asia/Shanghai
  6. 执行命令 hwclock -w 更新硬件时钟(RTC)。

您的 Linux 实例时区已设置为 Asia/Shanghai

方法 2. 通过修改配置文件修改时区

时区配置文件需要以 root 身份打开并编辑,所以此处使用 sudo 命令。

  1. 远程连接 Linux 实例。

  2. 执行命令 sudo rm /etc/localtime 删除系统里的当地时间链接。

  3. 执行命令 sudo vi /etc/sysconfig/clock 用 vim 打开并编辑配置文件 /etc/sysconfig/clock。

  4. 输入 i 添加时区城市,例如添加 Zone=Asia/Shanghai,按下 Esc 键退出编辑并输入 :wq 保存并退出。(可执行命令 ls /usr/share/zoneinfo 查询时区列表,Shanghai 为列表条目之一。)

  5. 执行命令 sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai/etc/localtime 更新时区修改内容。

  6. 执行命令 hwclock -w 更新硬件时钟(RTC)。

  7. 执行命令 sudo reboot 重启实例。

  8. 执行命令 date -R 查看时区信息是否生效,未生效可重走一遍步骤。


    下来贴上我使用成功的方式:

手动修改/etc/locale.gen文件,添加新的locale,然后执行locale-gen,即可生成新的locale,再 通过设置上面说的LC_*变量就可设置系统的locale了。下是我的locale.gen文件(直接复制下列这些即可):

zh_CN GB2312

en_GB ISO-8859-1

en_GB.ISO-8859-15 ISO-8859-15

en_GB.UTF-8 UTF-8

en_US ISO-8859-1

en_US.ISO-8859-15 ISO-8859-15

en_US.UTF-8 UTF-8

zh_CN.GB18030 GB18030

zh_CN.GBK GBK

zh_CN.UTF-8 UTF-8

zh_HK BIG5-HKSCS

zh_HK.UTF-8 UTF-8

zh_TW BIG5

zh_TW.EUC-TW EUC-TW

zh_TW.UTF-8 UTF-8
复制粘贴后 在使用 hwclock -w 同步时间即可

如果没有通过 请删掉本地时间文件rm -rf /etc/localtime   然后在hwclock -w
如果还不对  看看date是否时北京时间 如果不是就date -s 改为正确时间 再hwclock -w即可

你可能感兴趣的:(基本攻略)