Linux服务器使用NTP服务同步时间

同步服务器时间:ntpvsntpdate与ntpd的部署指南
本文介绍了如何在服务器上使用ntpdate进行单机时间同步以及通过ntpd实现多台机器之间的时间同步。步骤包括检查ntpdate安装、配置master节点、启动服务和同步子节点时间。

一般服务器都自带ntp服务,如果没有就先下载

第一种同步网络时间

第一种是在所有机器上,使用连接工具的并发命令功能,同时运行修改时间命令,比如执行date -s “2019-05-25 17:00:00” ,这个方法适合自己测试环境用

安装工具
yum -y install ntp ntpdate
同步网络时间(生产环境无法同步网络时间)
ntpdate cn.pool.ntp.org

第二种同步某一台机器的时间

第二种就是安装了一个ntpdate命令,然后同步时间的master是一个所有人都可以用的master
使用ntpd server,为多台机器同步时间,这里以三台机器为例

第一步:在三台机器上运行ntpdate命令,验证是否安装有ntpdate,如果没有运行yum -y install ntp安装该服务

第二步:选出一台机器,做master,编辑它的/etc/ntp.conf文件

进入配置文件之后,首先将配置文件中所有的restrict开头的行注释掉,如下面这种

#restrict 127.0.0.1 
#restrict ::1
restrict 172.xxx.xxx.0 mask 255.255.255.0 nomodify notrap

将所有server开头的行全部注释,添加如下的新信息

#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 3.centos.pool.ntp.org iburst
#server 172.18.0.4 iburst
server 127.127.1.0
fudge 127.127.1.0 stratum 10

第三步:master启动ntp服务并开机自启

systemctl restart ntpd.service
sudo systemctl enable ntpd.service

第四步:所有子节点运行命令,同步master的时间

ntpdate 主节点ip

如果子节点运行命令之后,如果提示no server那就等两分钟左右在在运行
如果提示`the NTP socket is in use, exiting`说明当前子节点的ntp服务启动了, 你要关掉它
systemctl stop ntpd.service
### 使用NTP进行时间同步 #### 安装NTP服务 在基于Debian或Ubuntu的系统上,可以使用以下命令安装NTP服务: ```bash sudo apt-get update sudo apt-get install ntp ``` 在基于Red Hat或CentOS的系统上,使用以下命令安装: ```bash sudo yum install ntp ``` #### 配置NTP服务器 编辑NTP配置文件`/etc/ntp.conf`,添加或修改NTP服务器地址。例如,可以使用公共的NTP服务器: ```plaintext server ntp.aliyun.com server time.google.com ``` #### 启动并设置开机自启 启动NTP服务: ```bash sudo systemctl start ntp ``` 设置NTP服务开机自启: ```bash sudo systemctl enable ntp ``` #### 检查时间同步状态 使用以下命令检查NTP服务的状态: ```bash sudo systemctl status ntp ``` 使用`ntpq -p`命令查看NTP服务器同步状态: ```bash ntpq -p ``` ### 使用Chrony进行时间同步 #### 安装Chrony服务 在基于Debian或Ubuntu的系统上,使用以下命令安装Chrony: ```bash sudo apt-get update sudo apt-get install chrony ``` 在基于Red Hat或CentOS的系统上,使用以下命令安装: ```bash sudo yum install chrony ``` #### 配置Chrony服务器 编辑Chrony配置文件`/etc/chrony.conf`,添加或修改NTP服务器地址。例如: ```plaintext server ntp.aliyun.com iburst server time.google.com iburst ``` #### 启动并设置开机自启 启动Chrony服务: ```bash sudo systemctl start chronyd ``` 设置Chrony服务开机自启: ```bash sudo systemctl enable chronyd ``` #### 检查时间同步状态 使用`chronyc sources -v`命令查看Chrony的同步源状态: ```bash chronyc sources -v ``` 使用`chronyc tracking`命令查看时间同步的详细信息: ```bash chronyc tracking ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值