Linux系统之时间管理
生活随笔
收集整理的這篇文章主要介紹了
Linux系统之时间管理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Linux系統(tǒng)之時(shí)間管理
- 一、date命令介紹
- 1.date幫助
- 2.查詢系統(tǒng)時(shí)間
- ①查詢電腦硬件時(shí)間
- ②查詢UTC時(shí)間
- ③查詢系統(tǒng)時(shí)間
- ④特定時(shí)間格式輸出系統(tǒng)時(shí)間
- ⑤查詢電腦所有時(shí)間信息
- 三、修改系統(tǒng)時(shí)間
- 1.使用-s選項(xiàng)修改
- 2.直接修改
- 3.硬件時(shí)間向系統(tǒng)時(shí)間同步
- 4.系統(tǒng)時(shí)間向硬件時(shí)間同步
- 四、修改時(shí)區(qū)
- 四、在shell腳本應(yīng)用
- 五、ntp服務(wù)器
- 1.檢查chrony服務(wù)
- 2.開啟chrony服務(wù)
- 3.編輯chrony.conf配置文件
- 4.重啟服務(wù)并查看同步狀態(tài)
- 5.立即同步ntp服務(wù)器
一、date命令介紹
1.date幫助
[root@tianyi ~]# date Mon Sep 6 20:56:57 CST 2021 [root@tianyi ~]# date --help Usage: date [OPTION]... [+FORMAT]or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date. 時(shí)間方面:% : 印出 % %n : 下一行 %t : 跳格 %H : 小時(shí)(00..23) %I : 小時(shí)(01..12) %k : 小時(shí)(0..23) %l : 小時(shí)(1..12) %M : 分鐘(00..59) %p : 顯示本地 AM 或 PM %r : 直接顯示時(shí)間 (12 小時(shí)制,格式為 hh:mm:ss [AP]M) %s : 從 1970 年 1 月 1 日 00:00:00 UTC 到目前為止的秒數(shù) %S : 秒(00..61) %T : 直接顯示時(shí)間 (24 小時(shí)制) %X : 相當(dāng)于 %H:%M:%S %Z : 顯示時(shí)區(qū) 日期方面:%a : 星期幾 (Sun..Sat) %A : 星期幾 (Sunday..Saturday) %b : 月份 (Jan..Dec) %B : 月份 (January..December) %c : 直接顯示日期與時(shí)間 %d : 日 (01..31) %D : 直接顯示日期 (mm/dd/yy) %h : 同 %b %j : 一年中的第幾天 (001..366) %m : 月份 (01..12) %U : 一年中的第幾周 (00..53) (以 Sunday 為一周的第一天的情形) %w : 一周中的第幾天 (0..6) %W : 一年中的第幾周 (00..53) (以 Monday 為一周的第一天的情形) %x : 直接顯示日期 (mm/dd/yy) %y : 年份的最后兩位數(shù)字 (00.99) %Y : 完整年份 (0000..9999)2.查詢系統(tǒng)時(shí)間
①查詢電腦硬件時(shí)間
[root@tianyi backups]# hwclock 2011-08-12 02:47:24.886129+08:00 [root@tianyi backups]#②查詢UTC時(shí)間
UTC:世界統(tǒng)一時(shí)間
[root@tianyi backups]# timedatectl Local time: Fri 2011-08-12 02:15:40 CSTUniversal time: Thu 2011-08-11 18:15:40 UTCRTC time: Mon 2021-09-06 14:06:56Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: noNTP service: activeRTC in local TZ: no③查詢系統(tǒng)時(shí)間
[root@tianyi ~]# date Mon Sep 6 21:08:49 CST 2021④特定時(shí)間格式輸出系統(tǒng)時(shí)間
[root@tianyi ~]# date "+%Y-%m-%d %H:%M:%S" 2021-09-06 21:20:03 [root@tianyi ~]#⑤查詢電腦所有時(shí)間信息
[root@tianyi backups]# timedatectl Local time: Fri 2011-08-12 03:04:41 CSTUniversal time: Thu 2011-08-11 19:04:41 UTCRTC time: Thu 2011-08-11 19:04:11Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: noNTP service: activeRTC in local TZ: no [root@tianyi backups]#三、修改系統(tǒng)時(shí)間
1.使用-s選項(xiàng)修改
[root@tianyi ~]# date -s "2021-09-06 21:24:00" Mon Sep 6 21:24:00 CST 2021 [root@tianyi ~]#2.直接修改
數(shù)字為月日時(shí)分年.秒
[root@tianyi ~]# date 090621282021.28 Mon Sep 6 21:28:28 CST 2021 [root@tianyi ~]# date "+%Y-%m-%d %H:%M:%S" 2021-09-06 21:28:303.硬件時(shí)間向系統(tǒng)時(shí)間同步
[root@tianyi backups]# hwclock -w [root@tianyi backups]#4.系統(tǒng)時(shí)間向硬件時(shí)間同步
[root@tianyi backups]# hwclock -s [root@tianyi backups]#四、修改時(shí)區(qū)
[root@tianyi backups]# timedatectl list-timezones |grep Shanghai Asia/Shanghai [root@tianyi backups]# timedatectl set-timezone Display all 426 possibilities? (y or n)^C [root@tianyi backups]# timedatectl set-timezone Asia/Shanghai [root@tianyi backups]#四、在shell腳本應(yīng)用
[root@tianyi tmp]# cp -r /etc /backups/etc_$(date +%Y%m%d%H%M%S) [root@tianyi tmp]# cd /backups/ [root@tianyi backups]# ls etc_20210906214116 [root@tianyi backups]#五、ntp服務(wù)器
1.檢查chrony服務(wù)
[root@tianyi backups]# systemctl status chronyd ● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:chronyd(8)man:chrony.conf(5) [root@tianyi backups]#2.開啟chrony服務(wù)
[root@tianyi backups]# systemctl enable --now chronyd Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service. [root@tianyi backups]# systemctl status chronyd ● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2021-09-06 21:54:47 CST; 1s agoDocs: man:chronyd(8)man:chrony.conf(5)3.編輯chrony.conf配置文件
[root@tianyi backups]# vim /etc/chrony.conf [root@tianyi backups]# # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). pool ntp.aliyun.com iburst4.重啟服務(wù)并查看同步狀態(tài)
[root@tianyi backups]# chronyc sources 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 37 60 +311us[-1713us] +/- 29ms [root@tianyi backups]#5.立即同步ntp服務(wù)器
[root@tianyi backups]# chronyc sources -v 210 Number of sources = 1.-- Source mode '^' = server, '=' = peer, '#' = local clock./ .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 17 3 +305us[ +387us] +/- 28ms [root@tianyi backups]# timedatectl Local time: Mon 2021-09-06 23:24:22 CSTUniversal time: Mon 2021-09-06 15:24:22 UTCRTC time: Thu 2011-08-11 19:36:12Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yesNTP service: activeRTC in local TZ: no [root@tianyi backups]# date Mon Sep 6 23:24:38 CST 2021 [root@tianyi backups]# hwclock -w [root@tianyi backups]# timedatectl Local time: Mon 2021-09-06 23:24:51 CSTUniversal time: Mon 2021-09-06 15:24:51 UTCRTC time: Mon 2021-09-06 15:24:51Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yesNTP service: activeRTC in local TZ: no [root@tianyi backups]#總結(jié)
以上是生活随笔為你收集整理的Linux系统之时间管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统的磁盘管理
- 下一篇: Linux系统之高级用户组和权限管理