學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

Linux系統(tǒng)自動(dòng)更新時(shí)間命令的詳細(xì)說明

時(shí)間: 佳洲1085 分享

  Linux系統(tǒng)可以通過命令實(shí)現(xiàn)自動(dòng)更新時(shí)間的功能。下面由學(xué)習(xí)啦小編為大家整理了Linux系統(tǒng)自動(dòng)更新時(shí)間命令的詳細(xì)說明,希望對大家有幫助!

  Linux系統(tǒng)自動(dòng)更新時(shí)間命令的詳細(xì)說明

  Linux系統(tǒng)自動(dòng)更新時(shí)間方法1

  與一個(gè)已知的時(shí)間服務(wù)器同步

  復(fù)制代碼代碼如下:

  ntpdate time.nist.gov

  其中 time.nist.gov 是一個(gè)時(shí)間服務(wù)器.

  刪除本地時(shí)間并設(shè)置時(shí)區(qū)為上海

  復(fù)制代碼代碼如下:

  rm -rf /etc/localtime

  ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  Linux系統(tǒng)自動(dòng)更新時(shí)間方法2

  linux自動(dòng)同步時(shí)間

  vi /etc/crontab

  加上一句:

  復(fù)制代碼代碼如下:

  00 0 1 * * root rdate -s time.nist.gov

  配置時(shí)間服務(wù)器配置(192.168.10.1)

  復(fù)制代碼代碼如下:

  1)、# rpm -ivh ntp-4.1.2-4.EL3.1.i386.rpm

  2)、# vi /etc/ntp.conf

  注釋一行:

  restrict default ignore

  加入一行:

  復(fù)制代碼代碼如下:

  restrict 192.168.10.0 mask 255.255.255.0 notrust nomodify notrap

  3)、# vi /etc/ntp/step-tickers

  加入一行:

  pool.ntp.org

  這樣每次ntpd啟動(dòng)時(shí),會(huì)自動(dòng)連接該國際標(biāo)準(zhǔn)時(shí)間服務(wù)器;

  4)、# service ntpd start

  5)、# netstat -an |grep 123

  確保該端口以udp方式開放

  時(shí)間客戶端配置(192.168.10.2)

  1)、# ntpdate 192.168.10.2

  應(yīng)該顯示同步成功

  2)、# crond -e

  加入

  復(fù)制代碼代碼如下:

  0-59/10 * * * * /usr/sbin/ntpdate 192.168.10.1

  每隔10分鐘同步一次時(shí)間

  補(bǔ)充:Linux系統(tǒng)設(shè)置時(shí)間同步的方法

  調(diào)整linux系統(tǒng)時(shí)間和時(shí)區(qū)與Internet時(shí)間同步

  一、修改時(shí)區(qū):

  # cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  修改為中國的東八區(qū)

  # vi /etc/sysconfig/clock

  ZONE="Asia/Shanghai"

  UTC=false

  ARC=false

  二、配置新的時(shí)間

  日期設(shè)定:

  # date -s 2008/05/06

  時(shí)間設(shè)定:

  # date -s 18:40:00

  查看硬件時(shí)間(BIOS的):

  hwclock [-rw]

  -r:查看現(xiàn)有BIOS時(shí)間,默認(rèn)為-r參數(shù)

  -w:將現(xiàn)在的linux系統(tǒng)時(shí)間寫入BIOS中

  當(dāng)我們進(jìn)行完 Linux 時(shí)間的校時(shí)后,還需要以 hwclock -w 來更新 BIOS 的時(shí)間,因?yàn)槊看伍_機(jī)的時(shí)候,系統(tǒng)會(huì)重新由 BIOS 將時(shí)間讀出來,所以, BIOS 才是重要的時(shí)間依據(jù)。

  # hwclock

  Tue 06 May 2008 03:49:37 PM CST -0.039646 seconds

  同步BIOS時(shí)鐘,強(qiáng)制把系統(tǒng)時(shí)間寫入CMOS:

  # clock -w

  三、實(shí)現(xiàn)Internet時(shí)間同步(這里可以忽略上面兩步)

  方法1. 開機(jī)的時(shí)候自動(dòng)網(wǎng)絡(luò)校時(shí):

  vi /etc/rc.d/rc.local

  /usr/sbin/ntpdate -u 192.168.0.2 192.168.0.3 192.168.0.4; /sbin/hwclock -w

  方法2. 設(shè)定計(jì)劃任務(wù)

  crontab格式如下:

  # For details see man 4 crontabs

  # Example of job definition:

  # .---------------- minute (0 - 59)

  # | .------------- hour (0 - 23)

  # | | .---------- day of month (1 - 31)

  # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...

  # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

  # | | | | |

  # * * * * * command to be executed

  設(shè)定crontab計(jì)劃任務(wù)也有兩種方式:

  1、寫在/etc/crontab里

  代碼:

  00 11 * * * root ntpdate 210.72.145.44

  每天11點(diǎn)與中國國家授時(shí)中心同步時(shí)間

  每天11點(diǎn)與中國國家授時(shí)中心同步時(shí)間

  當(dāng)然前提是

  apt-get install ntpdate

  代碼也可是

  00 11 * * * root ntpdate us.pool.ntp.org

  2、使用命令crontab -e

  crontab -e

  10 5 * * * root ntpdate us.pool.ntp.org;hwclock -w

  這樣每天5:10自動(dòng)進(jìn)行網(wǎng)絡(luò)校時(shí),并同時(shí)更新BIOS的時(shí)間

3644593