linux ntpdate 查看同步时间,linux时区的修改及使用ntpdate同步时间

时间对linux来说十分重要,很多任务要求时间准确

时区的修改

查看当前时区

# date -R

通过如下命令,交互式问答来设置时区

# tzselect

这时会显示出来选择国家和地区,选好后提示你

You can make this change permanent for yourself by appending the line

TZ=’Asia/Shanghai’; export TZ

to the file ‘.profile’ in your home directory; then log out and log in again.

想要永久改变就写到你的.profile里,或/etc/profile全局文件中。

TZ='Asia/Shanghai'; export TZ

Here is that TZ value again, this time on standard output so that you

can use the /usr/bin/tzselect command in shell scripts:

Asia/Shanghai

或者使所有用户的时区改变

echo "TZ='Asia/Shanghai'" >> /etc/environment

对于 centos 6,用如下方法更改时区

cp /etc/localtime /etc/localtime.old

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

ls -lh /etc/localtime

date

还有一个地方如果能改也要改一下

vi /etc/sysconfig/clock

ZONE="Asia/Shanghai"

对于 centos7,稍微方便一些,原理一样

查看 timezones

[root@localhost ~]# timedatectl list-timezones

[root@localhost ~]# ls -l /etc/localtime

lrwxrwxrwx. 1 root root 38 8月 1 2014 /etc/localtime -> ../usr/share/zoneinfo/America/New_York

[root@localhost ~]# date

2015年 02月 24日 星期二 22:08:35 EST

设置 timezone

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai

[root@localhost ~]# date

2015年 02月 25日 星期三 11:09:14 CST

[root@localhost ~]# ls -l /etc/localtime

lrwxrwxrwx. 1 root root 35 2月 25 11:09 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

接下来,更新系统时间为Internet时间,权根为root用户。

先安装ntpdate并与0.cn.pool.ntp.org同步时间:

apt-get install ntpdate

ntpdate 0.cn.pool.ntp.org

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

# hwclock --systohc

上面命令中,--systohc,简写为 -w 表示SYStem clock to Hardware Clock;

同理--hctosys,简写为 -s 表示Hardware Clock to SYStem clock,不过我的理解是系统时间更新后把硬件时钟刷新的情况为正统。

为保证时间的准确性,把时间同步写入到cron里,每小时把当前系统时间与Internet时间同步,每天把系统时间与硬件时间同步。

* */1 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org >/dev/null

0 0 * * * /sbin/hwclock -w >/dev/null

Linux时钟介绍:

Linux机器里有两个时钟,一个是硬件时钟(CMOS时钟),一个是内核时钟。硬件时钟是电池驱动的,通过专门的芯片工作。可以通过BIOS设置屏或一些系统命令(如hwclock)进行设置。内核时钟是由内核维护的,启动时从硬件读取时间,之后独立运行。

RTC(Real Time Clock)

即Linux实时的时钟驱动,通常它们被嵌在计算机的芯片中,也有一些是在主板上使用Motorola MC146818(或clone)实现的。该硬件设备可映射到/dev/rtc,供root编程访问。

NTP(Network time protocol)

NTP为一种网络时间协议,是一种确保网络上的主机调整时间的方法。很多地方都会提供NTP服务器。

常用 ntp 服务器

ntpupdate.tencentyun.com

CentOS 7 更新时间

1. timedatectl 查看当前同步情况,如果没开,开启

timedatectl set-ntp 1

check 时间

datetimectl 显示

NTP enabled: yes

NTP synchronized: yes

2. 如果是物理机,将时间刷入 hardware

hwclock –systohc

check 硬件时间

hwclock

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值