第二章:chrony服务器
一、设置时间
-ctl #有子命令
[root@RHCE ~]# date #查看时间
[root@RHCE ~]# date "+%Y-%m-%d %H:%M:%S"
[root@RHCE ~]# timedatectl #查看详细时间
[root@RHCE ~]# timedatectl set-time "2011-11-11 11:11:11" #设置时间为2011年11月11日11时11分11秒
[root@RHCE ~]# timedatectl set-ntp 0/1 #关闭或开启时间服务器(ntp)
[root@RHCE ~]# timedatectl set-local-rtc 0/1 #关闭或开启硬件时间(rtc)
二、chrony服务
配置文件保存在etc
命令保存在bin
超级命令保存在sbin
服务保存在server
[root@RHCE ~]# rpm -ql chrony # -ql查看哪些软件(如chrony)产生那些文件
[root@RHCE ~]# rpm -qf chrony # -qf查看哪些文件是由哪些软件产生的(如chrony)
[root@RHCE ~]# systemctl status chronyd #查看chrony服务状态
[root@RHCE ~]# systemctl stop chronyd.server # 关闭chrony服务
[root@RHCE ~]# systemctl start chronyd.server #开启chrony服务
chronyd.server 用来控制时间同步服务
[root@RHCE ~]# vim /etc/chrony.conf #查看chrony的配置文件
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
#pool(池子) 2.rhel.pool.ntp.org(ip/网址) iburst(立刻同步)
#server(从池子中获取一台服务器) 2.rhel.pool.ntp.org(ip/网址)iburst(立刻同步)
pool表示时间服务集群
server表示从集群中挑选一台时间同步服务器
iburst表示如果重启会立即同步时间
······
[root@RHCE ~]# systemctl restart chronyd #重启chrony服务
服务都会在命令后加个d,如要重启chrony命令要写成chronyd即chronyd.server
三、实验
创建两台虚拟机,一台作为时间服务器(服务端),一台作为服务器获取时间(客户端)
1.创建服务端
[root@RH ~]# hostnamectl hostname timeserver #永久修改名称为timeserver(时间服务器)
[root@RH ~]# reboot #重启虚拟机(想要看到刚修改的名称需要重启虚拟机)
修改ip要围绕4个点修改:IP地址、子网掩码、dns、网关
[root@timeserver ~]# nmcli connection modify ens160 ipv4.addresses 192.168.153.100/24 #修改IP地址和掩码(要注意自己的虚拟机的网络位,如192.168.153.xxx/24)
[root@timeserver ~]# nmcli connection modify ens160 ipv4.dns 8.8.8.8 #修改dns
[root@timeserver ~]# nmcli connection modify ens160 ipv4.gateway 192.168.153.2 #修改网关(要注意自己虚拟机的网关,默认的网关是多少,就配置多少)
[root@timeserver ~]# nmcli connection modify ens160 ipv4.method manual #将自动获取IP地址改为手动配置IP地址
[root@timeserver ~]# nmcli connection up ens160 #激活
2.创建客户端
[root@RH ~]# hostnamectl hostname timeclient #永久修改名称为timeclient
[root@RH ~]# reboot #重启虚拟机(想要看到刚修改的名称需要重启虚拟机)
[root@timeclient ~]# nmcli connection modify ens160 ipv4.method ipv4.addresses 192.168.153.200/24 ipv4.gateway 192.168.153.2 ipv4.dns 8.8.8.8 #同时将自动改手动,修改IP地址、子网掩码、网关和dns
[root@timeclient ~]# nmcli connection up ens160 #激活
3.配置时间服务器
[root@timeserver ~]# vim /etc/chrony.conf
2 # Please consider joining the pool (https://www.pool.ntp.org/join.html).
3 #pool 2.rhel.pool.ntp.org iburst #配置服务器,不用与官方匹配就屏蔽掉
······
25 # Allow NTP client access from local network.
26 allow 0.0.0.0/0 #允许所有人获取时间(允许谁获取时间就把时间写到这)
······
28 # Serve time even if not synchronized to a time source.
29 local stratum 10 #第10层时间服务器
[root@timeserver ~]# timedatectl set-ntp 0 #关闭时间服务器
[root@timeserver ~]# timedatectl set-time "2011-11-11 11:11:11" #设置时间
[root@timeserver ~]# systemctl stop firewalld.sever #临时(stop)关闭网络防火墙
[root@timeserver ~]# setenforce 0 #关闭内核防火墙
[root@timeserver ~]# systemctl restart chronyd #重启chrony服务
[root@timeserver ~]# watch -n 1 timedatectl #每一秒监控详细时间
4.配置客户端同步时间源
[root@timeclient ~]# vim /etc/chrony.conf
2 # Please consider joining the pool (https://www.pool.ntp.org/join.html).
3 pool 192.168.153.100 iburst #从timeserver获取时间就把ip改成timeserverIP地址
······
[root@timeclient ~]# systemctl restart chronyd #重启chrony服务
[root@timeclient ~]# watch -n 1 timedatectl #每一秒监控详细时间