CentOS时间的查看与修改

本文介绍了如何在Linux系统中查看和修改系统时区与时间,包括使用date、hwclock等命令进行操作的方法,并提供了定时同步时间的具体步骤。

http://www.centoscn.com/CentOS/help/2014/0805/3430.html


  1、查看、修改Linux时区与时间


一、linux时区的查看与修改


1,查看当前时区
date -R

 

2,修改设置时区
方法1:
tzselect

 

方法2:
仅限于RedHat Linux 和 CentOS
timeconfig

 

方法3:
适用于Debian
dpkg-reconfigure tzdata

 

3,复制相应的时区文件,替换系统时区文件;或者创建链接文件
cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime


在中国可以使用:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 

二、linux时间的查看与修改


1、查看时间和日期
date


2、设置时间和日期
将系统日期设定成1996年6月10日的命令
date -s 06/22/96


将系统时间设定成下午1点52分0秒的命令
date -s 13:52:00


3. 将当前时间和日期写入BIOS,避免重启后失效
hwclock -w

 

三、定时同步时间


* * * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1

 

Linux中用于时钟查看和设置的命令主要有date、hwclock和clock。
其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

 

查看Linux系统时间:
date

 

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. hwclock –hctosys    
  2. #或    
  3. clock –hctosys    


修改Linux系统时间:
date -s (后面跟时间)

 

 

查看Linux硬件时间:

Java代码  收藏代码
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. hwclock    
  2. #或    
  3. clock    
  4. #或    
  5. hwclock –show    
  6. #或    
  7. clock –show    

修改Linux硬件时间:

C代码  收藏代码

 

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. hwclock –set –date    
  2. #或    
  3. clock –set –date    
 让系统时间与硬件时钟同步,用:

 

C代码  收藏代码

 

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. hwclock –hctosys    
  2. #或    
  3. clock –hctosys    
 相反地,让硬件时钟与系统时间同步:

 

C代码  收藏代码

 

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. hwclock –systohc    
  2. #或    
  3. clock –systohc   

 

让系统时间每隔十分钟去同步一下硬件时间。

C代码  收藏代码
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. [hqw@localhost root]$ vi /etc/crontab    
  2. SHELL=/bin/bash    
  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin    
  4. MAILTO=root    
  5. HOME=/    
  6. # run-parts    
  7. 01 * * * * root run-parts /etc/cron.hourly    
  8. 02 4 * * * root run-parts /etc/cron.daily    
  9. 22 4 * * 0 root run-parts /etc/cron.weekly    
  10. 42 4 1 * * root run-parts /etc/cron.monthly    
  11. */10 * * * * root hwclock --hctosys #加入此行,每隔10分钟执行一次hwclock --hctosys  


### 查看修改文件或目录的时间属性 在 CentOS 系统中,可以通过以下方法查看修改文件或目录的时间属性。 #### 查看文件或目录的时间属性 使用 `stat` 命令可以详细查看文件的三个时间属性:`mtime`(修改时间)、`ctime`(状态时间)和 `atime`(存取时间)。例如: ```bash stat 文件名 ``` 上述命令将输出文件的详细信息,包括时间戳。例如: ```plaintext File: 文件名 Size: 4506 Blocks: 8 IO Block: 4096 regular file Device: fd02h/64770d Inode: 137917 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-07-19 17:53:22.000000000 +0800 Modify: 2023-04-08 19:11:45.000000000 +0800 Change: 2023-06-25 08:28:12.000000000 +0800 Birth: - ``` 其中,`Access` 表示 `atime`,`Modify` 表示 `mtime`,`Change` 表示 `ctime`[^1]。 此外,还可以通过 `ls` 命令查看文件的时间属性: ```bash ls -l --time=mtime 文件名 # 查看 mtime ls -l --time=atime 文件名 # 查看 atime ls -l --time=ctime 文件名 # 查看 ctime ``` #### 修改文件或目录的时间属性 使用 `touch` 命令可以修改文件的时间属性。以下是具体用法: 1. **修改 mtime 和 atime**: ```bash touch 文件名 ``` 上述命令会将 `mtime` 和 `atime` 更新为当前系统时间[^2]。 2. **指定时间进行修改**: 使用 `-t` 参数可以指定时间戳: ```bash touch -t [[CC]YY]MMDDhhmm[.ss] 文件名 ``` 例如,将文件的时间设置为 2023 年 10 月 1 日 12:30:45: ```bash touch -t 202310011230.45 文件名 ``` 3. **仅修改 mtime**: 使用 `-m` 参数可以单独修改 `mtime`: ```bash touch -m 文件名 ``` 4. **仅修改 atime**: 使用 `-a` 参数可以单独修改 `atime`: ```bash touch -a 文件名 ``` 5. **复制其他文件的时间属性**: 使用 `-r` 参数可以从另一个文件复制时间属性: ```bash touch -r 参考文件 目标文件 ``` #### 示例代码 以下是一个完整的示例,展示如何查看修改文件的时间属性: ```bash # 创建一个测试文件 echo "测试内容" > test.txt # 查看文件的原始时间属性 stat test.txt # 修改文件的时间属性为指定时间 touch -t 202310011230.45 test.txt # 再次查看文件的时间属性 stat test.txt ``` ### 注意事项 - `ctime`(状态时间)无法通过 `touch` 命令直接修改,因为它是系统自动更新的,当文件的状态发生变化时才会更新。 - 如果需要批量修改多个文件的时间属性,可以结合 `find` 命令使用[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值