(Linux系统之时间管理)
一、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.
时间方面:
% : 印出 %
%n : 下一行
%t : 跳格
%H : 小时(00..23)
%I : 小时(01..12)
%k : 小时(0..23)
%l : 小时(1..12)
%M : 分钟(00..59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00..61)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区
日期方面:
%a : 星期几 (Sun..Sat)
%A : 星期几 (Sunday..Saturday)
%b : 月份 (Jan..Dec)
%B : 月份 (January..December)
%c : 直接显示日期与时间
%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 : 年份的最后两位数字 (00.99)
%Y : 完整年份 (0000..9999)
2.查询系统时间
①查询电脑硬件时间
[root@tianyi backups]# hwclock
2011-08-12 02:47:24.886129+08:00
[root@tianyi backups]#
②查询UTC时间
UTC:世界统一时间
[root@tianyi backups]# timedatectl
Local time: Fri 2011-08-12 02:15:40 CST
Universal time: Thu 2011-08-11 18:15:40 UTC
RTC time: Mon 2021-09-06 14:06:56
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
③查询系统时间
[root@tianyi ~]# date
Mon Sep 6 21:08:49 CST 2021
④特定时间格式输出系统时间
[root@tianyi ~]# date "+%Y-%m-%d %H:%M:%S"
2021-09-06 21:20:03
[root@tianyi ~]#
⑤查询电脑所有时间信息
[root@tianyi backups]# timedatectl
Local time: Fri 2011-08-12 03:04:41 CST
Universal time: Thu 2011-08-11 19:04:41 UTC
RTC time: Thu 2011-08-11 19:04:11
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
[root@tianyi backups]#
三、修改系统时间
1.使用-s选项修改
[root@tianyi ~]# date -s "2021-09-06 21:24:00"
Mon Sep 6 21:24:00 CST 2021
[root@tianyi ~]#
2.直接修改
数字为月日时分年.秒
[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:30
3.硬件时间向系统时间同步
[root@tianyi backups]# hwclock -w
[root@tianyi backups]#
4.系统时间向硬件时间同步
[root@tianyi backups]# hwclock -s
[root@tianyi backups]#
四、修改时区
[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脚本应用
[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服务器
1.检查chrony服务
[root@tianyi backups]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: 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服务
[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/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-09-06 21:54:47 CST; 1s ago
Docs: 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 iburst
4.重启服务并查看同步状态
[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服务器
[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 CST
Universal time: Mon 2021-09-06 15:24:22 UTC
RTC time: Thu 2011-08-11 19:36:12
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC 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 CST
Universal time: Mon 2021-09-06 15:24:51 UTC
RTC time: Mon 2021-09-06 15:24:51
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@tianyi backups]#
标签:tianyi,..,管理,time,系统,2021,Linux,backups,root
From: https://blog.51cto.com/u_14664141/6066740