===============服务器时间同步================================
ntpdate 172.18.0.1 瞬间同步
ntp协议 network time protocol保证时刻同步 (前提是时间差不能太大)
vim /etc/ntp.conf
server 172.18.0.1 iburst
启动ntpd服务
#chkconfig --list ntpd
chkconfig ntpd on
service ntpd start
cenos7
工具一yum install ntp
工具二:(建议)
yum info chrony
rpm -ql chrony /etc/chrony.conf
vim /etc/chrony.conf
server 172.18.0.1 iburst
systemctl start chronyd
systemctl is-enabled chronyd 是否开机启动
systemctl enable chronyd 设为开机启动
http://blog.sina.com.cn/s/blog_636a55070101u1mg.html
1、date 显示日期和时间
2、时区:/etc/localtime
3、cal 显示日历
4、Linux的两种时钟
系统时钟:由Linux内核通过CPU的工作频率进行的
硬件时钟:主板
hwclock,clock: 显示硬件时钟
-s, --hctosys 以硬件时钟为准,校正系统时钟
-w, --systohc 以系统时钟为准,校正硬件时钟
1、时间
date
[root@centos6 ~]# date "+%Y年%m月%d日 %H时%M分%S秒 星期2"
2018年05月15日 15时59分43秒 星期2
[root@centos6 ~]# date "+%F %T"
2018-05-15 16:00:07
[root@centos6 ~ ]#date +%s #1970年1月1号(unix诞生日)到现在有多少秒
1531454783
[root@centos6 ~ ]#date -d @1531454783
Fri Jul 13 12:06:23 CST 2018
[root@centos6 ~]# date 100112202019.20 #月日时分[年][.秒]
Tue Oct 1 12:20:20 CST 2019
[root@centos6 ~]# date -s '2020-1-5 12:30:50'
Sun Jan 5 12:30:50 CST 2020
[root@instructor_v7(nanyibo) ~]# date -d '-2 days' +%A 表示前天是星期几
Wednesday
2、时区
centos7---
[root@centos7:~]# timedatectl list-timezones
[root@centos7:~]# timedatectl status
Local time: Fri 2018-07-13 11:23:24 CST
Universal time: Fri 2018-07-13 03:23:24 UTC
RTC time: Fri 2018-07-13 03:23:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@centos7:~]# timedatectl set-timezone Africa/Kinshasa
centos6---
[root@centos6 ~ ]#tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#?
[root@centos6 ~ ]#ll /usr/share/zoneinfo/Asia/
total 416
-rw-r--r--. 3 root root 157 Dec 1 2016 Aden
-rw-r--r--. 1 root root 971 Dec 1 2016 Almaty
-rw-r--r--. 6 root root 388 Dec 1 2016 Shanghai
[root@centos6 ~ ]#ll /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
-rw-r--r--. 6 root root 388 Dec 1 2016 /etc/localtime
-rw-r--r--. 6 root root 388 Dec 1 2016 /usr/share/zoneinfo/Asia/Shanghai
3、日历
[root@centos6 ~ ]#cal
July 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[root@centos6 ~ ]#cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
历史原因导致2到14。4年一闰,百年不闰,400年再闰,即:
"能被4整除却不能被100整除 或 能被400整除的年份是闰年"
Centos date 设置自定义时间
【1】手动修改
(1)设置日期
# date -s 20190315
(2)设置时间
# date -s 15:23:34
(3)设置日期和时间
# date -s "20190315 15:12:23"
命令执行结果如下图:
如上三种方式设置自定义时间。
【2】自动同步
(1)安装ntpdate工具
# yum -y install ntp ntpdate
(2)设置系统时间与网络时间同步
# ntpdate cn.pool.ntp.org
(3)将系统时间写入硬件时间
# hwclock --systohc
(4)强制系统时间写入CMOS中防止重启失效
# hwclock -w
或clock -w
命令执行结果如下图: