05-时间同步与自动化部署-01

时间同步

  • 时间同步软件实现:ntp、chrony

chrony文件组成

  • :chrony
  • 两个主要程序:chronyd、chronyc
  • chronyd:后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿
  • chronyc:命令行用户工具,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可在一台不同的远程计算机上工作
  • 服务unit 文件: /usr/lib/systemd/system/chronyd.service
  • 监听端口
    • 服务端: 123/udp
    • 客户端: 323/udp
  • 配置文件: /etc/chrony.conf

配置文件chrony.conf

server  #可用于时钟服务器,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据
包。 包间隔通常为2秒,可加快初始同步速度
pool     #该指令的语法与server 指令的语法相似,不同之处在于它用于指定NTP服务器池而不是单个
NTP服务器。池名称应解析为随时间可能会变化的多个地址
driftfile #根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作
出补偿
rtcsync  #启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)
allow / deny #指定一台主机、子网,或者网络以允许或拒绝访问本服务器
cmdallow / cmddeny #可以指定哪台主机可以通过chronyd使用控制命令
bindcmdaddress #允许chronyd监听哪个接口来接收由chronyc执行的命令
makestep # 通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定
情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在
调整期大于某个阀值时调整系统时钟
local stratum 10  #即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它
客户端
  • chronyc 可以运行在交互式和非交互式两种方式,支持以下子命令
help 命令可以查看更多chronyc的交互命令
accheck 检查是否对特定主机可访问当前服务器
activity 显示有多少NTP源在线/离线
sources [-v]   显示当前时间源的同步信息
sourcestats [-v]显示当前时间源的同步统计信息
add server 手动添加一台新的NTP服务器
clients 报告已访问本服务器的客户端列表
delete 手动移除NTP服务器或对等服务器
settime 手动设置守护进程时间
tracking 显示系统时间信息
  • 公共NTP服务
pool.ntp.org:项目是一个提供可靠易用的NTP服务的虚拟集群cn.pool.ntp.org,0-3.cn.pool.ntp.org
阿里云公共NTP服务器
Unix/linux类:ntp.aliyun.com,ntp1-7.aliyun.com
windows类: time.pool.aliyun.com
腾讯公共NTP
time1-5.cloud.tencent.com
大学ntp服务
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
国家授时中心服务器:210.72.145.44 
美国标准技术院: time.nist.gov
  • timedatectl 时间和时区管理
#查看日期时间、时区及NTP状态:
timedatectl
#查看时区列表:
timedatectl list-timezones
#修改时区:
timedatectl set-timezone Asia/Shanghai
#修改时区
[root@ubuntu18.04:~# rm -f /etc/localtime
[root@ubuntu18.04:~# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#修改日期时间:
timedatectl set-time "2017-01-23 10:30:00"
#开启NTP:
timedatectl set-ntp true/false

部署企业私有NTP时间同步服务器

  • 服务端操作
  • 【作为企业内部的服务端,同时作为互联网时间同步服务器的客户端】
[16:22:38 nameke@vb01 ~]$ sudo yum install -y chrony
[16:25:38 nameke@vb01 ~]$ sudo egrep -v "#|^$" /etc/chrony.conf
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
#阿里云时间同步服务器ntp1-7.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.56.0/24
#指定允许时间同步的网段
local stratum 10
#取消注释,表示当互联网无法连接时,仍可以为客户端提供时间同步服务
logdir /var/log/chrony
  • 防火墙开放udp的123/323端口
#查看源的详细信息
chrony sources -v
#查看时间同步状态
chronyc sourcestats -v
#查看当前时间同步服务开启情况
timedatectl
#防火墙放开udp的323,123端口
firewall-cmd --zone=public --permanent --add-port=123/udp
firewall-cmd --zone=public --permanent --add-port=323/udp
firewall-cmd --reload
  • 客户端操作
[16:24:29 nameke@vb03 ~]$ sudo yum install -y chrony
[16:25:29 nameke@vb03 ~]$ sudo egrep -v "#|^$" /etc/chrony.conf
#配置私有时间同步服务器作为时间源
server 192.168.56.11 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony

sudo systemctl start chronyd.service && sudo systemctl status chronyd.service

一键部署私有NTP时间同步服务器

#!/bin/bash
#
#********************************************************************
#Author:                chengzi
#QQ:                     978668580
#Date:                  2022-04-08
#FileName:              scripts/install_ntp_chrony.sh
#URL:                   https://www.cnblogs.com/chenwei01/
#Description:           The test script
#Copyright (C):         2022 All rights reserved
#********************************************************************
#. /etc/init.d/functions
. /home/nameke/scripts/functions

#检查chrony服务端是否已安装/启动chrony
function check_start_chrony(){
    if [ $(rpm -qa chrony|wc -l) -eq 0 ]
    then
        yum -y install chrony >>/dev/null &>1 && colors green "NTP service of chrony had installed succ!"
    else
        colors yellow "warnning:NTP service of chrony had aready installed!"
    fi
    #检查chrony服务是否已启动
    proc_num=$(ps aux|egrep chronyd|egrep -v grep|wc -l)
    port_num=$(netstat -anulp|egrep "123|323"|egrep -v grep|wc -l)
    if [ $proc_num -gt 0 -a $port_num -gt 0 ]
    then
        colors red "NTP service had aready started"
    else
        systemctl start chronyd.service && systemctl status chronyd.service && systemctl enable chronyd.service
    fi
}
function config_chrony(){
#配置chrony服务端配置文件
if [ ! -f /etc/chrony.conf ]
then
    cat >/etc/chrony.conf<<EOF
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
#阿里云时间同步服务器ntp1-7.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.56.0/24
#指定允许时间同步的网段
local stratum 10
#取消注释,表示当互联网无法连接时,仍可以为客户端提供时间同步服务
logdir /var/log/chrony
EOF
else
    colors yellow "warning:/etc/chrony.conf had aready exist!"
    if [ $(egrep "ntp*.aliyun.com" /etc/chrony.conf|egrep -v grep|wc -l) -eq 0 ]
    then
        sed -i 's|^#allow 192.168.0.0/16|allow 192.168.56.0/24|g' /etc/chrony.conf
        sed -i 's|^#local stratum 10|local stratum 10|g' /etc/chrony.conf
        egrep -v "#|^$" /etc/chrony.conf >/etc/chrony.conf-$(date +%F).bak
        for i in $(seq -w 6);
        do
            sed -nr "$i s/^server .*iburst/server ntp$i.aliyun.com iburst/gp" /etc/chrony.conf-$(date +%F).bak 
            #sed -nr "$i s/^server ntp4.aliyun.com iburst/server ntp$i.aliyun.com iburst/gp" chrony.conf
        done >/etc/chrony.conf
        egrep -v "#|^$" /etc/chrony.conf-$(date +%F).bak|tail -6 >>/etc/chrony.conf
        colors green "Replace NTP server's domain to ntp.aliyun.com succ!"
    else
        colors red "NTP server's domain had aready configurated to ntp*.aliyun.com!"
    fi
fi
}

function chrony_firewall_status(){
#查看源的详细信息
sleep 10 && chronyc sources -v
#查看当前时间同步服务开启情况
#timedatectl
#防火墙放开udp的323,123端口
if [ $(iptables-save|egrep "123|323"|wc -l) -eq 0 ]
then
    firewall-cmd --zone=public --permanent --add-port=123/udp
    firewall-cmd --zone=public --permanent --add-port=323/udp
    firewall-cmd --reload && colors green "Add firewall-port 123/323 succ!"
else
    colors red "firewall-port 123/323 had aready exist!"
    exit 2
fi
}
function server_node(){
    check_start_chrony
    config_chrony
    chrony_firewall_status
    colors green "Install and configurate chrony server-node succ!" && exit 0
}

function client_node(){
check_start_chrony
if [ ! -f /etc/chrony.conf ]
then
    cat >/etc/chrony.conf<<EOF
#配置私有时间同步服务器作为时间源
server 192.168.56.11 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
else
    if [ $(egrep "192.168.56.11" /etc/chrony.conf|egrep -v grep|wc -l) -eq 0 ]
    then
        sed -i 's/^server .*iburst$/server 192.168.56.11 iburst/g' /etc/chrony.conf 
        colors green "Replace NTP server's domain for 192.168.56.11 succ!"
    else
        colors red "NTP server's domain had aready configurated to 192.168.56.11!"
    fi
fi
    systemctl restart chronyd.service  && sleep 10
    chronyc sources -v && colors green "Install and configurate chrony client-node succ!" && exit 0
}
function usage(){
    colors red "Usage:bash $0 {server|client}"
    exit 2
}
function main(){
    check_root
    if [ $# -eq 1 ]
    then
        case $1 in
            server)
            server_node
            ;;
            client)
            client_node
            ;;
            *)
            usage
            ;;
        esac
    else
        usage
    fi
}
main $1
  •  参考资料:

https://www.cnblogs.com/rusking/p/7634412.html

 

posted @ 2022-05-12 01:09  西瓜的春天  阅读(110)  评论(0)    收藏  举报