歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> linux小竅門──設置時鐘和時間

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

日期:2017/2/27 9:32:44   编辑:更多Linux

介紹:

本文檔解釋了如何從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



* /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





Copyright © Linux教程網 All Rights Reserved