日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux小窍门──设置时钟和时间

發(fā)布時(shí)間:2025/7/14 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux小窍门──设置时钟和时间 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文檔解釋了如何從linux下設(shè)置計(jì)算機(jī)的時(shí)鐘,如何設(shè)置您的時(shí)區(qū)和其它與linux如何保存時(shí)間相關(guān)的材料。

??? 您的計(jì)算機(jī)有兩個(gè)時(shí)鐘,一個(gè)是始終運(yùn)行的、由電池供電的( 硬件的、BIOS或CMOS )時(shí)鐘,另一個(gè)是由運(yùn)行在您的計(jì)算機(jī)上的操作系統(tǒng)維護(hù)的( 系統(tǒng) )時(shí)鐘。硬件時(shí)鐘通常只在操作系統(tǒng)啟動(dòng)時(shí)用來(lái)設(shè)置系統(tǒng)時(shí)鐘,然后直到重啟或關(guān)閉系統(tǒng),由系統(tǒng)時(shí)鐘來(lái)記錄時(shí)間。在Linux 系統(tǒng)中,您可以選擇用UTC/GMT 時(shí)間或本地時(shí)間來(lái)記錄硬件時(shí)鐘。推薦的選項(xiàng)是用UTC 記錄,因?yàn)橄牧顣r(shí)可以自動(dòng)記錄。使用UTC 記錄硬件時(shí)鐘的唯一不足是,如果您使用雙系統(tǒng),其它操作系統(tǒng),如DOS 要求硬件時(shí)鐘用本地時(shí)間設(shè)置,那么在那個(gè)操作系統(tǒng)里時(shí)間將是錯(cuò)誤的。

??? 設(shè)置時(shí)區(qū):

??? Linux 下的時(shí)區(qū)是通過(guò)建立從/etc/localtime[1] 到/usr/share/zoneinfo [2] 目錄下與您所在時(shí)區(qū)相符的文件的符號(hào)鏈結(jié)實(shí)現(xiàn)的。例如,由于我在南澳大利亞,/etc/localtime就是到/usr/share/zoneinfo/Australia/South的符號(hào)鏈結(jié)。要建立這個(gè)鏈結(jié),運(yùn)行:

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

??? 替換your/zone 為形如Australia/NSW或Australia/Perth 的文件。看看/usr/share/zoneinfo目錄都有什么時(shí)區(qū)。

??? [1] 這里假設(shè)/usr/share/zoneinfo 是到/etc/localtime 的鏈結(jié)的前提是Redhat Linux

??? [2] 在舊版本的系統(tǒng)里,您會(huì)發(fā)現(xiàn)使用/usr/lib/zoneinfo而不是/usr/share/zoneinfo。參考后面“一些應(yīng)用程序中時(shí)間錯(cuò)誤”。

??? 設(shè)置UTC 或本地時(shí)間:

??? 當(dāng)Linux 啟動(dòng)時(shí),一個(gè)啟動(dòng)腳本運(yùn)行/sbin/hwclock 程序復(fù)制當(dāng)前硬件時(shí)鐘時(shí)間到系統(tǒng)時(shí)鐘。hwclock 假定硬件時(shí)鐘設(shè)置為本地時(shí)間,除非它使用了--utc 參數(shù)。在RedHat Linux下您不是編輯啟動(dòng)腳本,而是編輯/etc/sysconfig/clock 文件,相應(yīng)的改變UTC 一行為UTC=true或UTC=false。

??? 設(shè)置系統(tǒng)時(shí)鐘:

??? 在Linux 下設(shè)置系統(tǒng)時(shí)鐘使用date命令。例如,設(shè)置當(dāng)前時(shí)間和日期為July 31,11:16pm,運(yùn)行date 07312316 ( 注意這里的時(shí)間是24小時(shí)制) ;如果您想設(shè)置年份為1998,

??? 應(yīng)該運(yùn)行date 073123161998 ;要是也想設(shè)置秒,運(yùn)行date 07312316.30或date 073123161998.30。要查看Linux 當(dāng)前本地時(shí)間,使用date,不帶參數(shù)。

??? 設(shè)置硬件時(shí)鐘:

??? 要設(shè)置硬件時(shí)鐘,我喜歡的方式是首先設(shè)置系統(tǒng)時(shí)鐘,然后設(shè)置硬件時(shí)鐘為當(dāng)前系統(tǒng)時(shí)鐘時(shí)間,使用命令/sbin/hwclock --systohc (或 /sbin/hwclock --systohc --utc ) ,如果您使用UTC 保存硬件時(shí)鐘) 。要查看當(dāng)前硬件時(shí)鐘的設(shè)置,不帶參數(shù)運(yùn)行hwclock 。如果硬件時(shí)鐘是UTC 保存,而您想看相應(yīng)的本地時(shí)間,運(yùn)行/sbin/hwclock --utc 。

??? 一些應(yīng)用程序中時(shí)間錯(cuò)誤:

??? 如果一些應(yīng)用程序,如date顯示了正確的時(shí)間,而另一些則錯(cuò)誤,而您運(yùn)行著RedHat Linux 5.0/5.1,您很可能遇到了一個(gè)由于將時(shí)區(qū)信息從/usr/lib/zoneinfo 移動(dòng)到/usr/share/zoneinfo 引起的bug 。修復(fù)的方法是建立一個(gè)從/usr/lib/zoneinfo 到/usr/share/zoneinfo 的符號(hào)鏈結(jié):

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

??? 小結(jié):

??? */etc/sysconfig/clock 設(shè)置硬件時(shí)鐘,無(wú)論是用UTC 保存還是用本地時(shí)間保存
??? *建立/etc/localtime到/usr/share/zoneinfo/...的符號(hào)鏈結(jié)來(lái)設(shè)置時(shí)區(qū)
??? *運(yùn)行date MMDDhhmm 來(lái)設(shè)置當(dāng)前系統(tǒng)日期/ 時(shí)間
??? *運(yùn)行/sbin/hwclock --systohc [--utc]來(lái)設(shè)置硬件時(shí)鐘

??? 其它有趣的注解:

??? Linux kernel總是按照從UTC 時(shí)間1970年1 月1 日午夜開(kāi)始的秒數(shù)來(lái)儲(chǔ)存和計(jì)算時(shí)間,無(wú)論您的硬件時(shí)鐘是否用UTC 保存。轉(zhuǎn)換到本地時(shí)間的工作是運(yùn)行時(shí)完成的。這樣做的一個(gè)妙處是,如果某人從不同的時(shí)區(qū)使用您的計(jì)算機(jī),他可以設(shè)置時(shí)區(qū)環(huán)境變量,所有的日期和時(shí)間可以按他的時(shí)區(qū)正確顯示。

??? 如果自UTC 1972年1 月1 日開(kāi)始的秒數(shù)用保存為帶正負(fù)號(hào)32位整數(shù),如同在您的Linux/Intel 系統(tǒng)上一樣,您的時(shí)鐘將在2038年停止工作。Linux 沒(méi)有Y2K 問(wèn)題,但是確實(shí)存在2038年的問(wèn)題。令人期望的是,那時(shí)我們都會(huì)使用64位系統(tǒng)來(lái)運(yùn)行Linux 了。64位整數(shù)將使我們的時(shí)鐘一直運(yùn)行到大約2922.71億年。

??? 其它值得一看的程序:

??? * rdate ──從遠(yuǎn)程機(jī)器獲得當(dāng)前時(shí)間;可以用來(lái)設(shè)置系統(tǒng)時(shí)間
??? * xntpd ──類似rdata ,但是它是相當(dāng)精確的,并且您需要有永久的網(wǎng)絡(luò)連結(jié)xntpd 持續(xù)地運(yùn)行,記錄網(wǎng)絡(luò)延時(shí)、時(shí)鐘漂移等事件但是也有一個(gè)程序( ntpdate ) 包括在內(nèi),像rdate 一樣設(shè)置當(dāng)前時(shí)間。

??? 相關(guān)信息:

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

轉(zhuǎn)載于:https://www.cnblogs.com/cy163/archive/2008/05/18/1201996.html

總結(jié)

以上是生活随笔為你收集整理的linux小窍门──设置时钟和时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。