Linux 实例时区与设置 NTP 服务

  • A+
所属分类:Linux
高性能企业级服务器首台5折

时区和时间的同步性对于云服务器很重要(例如您在更新数据库时,时间的准确性对业务的影响会非常大),为避免实例上运行的业务逻辑混乱和避免网络请求错误,您需要将一台或多台实例设置在同一时区下,比如 

1
Asia/Shanghai

 或 

1
America/Los Angeles

。目前,所有地域的阿里云 ECS 实例的默认时区为 CST(China Standard Time),您可以根据自己的业务需求并参照本文为 ECS 实例设置或者修改时区。此外,NTP(Network Time Protocol)服务能保证您的云服务器 ECS 的时间与标准时间同步,您可以根据本文配置 NTP 服务。

 

修改 ECS 实例时区

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

注意:修改时区后,请切记您需要运行 

1
hwclock -w

 更新实例硬件时钟。

方法 1. 通过命令 

1
tzselect

 修改时区

  1. 远程连接 Linux 实例。
  2. 执行命令 
    1
    tzselect

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

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

    1. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash">? 9  <span class="hljs-comment"># 输入 9,选择中国。</span></span></span>
    2. 1
       <span class="typ">Please</span> <span class="kwd">select</span><span class="pln"> one of the following time zone regions</span><span class="pun">.</span>
    3. 1
       <span class="lit">1</span><span class="pun">)</span><span class="pln"> east </span><span class="typ">China</span> <span class="pun">-</span> <span class="typ">Beijing</span><span class="pun">,</span> <span class="typ">Guangdong</span><span class="pun">,</span> <span class="typ">Shanghai</span><span class="pun">,</span><span class="pln"> etc</span><span class="pun">.</span>
    4. 1
       <span class="lit">2</span><span class="pun">)</span> <span class="typ">Heilongjiang</span> <span class="pun">(</span><span class="kwd">except</span> <span class="typ">Mohe</span><span class="pun">),</span> <span class="typ">Jilin</span>
    5. 1
       <span class="lit">3</span><span class="pun">)</span><span class="pln"> central </span><span class="typ">China</span> <span class="pun">-</span> <span class="typ">Sichuan</span><span class="pun">,</span> <span class="typ">Yunnan</span><span class="pun">,</span> <span class="typ">Guangxi</span><span class="pun">,</span> <span class="typ">Shaanxi</span><span class="pun">,</span> <span class="typ">Guizhou</span><span class="pun">,</span><span class="pln"> etc</span><span class="pun">.</span>
    6. 1
       <span class="lit">4</span><span class="pun">)</span><span class="pln"> most of </span><span class="typ">Tibet</span> <span class="pun">&amp;</span> <span class="typ">Xinjiang</span>
    7. 1
       <span class="lit">5</span><span class="pun">)</span><span class="pln"> west </span><span class="typ">Tibet</span> <span class="pun">&amp;</span> <span class="typ">Xinjiang</span>
  5. 输入上述城市列表中的数字,如本示例中的 
    1
    1

    。并输入确认信息 Yes/No,如本示例中的 

    1
    1

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

     更新硬件时钟(RTC)。

您的 Linux 实例时区已设置为 

1
Asia/Shanghai

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

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

1
sudo

 命令。

  1. 远程连接 Linux 实例。
  2. 执行命令 
    1
    sudo rm /etc/localtime

     删除系统里的当地时间链接。

  3. 执行命令 
    1
    sudo vi /etc/sysconfig/clock

     用 vim 打开并编辑配置文件 /etc/sysconfig/clock。

  4. 输入 
    1
    i

     添加时区城市,例如添加 

    1
    Zone=Asia/Shanghai

    ,按下 Esc 键退出编辑并输入 

    1
    :wq

     保存并退出。(可执行命令 

    1
    ls /usr/share/zoneinfo

     查询时区列表,

    1
    Shanghai

     为列表条目之一。)

  5. 执行命令 
    1
    sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai/etc/localtime

     更新时区修改内容。

  6. 执行命令 
    1
    hwclock -w

     更新硬件时钟(RTC)。

  7. 执行命令 
    1
    sudo reboot

     重启实例。

  8. 执行命令 
    1
    date -R

     查看时区信息是否生效,未生效可重走一遍步骤。

设置 NTP 服务器

Linux 系统有 

1
ntpd

 和 

1
ntpdate

 两种方式实现 NTP 时间同步,其中,

1
ntpd

 同步时间为步进式的逐渐调整时间,

1
ntpdate

 为断点更新。新购实例可使用 

1
ntpdate

 更新服务,已经运行业务的实例建议使用 

1
ntpd

 同步时间。此处提供标准 NTP 服务配置和 自定义 NTP 服务配置,您可以根据需要选择性地配置。关于更多 NTP 服务信息请参考 内网和公共 NTP 服务器

前提条件:NTP 服务的通信端口为 UDP 123,设置 NTP 服务之前请确保您已经打开 UDP 123 端口。您可以通过 

1
netstat -nupl

 查看实例是否开通 123 端口。您可以参考文档 添加安全组规则 放行 123 端口。

启用标准 NTP 服务

  1. 远程连接 Linux 实例。
  2. 执行命令 
    1
    sudo service ntpd start

     启用 NTP 服务。

  3. 执行命令 
    1
    chkconfig ntpd on

    ,启用 NTP 服务。

  4. 执行命令 
    1
    ntpstat

     查看是否启用了 NTP 服务。

  5. (可选)执行命令 
    1
    ntpq -p

     可查看 NTP 服务对等端的列表信息;执行命令 

    1
    sudo chkconfig --list ntpd

     可查看 NTP 服务的运行级别。

配置自定义 NTP 服务

  1. 远程连接 Linux 实例。
  2. 执行命令 
    1
    sudo vi /etc/ntp.conf

     用 vim 打开并编辑 NTP 服务配置文件。

  3. 找到 
    1
    server ntp 服务器 iburst

     的信息后,输入 

    1
    i

     开始编辑文件,给您暂时不需要的 NTP 服务器句首加上 

    1
    #

     隐藏起来。

  4. 新添加一行 NTP 服务器信息,格式为:
    1
    server 您需要添加的NTP服务器 iburst

    。完成编辑后按下 Esc 键并输入 

    1
    :wq

     保存退出。

  5. 执行命令 
    1
    sudo service ntpd start

     启用自定义的 NTP 服务。

  6. 执行命令 
    1
    chkconfig ntpd on

    ,启用 NTP 服务。

  7. 执行命令 
    1
    ntpstat

     查看是否启用了 NTP 服务。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: