佐伊大叔 發表於 2025-3-22 00:00:00

CentOS系统中的时间日期设置全攻略

<p style="margin-left:0; margin-right:0">在过去的CentOS版本里,要设置时区的话要手动修改 /etc/locale .conf文件,很是麻烦,不过CentOS已经为我们准备好一个非常强大的工具了: localectl</p>
<p style="margin-left:0; margin-right:0"><strong>显示当前时区</strong><br />使用以下命令:</p>
<div class="dxycode"><pre class="brush:bash;">~]$ localectl status
System Locale: LANG=en_US.UTF-8
VC Keymap: us
X11 Layout: n/a</pre></div>
<p><br /><span style="font-family:tahoma,arial,">可以看到,除了显示了系统的时区信息以外,还显示了键盘信息和X11布局信息</span></p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0"><strong>列出所有的时区</strong><br />用以下命令显示所有的英文时区<br />&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">~]$ localectl list-locales | grep en_
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8</pre></div>
<p><br /><span style="font-family:tahoma,arial,">如果要显示中文的,只需要把grep en 改成grep zh就行了</span></p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0"><strong>设置本地时区</strong><br />使用Root执行以下命令:<br />&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">localectl set-locale LANG=locale</pre></div>
<p><br /><span style="font-family:tahoma,arial,">把最后的 locale 替换成具体的时区,比如zh_CN.UTF-8就可以设置了。</span></p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0">是不是很强大啊。</p>
<p style="margin-left:0; margin-right:0"><br /><strong>timedatectl</strong></p>
<p style="margin-left:0; margin-right:0">下面隆重来介绍一下timedatectl命令~<br />在以前的CentOS版本里,时间设置有 date , hwclock 等一系列命令,但是CentOS 7 开始,使用了一个统一的命令:timedatectl</p>
<p><br /><span style="font-family:tahoma,arial,">这个命令非常的强大,首先是直接使用可以显示当前的系统时间的一些信息:</span></p>
<p></p>
<div class="dxycode"><pre class="brush:bash;">~]$ timedatectl
Local time: Mon 2013-09-16 19:30:24 CEST
Universal time: Mon 2013-09-16 17:30:24 UTC
Timezone: Europe/Prague (CEST, +0200)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2013-03-31 01:59:59 CET
Sun 2013-03-31 03:00:00 CEST
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2013-10-27 02:59:59 CEST
Sun 2013-10-27 02:00:00 CET</pre></div>
<p style="margin-left:0; margin-right:0"><strong>设置当前日期:</strong><br />使用Root执行以下命令就可以了:timedatectl set-time YYYY-MM-DD</p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0"><strong>设置当前时间:</strong><br />依旧是要Root权限<br />&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">timedatectl set-time HH:MM:SS</pre></div>
<div class="msgheader" style="background:#f6fbff; border-color:#0099cc; border-style:solid; border-width:1px; clear:both; font-family:tahoma,arial,"></div>
<p><br /><span style="font-family:tahoma,arial,">默认的,系统是使用UTC时间的,可以用以下命令打开和关闭UTC时间:</span></p>
<div class="dxycode"><pre class="brush:bash;">timedatectl set-local-rtc boolean</pre></div>
<p><br /><span style="font-family:tahoma,arial,">把 boolean 替换成yes则表示使用本地时间,替换成no则表示是UTC时间</span></p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0"><strong>设置任意时区</strong><br />可以用以下命令查看所有的时区:<br />&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">timedatectl list-timezones</pre></div>
<p><span style="font-family:tahoma,arial,">然后用以下命令设置时区:</span></p>
<div class="dxycode"><pre class="brush:bash;">timedatectl set-timezone time_zone</pre></div>
<p><span style="font-family:tahoma,arial,">当然root权限是免不了的</span></p>
<p style="margin-left:0; margin-right:0"></p>
<p style="margin-left:0; margin-right:0"><strong>与远程NTP服务器同步</strong></p>
<p style="margin-left:0; margin-right:0">timedatectl还可以设置是否打开NTP选项</p>
<div class="dxycode"><pre class="brush:bash;">timedatectl set-ntp boolean</pre></div>
<p><span style="font-family:tahoma,arial,">同样的,这里的boolean是yes或者no</span></p>
頁: [1]
查看完整版本: CentOS系统中的时间日期设置全攻略