Linux 7.6
第六章:日期与时间
一.时间命令
- date
- 查看当前系统时间
- cal(calendar)查看日历
- cal 2020
- 修改时间
date -s 11:11:11//修改时分秒
date -s 2019-11-11//修改年月日,这时候会默认将时分秒归为0,所以需要先修改年月日再修改时分秒
date -s '2019-11-11 11:11:11'//或者直接一起修改
二.日期自动同步
多个客户端之间传输文件时需要确保双方使用同一时间,否则发送文件时显示的时间不一样。
这时候就需要有一个基准时间,所有的客户端以此为基准。
- 自动同步时间
- yum install ntp -y
- ntpdate cn.ntp.org.cn
可以登录全球可用的NTP服务器列表与解析服务cn.ntp.org.cn,查看IP池。
这次调用的就是儒科电子提供的时间。
也可以直接通过
ntpdate IP地址
来调用对应IP的时间
当我们的客户端处于内网环境时,无法连接到外网,不能使用NTP服务提供的基准时间时,以一台计算器的时间为基准时间,ntp该计算机的IP地址,保持各个客户端时间一致。
- 本地搭建NTP服务
- 开启本地NTP服务器
service ntpd start
配置文件
vi /etc/ntp.conf
//-----权限控制-----
restrict default kod nomodify notrap nopeer noquery//拒绝IPV4用户
restrict -6 default kod nomodify notrap nopeer noquery//拒绝IPV6用户
restrict 210.72.145.44//授权国家授时中心服务器访问本地NTP
restrict 133.100.11.8//授权133.100.11.8访问本地NTP
restrict 127.0.0.1
restrict -6 : :1
restrict 192.168.188.2 mask 255.255.255.0 nomodify//本地网段授权访问
//-----源服务器-----
10server cn.ntp. org.cn prefer//指定上级更新时间服务器,优先使用这个地址
//-----差异分析-----
driftfile /var/lib/ntp/drift
keys/etc/ntpkeys
- 客户端同步时间
ntpdate IP地址
这里一开始没有获取成功,还是在IP192.168.12.100中的配置文件ntp.conf中授予了权限才成功配置了
restrict 192.168.12.101