• centos同步服务器时间


    集群同步,不同步互联网

    查看所有节点ntpd服务状态

    sudo systemctl status ntpd
    sudo systemctl start ntpd
    sudo systemctl is-enabled ntpd
    
    • 1
    • 2
    • 3

    查询自己的网段

    ip addr
    
    • 1

    修改hadoop102的ntp.conf配置文件

    sudo vim /etc/ntp.conf
    
    • 1

    先按i,添加如下

    restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap
    
    • 1

    注释以前,去除互联网时间同步

    #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
    
    • 1
    • 2
    • 3
    • 4

    当该节点丢失网络连接,采用本地时间作为时间服务器为集群中的其他节点提供时间同步,添加如下

    server 127.127.1.0
    fudge 127.127.1.0 stratum 10
    
    • 1
    • 2

    按esc, 输入!wq退出

    修改hadoop102的/etc/sysconfig/ntpd 文件

    vim /etc/sysconfig/ntpd
    
    • 1

    增加内容如下(让硬件时间与系统时间一起同步)

    SYNC_HWCLOCK=yes
    
    • 1

    按esc, 输入!wq退出

    重新启动ntpd服务
    sudo systemctl start ntpd

    设置ntpd服务开机启动
    sudo systemctl enable ntpd

    其他机器配置(必须root用户)

    (1)关闭所有节点上ntp服务和自启动

    systemctl stop ntpd
    systemctl disable ntpd
    
    • 1
    • 2

    (2)在其他机器配置1分钟与时间服务器同步一次

    crontab -e
    
    • 1

    编写定时任务如下:

    */1 * * * * /usr/sbin/ntpdate hadoop102
    
    • 1

    (3)修改任意机器时间

    date -s "2021-9-11 11:11:11"
    
    • 1

    (4)将当前时间和日期写入BIOS,避免重启后失效

    hwclock -w或者hwclock --systohc
    
    • 1

    (5)1分钟后查看机器是否与时间服务器同步
    date

    同步互联网时间

    下载 ntpdate,用ntp来实现时间同步
    yum install -y ntpdate

    调整时区为上海,我也好神奇为什么是上海,也就是北京时间 + 8 区
    注:想改其他时区也可以去看看 / usr/share/zoneinfo 目录
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    先启动,否则会如提示 no server suitable for synchronization found
    service ntpd start

    使用 NTP 来同步时间
    ntpdate us.pool.ntp.org
    ntpdate ntp.ntsc.ac.cn

    查看状态
    service ntpd status

    查看当前服务器运行的时区
    timedatectl status

    查看一下服务器是否有上海时区
    timedatectl list-timezones | grep Shanghai

    设置服务器的时区为上海时区
    timedatectl set-timezone Asia/Shanghai

    这个时候再使用这个命令service ntpd start回车
    会看到18 Mar 22:53:11 ntpdate[1230]: the NTP socket is in use, exiting

    如使用中,要再次同步,先停止
    service ntpd stop

    手动配置当前时间

    timedatectl set-time  "2022-09-22 16:48:00"
    
    • 1

    来验证下时间
    date

    定时同步时间(每隔 10 分钟同步时钟)
    echo “*/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP” >> /tmp/crontab.bak

    启动定时任务
    crontab /tmp/crontab.bak

    其他参考 https://blog.51cto.com/u_11239407/2921519

  • 相关阅读:
    基于NodeJS 的健康饮酒管理小程序设计与实现
    Java版工程行业管理系统源码-专业的工程管理软件- 工程项目各模块及其功能点清单
    2022大厂高频面试题之Vue篇
    05-SA8155 QNX Hypervisor BSP之Interrupts中断
    redis持久化和Redis事务
    在win10上安装配置Hadoop的环境变量
    数据库的三种日志:redo log、binlog和undo log
    硬件工程师经常犯的几个典型错误
    Autosar Configuration(十) Ethernet之物理层PHY-EB配置
    选择振弦采集仪进行岩土工程监测时,根据不同工况选择合适的种类
  • 原文地址:https://blog.csdn.net/weixin_44371237/article/details/126840022