linux系统时间设置

linux系统时间设置

linux 系统时间设置

两步

(1)date 042612492005

(2)hwclock –w

第一步的意思是设置时间 , 设置完了可以用 date 命令查看对不对 ... 注意是月日时分年

第二步的意思是写入主板的 rtc 芯片 ..
=======================================

su -c 'date -s / / '

su -c 'date -s : : '

了解 Linux 的时钟

     由于 Linux 时钟和 Windows 时钟从概念的分类、使用到设置都有很大的不同,所以,搞清楚 Linux 时钟的工作方式与设置操作,不仅对于 Linux 初学者有着重大意义,而且对于使用 Linux 服务器的用户来说尤为重要。

Linux 时钟的分类

Windows 时钟大家可能十分熟悉了, Linux 时钟在概念上类似 Windows 时钟显示当前系统时间,但在时钟分类和设置上却和 Windows 大相径庭。和 Windows 不同的是, Linux 将时钟分为系统时钟 (System Clock) 和硬件 (Real Time Clock ,简称 RTC) 时钟两种。系统时间是指当前 Linux Kernel 中的时钟,而硬件时钟则是主板上由电池供电的那个主板硬件时钟,这个时钟可以在 BIOS “Standard BIOS Feture” 项中进行设置。

既然 Linux 有两个时钟系统,那么大家所使用的 Linux 默认使用哪种时钟系统呢?会不回出现两种系统时钟冲突的情况呢?这些疑问和担心不无道理。首先, Linux 并没有默认哪个时钟系统。当 Linux 启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。

Linux 启动过程来看,系统时钟和硬件时钟不会发生冲突,但 Linux 中的所有命令 ( 包括函数 ) 都是采用的系统时钟设置。不仅如此,系统时钟和硬件时钟还可以采用异步方式,见图 1 所示,即系统时间和硬件时间可以不同。这样做的好处对于普通用户意义不大,但对于 Linux 网络管理员却有很大的用处。例如,要将一个很大的网络中 ( 跨越若干时区 ) 的服务器同步,假如位于美国纽约的 Linux 服务器和北京的 Linux 服务器,其中一台服务器无须改变硬件时钟而只需临时设置一个系统时间,如要将北京服务器上的时间设置为纽约时间,两台服务器完成文件的同步后,再与原来的时钟同步一下即可。这样系统和硬件时钟就提供了更为灵活的操作。

设置 Linux 的时钟

Linux 中,用于时钟查看和设置的命令主要有 date hwclock clock 。其中, clock hwclock 用法相近,只不过 clock 命令除了支持 x86 硬件体系外,还支持 Alpha 硬件体系。由于目前绝大多数用户使用 x86 硬件体系,所以可以视这两个命令为一个命令来学习。

1.   在虚拟终端中使用 date 命令来查看和设置系统时间

查看系统时钟的操作:

# date

设置系统时钟的操作:

# date 091713272003.30

通用的设置格式:

# date 月日时分年 .

2.   使用 hwclock clock 命令查看和设置硬件时钟

查看硬件时钟的操作:

# hwclock --show

# clock –show

2003 09 17 星期三 13 24 11 -0.482735 seconds

设置硬件时钟的操作:

# hwclock --set --date="09/17/2003 13:26:00"

或者

# clock --set --date="09/17/2003 13:26:00"

通用的设置格式: hwclock/clock --set --date=“ / / 时:分:秒

3.   同步系统时钟和硬件时钟

Linux 系统 ( 笔者使用的是 Red Hat 8.0 ,其它系统没有做过实验 ) 默认重启后,硬件时钟和系统时钟同步。如果不大方便重新启动的话 ( 服务器通常很少重启 ) ,使用 clock hwclock 命令来同步系统时钟和硬件时钟。

硬件时钟与系统时钟同步:

# hwclock –hctosys

或者

# clock –hctosys

上面命令中, --hctosys 表示 Hardware Clock to SYStem clock

系统时钟和硬件时钟同步:

# hwclock –systohc

或者

# clock –systohc

使用图形化系统设置工具设置时间

对于初学者来,笔者推荐使用图形化的时钟设置工具,如 Red Hat 8.0 中的日期与时间设置工具,可以在虚拟终端中键 “system-config-time” 命令,或者选择 “K 选单 / 系统设置 / 日期与时间 来启动日期时间设置工具。使用该工具不必考虑系统时间和硬件时间,只需从该对话框中设置日期时间,可同时设置、修改系统时钟和硬件时钟。

Internet 同步时钟设置

Windows XP 日期与时间设置中有一项与 Internet 同步的功能,有了这项功能只要上网便可得到十分准确的时间。 Red Hat 8.0 也提供了这样的功能,在日期与时间设置工具对话框中的下部,有一个 启用网络时间协议 的选项,将该项选中就可以使用网络时间协议来同步 Linux 系统时钟。选中该项后,其下面的服务器下拉列表框就变为可用状态,可从中选择一个时间服务器作为远程时间服务器。然后单击确定按钮,便可连接所设定的时间服务器,并与之同步时间。

Case 1

How to change the clock source in the system?

Article ID: 34407 - Created on: Sep 2, 2009 3:03 AM - Last Modified:  Jun 22, 2010 3:33 AM

Issue

· In some systems, the default clock source  may cause system time delay.

· How to change the clock source in the system?

· How to check which clock source is used on my system.

Environment

· Red Hat Enterprise Linux 4

· Red Hat Enterprise Linux 5

Resolution

The clock source in the system could be changed to make the time more accurate.

Red Hat Enterprise Linux 5

1. Check the available & current clock-source in the system:

# cat /sys/devices/systems/clocksource/clocksource0/available_clocksource

  tsc jiffies pit

# cat /sys/devices/systems/clocksource/clocksource0/current_clocksource

  tsc

Clocksource availability depends on the hardware of the system.

2. Change a value of the clocksource:

# echo "jiffies" > /sys/devices/systems/clocksource/clocksource0/current_clocksource

In order to use a specified clock source after reboot, please append the kernel 'clocksource =' parameter in /boot/grub/grub.conf. The following is an example from x86 system:

  title Red Hat Enterprise Linux (2.6.18-128.el5)

        root (hd0,0)

        kernel /vmlinuz-2.6.18-128.el5 ro root=LABEL=/ clocksource=jiffies

        initrd /initrd-2.6.18-128.el5.img

Case 2

VMWare guest time runs fast and jumps ahead on RHEL4 or RHEL5

Article ID: 35501 - Created on: Dec 29, 2009 4:41 PM - Last Modified:  Aug 16, 2010 10:15 PM

Issue

· Rapid kernel timekeeping issue.

· /usr/bin/top redisplays all of the processes in a continuous fashion, instead of observing the set three second refresh delay.

· select() and sleep() system calls complete faster than they should.

· Time skipping with VMWare.

Environment

· Red Hat Enterprise Linux 4

· Red Hat Enterprise Linux 5

· VMWare guest

Resolution

· Kernel parameters will need to be added to the current kernel in /boot/grub/grub.conf. This parameter is different for different versions of Red Hat Enterprise Linux:

Kernel parameters for 32-bit kernels

Version(s)

Parameter(s)

* Red Hat Enterprise Linux 5.4 (kernel 2.6.18-164)

No additional kernel parameters required.

Refer to vmware kbase about the optional use of divider=10 .

Red Hat Enterprise Linux 5.1 to 5.3 (kernel 2.6.18-53 - 2.6.18-128)

clocksource=acpi_pm divider=10

Red Hat Enterprise Linux 5.0 (kernel 2.6.18-8)

clocksource=acpi_pm

Red Hat Enterprise Linux 4.7 to 4.8 (kernel 2.6.9-78 - 2.6.9-89)

clock=pmtmr divider=10

Red Hat Enterprise Linux 4.0 to 4.6 (kernel 2.6.9-5 - 2.6.9-67)

clock=pmtmr

Kernel parameters for 64-bit kernels

Version(s)

Parameter(s)

* Red Hat Enterprise Linux 5.4 (kernel 2.6.18-164)

No additional kernel parameters required.

Refer to vmware kbase about the optional use of divider=10 .

Red Hat Enterprise Linux 5.2 to 5.3 (kernel 2.6.18-53 - 2.6.18-128)

notsc divider=10

Red Hat Enterprise Linux 5.1 with RHSA:2007-0993  (kernel 2.6.18-8)

notsc divider=10

Red Hat Enterprise Linux 5.1 without RHSA:2007-0993  (kernel 2.6.18-8)

notsc

Red Hat Enterprise Linux 5.0 (kernel 2.6.18-8)

no additional kernel parameters required

Red Hat Enterprise Linux 4.7 to 4.8 (kernel 2.6.9-78 - 2.6.9-89)

notsc divider=10

Red Hat Enterprise Linux 4.2 to 4.6 (kernel 2.6.9-5 - 2.6.9-67)

notsc

Note: None of these cases have reported that using divider has solved this problem on Red Hat Enterprise Linux 5.4.

VMware has released the patch for a similar problem on ESX 4.0 Update 2. Check your VMWare support to verify your problem can be applicable for this patch:

http://www.vmware.com/support/vsphere4/doc/vsp_esx40_u2_rel_notes.html#patches

An issue in the timer emulation causes timer interrupts to be delivered to the guest operating system at an excessive rate

This issue has been observed after a vMotion migration when a virtual machine has been up for a relatively long time, such as for one hundred days.

This issue is resolved in this release.

Examples for kernel parameters setup

1 Edit /etc/grub.conf file.

o Before:

title Red Hat Enterprise Linux Server-base (2.6.18-92.el5)

        root (hd0,0)

        kernel /vmlinuz-2.6.18-92.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet

        initrd /initrd-2.6.18-92.el5.img

o After:

title Red Hat Enterprise Linux Server-base (2.6.18-92.el5)

        root (hd0,0)

        kernel /vmlinuz-2.6.18-92.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet clocksource=acpi_pm divider=10

        initrd /initrd-2.6.18-92.el5.img

2 Reboot the system:

# reboot

NTP parameters

· The /etc/ntp.conf should also be modified to    allow it to accept extremely large offset changes: add or edit the line tinker panic 0.

Examples for NTP parameters setup

Edit /etc/ntp.conf file:
tinker panic 0

restrict 127.0.0.1

restrict default kod nomodify notrap

server 0.vmware.pool.ntp.org

server 1.vmware.pool.ntp.org

server 2.vmware.pool.ntp.org

driftfile /var/lib/ntp/drift

3 Restart the ntpd service:

# service ntpd restart

Comments

For more information refer to:

http://kb.vmware.com/kb/1006427

Root cause

· The clock tick speed in a virtual machine can run at a different frequency to what the kernel expects hardware to be running at. The system expects more ticks than are provided by the hypervisor, and runs roughly ten seconds worth of ticks in one second.

Diagnostic steps

4 Apply the timekeeping best practices documented in http://kb.vmware.com/kb/1006427

5 Check for timer interrupt delivery falling behind: ntpq -p

6 Check that NTP is running properly in the guest and on the host: ntpq -p

7 Collect time in the guest versus time reported by a reference source: /usr/sbin/ntpdate -q <timeserver>

8 Open a ticket with vmware support:

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1008524
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值