linux进阶-⽹络时间ntp服务和chrony服务

实验准备

centos 6 :192.168.26.6
centos 7 :192.168.26.7

1.时间同步

date命令查看系统时间

[root@centos6 ~]# date
Mon Nov 11 20:05:20 CST 2019

查看硬件时钟

[root@centos6 ~]# clock
Mon 11 Nov 2019 08:05:29 PM CST  -0.907751 seconds

修改系统时间为2018年

[root@centos6 ~]# date -s " -1 year "
Sat Nov 10 20:05:51 CST 2018

但此时硬件时间依然没修改,若要修改硬件时间则

[root@centos6 ~]# clock
Mon 11 Nov 2019 08:05:55 PM CST  -0.172991 seconds
[root@centos6 ~]# clock -w
[root@centos6 ~]# clock
Sat 10 Nov 2018 08:06:08 PM CST  -1.016092 seconds

2.ntp软件实现时间同步

centos6上默认安装了ntp软件包(包括客户端和服务器端),但是ntp同步需要⼀定时间才能完全同步时间的,⽽chrony
同步时间⽐ntp快。centos7默认安装了chronyd服务

在 centos6上查看ntp软件、修改配置⽂件

[root@centos6 ~]# rpm -ql ntp
[root@centos6 ~]# vim /etc/ntp.conf

······
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

以下为增加的阿里的ntp服务器
server 120.25.115.20  iburst
······
[root@centos6 ~]# service ntpd start   #启动ntp
 
[root@centos6 ~]# chkconfig ntpd on   #开机自启

[root@centos6 ~]# ss -nul   #查看udp监听的123端口

[root@centos6 ~]# ntpq -p


说明:120.25.115.20是时间服务器的ip地址;iburst是加速同步时间。如果时间相差较⼤,是不能完成实时同步的

ntpdate命令,⽴即同步
ntp时间相差较⼤,是不能完成实时同步,那么可以⽤ntpdate命令来完成,但是在此之前得先关闭ntpd服务

[root@centos6 ~]# service ntpd stop
[root@centos6 ~]# ntpdate 120.25.115.20   #我们使用阿里的ntp服务同步时间

根据上例,centos6开启了ntpd服务,也可当做时间服务器被它⼈使⽤
centos6充当ntp服务器

[root@centos6 ~]# vim /etc/ntp.conf

······
restrict default nomodify notrap nopeer noquery 改为restrict default nomodify
······

[root@centos6 ~]# service ntpd start  #启动ntp服务

[root@centos6 ~]# chkconfig ntpd on   #开机启动

[root@centos6 ~]# ss -unl   #查看udp监听的123端口
[root@centos7 ~]# systemctl start ntpd   #开启ntpd服务
[root@centos7 ~]# date -s " -1 year "   #故意调错时间
[root@centos7 ~]# ntpdate 192.168.26.6   #同步centos6上设置的ntp服务器

3.在centos7上chrony软件实现时间同步

chrony同ntp,也可配置成客户端和服务器端两种。
查看chrony

[root@centos7 ~]# rpm -qi chrony

说明: /usr/bin/chronyc是客户端程序; /usr/sbin/chronyd是服务器端查询;
/usr/lib/systemd/system/chronyd.service是服务控制⽂件。

【例8】配置chrony为客户端,配置3个时间服务器地址

[root@centos7 ~]# vim /etc/chrony.conf

编辑配置文件
······
server 120.25.115.20 iburst
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
······

[root@centos7 ~]# systemctl start chronyd   #开启chronyd服务

[root@centos7 ~]# systemctl enable chronyd   #开机自启

[root@centos7 ~]# ss -untl   #查看udp监听的323端口

[root@centos7 ~]# date   #查看时间会立刻同步

chronyc客户端⼯具

[root@centos7 ~]# chronyc   #进入交互式命令行;
chronyc> help   #查看帮助子命令
chronyc> sources -v   #同步时钟
chronyc> quit 退出
[root@Magedu ~]# chronyc sources -v   #非交互式命令同步时钟

[root@Magedu ~]# systemctl status chronyd   #可以查看开机是否启动

[root@Magedu ~]# systemctl enable chronyd   #设置开机自动启动

在centos6上安装chrony软件包

[root@centos6 ~]# yum -y install chrony   #安装chrony

[root@centos6 ~]# service ntpd stop   #关闭ntpd服务

[root@centos6 ~]# chkconfig --list ntpd   #查看在6种登录模式下下ntpd服务是否自启

设置以centos7作为ntpd服务器

[root@centos6 ~]# vim /etc/chrony.conf

编辑文件加入
······
server 192.168.26.7 iburst
······

[root@centos6 ~]# service chronyd start   #开启chronyd服务

[root@centos6 ~]# chronyc sources -v   #同步时钟

在centos7上配置,充当时间同步服务器

[root@centos7 ~]# vim /etc/chrony.conf

编辑文件内容如下
······
server 120.25.115.20 iburst
allow 192.168.26.0/24
local stratum 10
······
[root@centos7 ~]# systemctl stop ntpd   #保证ntpd服务关闭,会与chronyd服务冲突

[root@centos7 ~]# systemctl start chronyd   #开启chronyd服务
说明:
allow 192.168.26.0/24表示192.168.26.   #这个网段可以向该主机同步时间;
allow 0.0.0.0/0   #表示允许所有主机向该主机同步时间;
local stratum 10   #表示当互联网不能访问时间服务器时,仍然使用本机时间提供时间服务。

再回到centos6,同步

[root@centos6 ~]# service stop ntpd   #客户端也要保持关闭ntpd服务

[root@centos6 ~]# service start chronyd   #开启chronyd服务

[root@centos6 ~]# chronyc sources -v    使用chronyc 同步192.168.26.7服务器时钟
总结:实现了使⽤⼀台主机向互联⽹上的时间服务器同步时间,在局域⽹内部,由其他主机向该主机同步时间即可
在初始化脚本里,修改chrony配置文件。

sed 's/^server.*/#&/' /etc/chrony.conf
echo server 120.25.115.20 iburst >> /etc/chrony.conf

4.timedatectl命令

列出所有时区

[root@centos7 ~]# timedatectl list-timezones

查看当前时区状态

[root@centos7 ~]# timedatectl status

修改时区

[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai 

[root@centos7 ~]# date

查看有效的配置⽂件

[root@centos7 ~]# grep -Ev "^(#.*|)$" /etc/chrony.conf

[root@centos7 ~]# grep -Ev "^#|^$" /etc/chrony.conf
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值