linux小竅門──設定時鐘和時間

來源:互聯網
上載者:User
 本文檔解釋了如何從linux下設定電腦的時鐘,如何設定您的時區和其它與linux如何儲存時間相關的材料。

    您的電腦有兩個時鐘,一個是始終啟動並執行、由電池供電的( 硬體的、BIOS或CMOS )時鐘,另一個是由運行在您的電腦上的作業系統維護的( 系統 )時鐘。硬體時鐘通常只在作業系統啟動時用來設定系統時鐘,然後直到重啟或關閉系統,由系統時鐘來記錄時間。在Linux 系統中,您可以選擇用UTC/GMT 時間或本地時間來記錄硬體時鐘。推薦的選項是用UTC 記錄,因為夏令時可以自動記錄。使用UTC 記錄硬體時鐘的唯一不足是,如果您使用雙系統,其它作業系統,如DOS 要求硬體時鐘用本地時間設定,那麼在那個作業系統裡時間將是錯誤的。

    設定時區:

    Linux 下的時區是通過建立從/etc/localtime[1] 到/usr/share/zoneinfo [2] 目錄下與您所在時區相符的檔案的符號鏈結實現的。例如,由於我在南澳大利亞,/etc/localtime就是到/usr/share/zoneinfo/Australia/South的符號鏈結。要建立這個鏈結,運行:

    ln -sf ../usr/share/zoneinfo/your/zone /etc/localtime

    替換your/zone 為形如Australia/NSW或Australia/Perth 的檔案。看看/usr/share/zoneinfo目錄都有什麼時區。

    [1] 這裡假設/usr/share/zoneinfo 是到/etc/localtime 的鏈結的前提是Redhat Linux

    [2] 在舊版本的系統裡,您會發現使用/usr/lib/zoneinfo而不是/usr/share/zoneinfo。參考後面“一些應用程式中時間錯誤”。

    設定UTC 或本地時間:

    當Linux 啟動時,一個啟動指令碼運行/sbin/hwclock 程式複製當前硬體時鐘時間到系統時鐘。hwclock 假定硬體時鐘設定為本地時間,除非它使用了--utc 參數。在RedHat Linux下您不是編輯啟動指令碼,而是編輯/etc/sysconfig/clock 檔案,相應的改變UTC 一行為UTC=true或UTC=false。

    設定系統時鐘:

    在Linux 下設定系統時鐘使用date命令。例如,設定目前時間和日期為July 31,11:16pm,運行date 07312316 ( 注意這裡的時間是24小時制) ;如果您想設定年份為1998,

    應該運行date 073123161998 ;要是也想設定秒,運行date 07312316.30或date 073123161998.30。要查看Linux 當前本地時間,使用date,不帶參數。

    設定硬體時鐘:

    要設定硬體時鐘,我喜歡的方式是首先設定系統時鐘,然後設定硬體時鐘為當前系統時鐘時間,使用命令/sbin/hwclock --systohc (或 /sbin/hwclock --systohc --utc ) ,如果您使用UTC 儲存硬體時鐘) 。要查看當前硬體時鐘的設定,不帶參數運行hwclock 。如果硬體時鐘是UTC 儲存,而您想看相應的本地時間,運行/sbin/hwclock --utc 。

    一些應用程式中時間錯誤:

    如果一些應用程式,如date顯示了正確的時間,而另一些則錯誤,而您運行著RedHat Linux 5.0/5.1,您很可能遇到了一個由於將時區資訊從/usr/lib/zoneinfo 移動到/usr/share/zoneinfo 引起的bug 。修複的方法是建立一個從/usr/lib/zoneinfo 到/usr/share/zoneinfo 的符號鏈結:

    ln -s ../share/zoneinfo /usr/lib/zoneinfo 。

    小結:

    */etc/sysconfig/clock 設定硬體時鐘,無論是用UTC 儲存還是用本地時間儲存
    *建立/etc/localtime到/usr/share/zoneinfo/...的符號鏈結來設定時區
    *運行date MMDDhhmm 來設定當前系統日期/ 時間
    *運行/sbin/hwclock --systohc [--utc]來設定硬體時鐘

    其它有趣的註解:

    Linux kernel總是按照從UTC 時間1970年1 月1 日午夜開始的秒數來儲存和計算時間,無論您的硬體時鐘是否用UTC 儲存。轉換到本地時間的工作是運行時完成的。這樣做的一個妙處是,如果某人從不同的時區使用您的電腦,他可以設定時區環境變數,所有的日期和時間可以按他的時區正確顯示。

    如果自UTC 1972年1 月1 日開始的秒數用儲存為帶加號或減號32位整數,如同在您的Linux/Intel 系統上一樣,您的時鐘將在2038年停止工作。Linux 沒有Y2K 問題,但是確實存在2038年的問題。令人期望的是,那時我們都會使用64位系統來運行Linux 了。64位整數將使我們的時鐘一直運行到大約2922.71億年。

    其它值得一看的程式:

    * rdate ──從遠程機器獲得目前時間;可以用來設定系統時間
    * xntpd ──類似rdata ,但是它是相當精確的,並且您需要有永久的網路連結xntpd 持續地運行,記錄網路延時、時鐘漂移等事件但是也有一個程式( ntpdate ) 包括在內,像rdate 一樣設定目前時間。

    相關資訊:

    * date(1)
    * hwclock(8)
    * /usr/doc/HOWTO/mini/Clock

    英文原文:

    http://www.linuxsa.org.au/tips/time.html

    引用:
    Linux Tips
    Linux, Clocks, and Time

    Introduction

    This document explains how to set your computer's clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping.

    Your computer has two timepieces; a battery-backed one that is always running (the ``hardware'', ``BIOS'', or ``CMOS'' clock), and another that is maintained by the operating system currently running on your computer (the ``system'' clock). The hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one used to keep track of time.

    On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time. The preferred option is to keep it in UTC because then daylight savings can be automatically accounted for. The only disadvantage with keeping the hardware clock in UTC is that if you dual boot with an operating system (such as DOS) that expects the hardware clock to be set to local time, the time will always be wrong in that operating system.
    Setting your timezone

    The timezone under Linux is set by a symbolic link from /etc/localtime[1] to a file in the /usr/share/zoneinfo[2] directory that corresponds with what timezone you are in. For example, since I'm in South Australia, /etc/localtime is a symlink to /usr/share/zoneinfo/Australia/South. To set this link, type:

    ln -sf ../usr/share/zoneinfo/your/zone /etc/localtime

    Replace your/zone with something like Australia/NSW or Australia/Perth. Have a look in the directories under /usr/share/zoneinfo to see what timezones are available.

    [1] This assumes that /usr/share/zoneinfo is linked to /etc/localtime as it is under Red Hat Linux.

    [2] On older systems, you'll find that /usr/lib/zoneinfo is used instead of /usr/share/zoneinfo. See also the later section ``The time in some applications is wrong''.
    Setting UTC or local time

    When Linux boots, one of the initialisation scripts will run the /sbin/hwclock program to copy the current hardware clock time to the system clock. hwclock will assume the hardware clock is set to local time unless it is run with the --utc switch. Rather than editing the startup script, under Red Hat Linux you should edit the /etc/sysconfig/clock file and change the ``UTC'' line to either ``UTC=true'' or ``UTC=false'' as appropriate.
    Setting the system clock

    To set the system clock under Linux, use the date command. As an example, to set the current time and date to July 31, 11:16pm, type ``date 07312316'' (note that the time is given in 24 hour notation). If you wanted to change the year as well, you could type ``date 073123161998''. To set the seconds as well, type ``date 07312316.30'' or ``date 073123161998.30''. To see what Linux thinks the current local time is, run date with no arguments.
    Setting the hardware clock

    To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ``/sbin/hwclock --systohc'' (or ``/sbin/hwclock --systohc --utc'' if you are keeping the hardware clock in UTC). To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and you want to see the local equivalent, type ``/sbin/hwclock --utc''
    The time in some applications is wrong

    If some applications (such as date) display the correct time, but others don't, and you are running Red Hat Linux 5.0 or 5.1, you most likely have run into a bug caused by a move of the timezone information from /usr/lib/zoneinfo to /usr/share/zoneinfo. The fix is to create a symbolic link from /usr/lib/zoneinfo to /usr/share/zoneinfo: ``ln -s ../share/zoneinfo /usr/lib/zoneinfo''.
    Summary

    * /etc/sysconfig/clock sets whether the hardware clock is stored as UTC or local time.

    * Symlink /etc/localtime to /usr/share/zoneinfo/... to set your timezone.

    * Run ``date MMDDhhmm'' to set the current system date/time.

    * Type ``/sbin/hwclock --systohc [--utc]'' to set the hardware clock.

    Other interesting notes

    The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not. Conversions to your local time are done at run-time. One neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone.

    If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system), your clock will stop working sometime on the year 2038. Linux has no inherent Y2K problem, but it does have a year 2038 problem. Hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.
    Other programs worth looking at

    * rdate - get the current time from a remote machine; can be used to set the system time.

    * xntpd - like rdate, but it's extremely accurate and you need a permanent 'net connection. xntpd runs continuously and accounts for things like network delay and clock drift, but there's also a program (ntpdate) included that just sets the current time like rdate does.

    Further information

    * date(1)

    * hwclock(8)

    * /usr/doc/HOWTO/mini/Clock

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.