时间对linux非常重要,许多任务都要求时间准确
更改时区
显示当前时区
# date -R
使用以下命令交互式设置时区
# tzselect
此时,系统会提示您选择国家和地区,选择后再提示
youcanmakethischangepermanentforyourselfbyappendingtheline
tz=’Asia/Shanghai’; export TZ
othe 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,thistimeonstandardoutputsothatyou
can use the/usr/bin/tzselectcommandinshellscripts 3360
Asia/Shanghai
或者更改所有用户的时区
echo ' tz=' Asia/Shanghai ' '/etc/environment
对于centos 6,请使用以下方法更改时区
CP/etc/local time/etc/local time.old
ln-NFS/usr/share/zoneinfo/Asia/Shanghai/etc/local time
ls -lh /etc/localtime
日期
另一个地方如果能改变的话就必须改变
vi /etc/sysconfig/clock
ZONE='Asia/Shanghai '
对于centos7,有点方便,原理相同
浏览时间分区
[ root @ localhost~] # timedatectllist-time zones
[ root @ localhost~] # ls-l/etc/local time
lrwxrwxrwx. 1 root root 38 8月1日2014/etc/local time-./usr/share/zoneinfo/America/new _ York
[root@localhost ~]# date
2015年02月24日星期二22:08:35 EST
设置时间区域
[ root @ localhost~] # timedatectlset-time zone Asia/Shanghai
[root@localhost ~]# date
2015年02月25日星期三11:09:14 CST
[ root @ localhost~] # ls-l/etc/local time
lrwxrwxrwx. 1 root root 35 2月25日11:09/etc/local time-- ./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,简称-w表示SYStem clock to Hardware Clock;
同样,--hctosys,简称-s表示硬件时钟到系统时钟,但我理解在系统时间更新后更新硬件时钟是合法的。
为了保证时间的准确性,将时间同步写入cron,每小时使当前系统时间与互联网时间同步,每天使系统时间与硬件时间同步。
*/1 * * */usr/sbin/ntpdate0. cn.pool.NTP.org/dev/null
0 0 * * * /sbin/hwclock -w /dev/null
Linux时钟介绍:
Linux机器有两个时钟。 一个是硬件时钟(CMOS时钟),另一个是内核时钟。 硬件时钟由电池供电,在专用芯片上工作。 可以通过BIOS设置画面或hwclock等系统命令进行设置。 内核时钟由内核维护,启动时从硬件读取时间,然后独立工作。
实时时钟(RTC )
也就是Linux的实时时钟驱动,通常它们集成在计算机的芯片中,有的在主板上使用Motorola MC146818 (或clone )实现。 该硬件设备可以映射到/dev/rtc,并且可以通过根程序访问。
网络时间协议(NTP )
NTP是网络时间协议,是确保网络上主机时间一致的方法。 很多地方都提供了NTP服务器。
典型ntp服务器
ntpupdate.tencentyun.com
CentOS 7更新时间
1. timedatectl检查当前同步状态,如果未选中,则选中
timedatectl set-ntp 1
检查时间
日期CTL显示
NTP已启用:是
NTP同步:是
2 .在物理机器的情况下,向硬件刷时间
hwclocksystohc
检查硬件时间
hwclock