• Centos内网服务器搭建NTP时间同步服务


    Centos内网服务器搭建NTP时间同步服务

    服务器环境

    Centos 7.6

    0.准备工作

    0.1 下载或拷贝以下rpm包到所有服务器

    autogen-libopts-5.18-5.el7.x86_64.rpm
    ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
    ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
    
    • 1
    • 2
    • 3

    推荐下载地址:https://pkgs.org/

    0.2 主服务器防火墙需放通TCP和UDP协议的123端口(123端口为NTP服务默认端口)

    1.配置NTP主服务器

    1.1 查看服务器是否安装ntp服务

    rpm -qa | grep ntp
    # 如果发现已安装,将其卸载
    rpm -e --nodeps ntpdate-4.2.6p5-28.el7.centos.x86_64	#版本因环境而异
    
    • 1
    • 2
    • 3

    1.2 依次安装所需软件包

    rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
    rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
    rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
    
    • 1
    • 2
    • 3

    1.3 修改配置文件ntp.conf

    vim /etc/ntp.conf
    将
    # restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    中的“192.168.1.0”改为集群ip地址的网段并取消注释,我这改成以下
    restrict 192.168.190.0 mask 255.255.255.0 nomodify notrap
    即容许192.168.190.0/24网段的服务器访问获取时间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果集群整体在内网中无法连接外网的情况下,需进行以下配置

    # 注释以下4个互联网授时站点
    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
    
    # 将主服务器时间设置为授时源
    server 127.0.0.1
    fudge 127.0.0.1 stratum 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.4 修改ntpd文件

    vim /etc/sysconfig/ntpd
    # 添加一行,使硬件时间和系统时间同步
    SYNC_HWCLOCK=yes
    
    • 1
    • 2
    • 3

    1.5 重启ntp服务并设置为开机自启动

    systemctl start ntpd
    systemctl enable ntpd
    
    • 1
    • 2

    2.配置从服务器

    2.1 查看服务器是否安装ntp服务

    rpm -qa | grep ntp
    # 如果发现已安装,将其卸载
    rpm -e --nodeps ntpdate-4.2.6p5-28.el7.centos.x86_64	#版本因环境而异
    
    • 1
    • 2
    • 3

    2.2 依次安装所需软件包

    rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
    rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
    rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
    
    • 1
    • 2
    • 3

    2.3 为主服务器设置一个别名

    vim /etc/hosts 
    # 添加一条
    192.168.190.136 ntp-server	# 根据主服务器IP地址进行替换
    # 重启网卡
    systemctl restart network
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.4 将ntp服务停止并关闭开机自启动,改用定期任务进行时间同步操作

    systemctl stop ntpd
    systemctl disable ntpd
    
    # 创建定期同步系统时间的任务
    crontab -e
    */1 * * * * /usr/sbin/ntpdate ntp-server	# 每隔1分钟向别名为ntp-server的服务器同步一次时间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.测试

    # 找一台从服务器,修改其系统时间
    date -s "2021-9-11 11:11:11"
    # 等待设定的执行时间或手动执行任务
    /usr/sbin/ntpdate ntp-server	# 手动执行任务
    date	# 查看系统时间是否已同步
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.常见的问题

    防火墙拦截导致时间同步失败

    /usr/sbin/ntpdate ntp-server	#手动执行任务
    # 执行任务后报错信息如下的,是因为服务器防火墙未放通导致,请根据前文准备工作进行更改
    26 Sep 13:02:26 ntpdate[11905]: no server suitable for synchronizati on found
    
    • 1
    • 2
    • 3
  • 相关阅读:
    MySQL中USER()和CURRENT_USER()的区别
    Flutter笔记:Matrix4矩阵变换与案例
    d3dcompiler_43.dll是什么文件?缺失d3dcompiler_43.dll文件修复与解决方法
    Android优化RecyclerView图片展示:Glide成堆加载批量Bitmap在RecyclerView成片绘制Canvas,Kotlin(b)
    工厂设备管理维护系统开发
    Linux脚本练习之script087-netstat练习之查看和3306端口建立的连接
    ue5蓝图请求接口
    序列化、反序列化
    Linux14 -- 进程间通信、管道
    HTML5离线Web应用概述
  • 原文地址:https://blog.csdn.net/qq_41103628/article/details/133383945