centos7 ntp时间同步
2026-06-23 05:21:26一 应用场景 公司分配给你两台服务器,要求你使他们时间同步,有人问为什么要时间同步?如果一个集群中,时间相差很大,那么会出现很多诡异的问题,你也不想在一个无法解决的问题上浪费几天时间吧!总之,设置服务器之间时间同步,为了避免很多问题的发生! 二 基础知识 #date #查看或者设置当前系统的时间 #ntpdate #手工临时同步系统时间 #ntpd #作为守护进程,按照一定的算法进行时间同步,即使你启动了该进程,ntpd也不会立刻进行时间同步 linux里面用asia/shanghai表示东八区,等价于windows中的 北京、重庆、香港特别行政区,乌鲁木齐的时区设置。 如果安装时选错了时区,或者使用了默认的纽约时区,可以通过以下命令进行修改: #cp /usr/share/zoneinfo/asia/shanghai /etc/localtime 三 操作步骤 1 准备工作 两个节点都需要执行 首先安装ntp #yum install ntp 安装完毕之后,启动服务 #systemctl start ntpd.service 设置开机自启动 #systemctl enable ntpd.service 2 准备2台服务器 2台服务器:【192.168.0.104】,【192.168.0.107】 3 ntp服务端设置 第一台服务器192.168.0.104,作为ntpserver,将他设置为同步外网时间(ntpd服务开启默认就同步了)。 但是得设置允许内网网段可以连接它,将它作为内网的时间同步服务器,所以需要一个简单配置。 修改/etc/ntp.conf文件,添加如下信息,表示允许210网段来同步此服务器 # vim /etc/ntp.conf restrict 192.168.0.0 mask 255.255.255.0 #添加此行 截图如下: 设置后,重启ntpd服务,用ntpstat来检查效果【大概几秒钟就可以看到效果】 #systemctl restart ntpd #ntpstat 4 ntp客户端设置 192.168.0.107 作为ntpclient,将他设置为同步上面的ntpserver,同样需要一个简单配置,分别在两个节点上做如下配置! 修改/etc/ntp.conf文件,注释掉外网时间服务器,添加本地服务器即可 #vim /etc/ntp.conf server 192.168.0.104 #添加此行 # 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 截图如下: 设置后,重启ntpd服务,用ntpstat来检查效果【大概需要等十五分钟,看到效果】 #systemctl restart ntpd #ntpstat 转发自:https://blog.csdn.net/chengqiuming/article/details/78735433