Linux时间同步服务 - chronyd

Linux时间同步服务 - chronyd

安装

使用软件包安装工具安装chronyntp

  • Arch Linux系列:

    sudo pacman -S ntp chrony
    
  • Debian系列:

    sudo apt install ntp chrony
    
  • Red Hat系列:

    sudo yum install ntp chrony
    

    或者

    sudo dnf install ntp chrony
    

配置

① 在chrony配置文件中,将原授时NTP服务器注释,并填入阿里或腾讯NTP授时服务器网址,示例如下:

### SPECIFY YOUR NTP SERVERS
# Most computers using chrony will send measurement requests to one or
# more 'NTP servers'.  You will probably find that your Internet Service
# Provider or company have one or more NTP servers that you can specify.
# Failing that, there are a lot of public NTP servers.  There is a list
# you can access at http://support.ntp.org/bin/view/Servers/WebHome or
# you can use servers from the pool.ntp.org project.

#! server 0.arch.pool.ntp.org iburst
#! server 1.arch.pool.ntp.org iburst
#! server 3.arch.pool.ntp.org iburst

#pool 2.arch.pool.ntp.org iburst

# The URL of Tencent NTP network time server is as follows:
! server ntp.tencent.com iburst
! server ntp2.tencent.com iburst
! server ntp3.tencent.com iburst
! server ntp4.tencent.com iburst
! server ntp5.tencent.com iburst

# The URL of Ali NTP network time server is as follows:
! server ntp.aliyun.com iburst
! server ntp1.aliyun.com iburst
! server ntp2.aliyun.com iburst
! server ntp3.aliyun.com iburst
! server ntp4.aliyun.com iburst
! server ntp5.aliyun.com iburst
! server ntp6.aliyun.com iburst
! server ntp7.aliyun.com iburst

pool ntp1.tencent.com

注意:

选择其中三、四个即可。

② 输入如下命令,使得防火墙为chronyd服务放行,以便chronyd服务顺利运行:

  • Firewalld设置:

    $ firewall-cmd --add-service=ntp --permanent
    $ firewall-cmd --reload
    
  • Ufw设置:

    $ sudo ufw allow ntp
    $ sudo ufw allow out ntp
    

管理

使用systemctl命令允许chronyd服务开机自启动、使得chronyd服务启动并查看chronyd服务的运行状态,具体命令如下所示:

# systemctl start chronyd.service    #启动
# systemctl status chronyd.service     #查看
# systemctl enable chronyd.service    #设置开机启动

使用如下命令,查看时间同步源:

$ chronyc sources -v

使用如下命令,查看时间同步源状态:

$ chronyc sourcestats -v

使用如下命令,启用NTP时间同步:

$ timedatectl set-ntp yes

使用如下命令,校准时间服务器:

$ chronyc tracking

注意:

配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux服务器时间同步可以通过NTP协议来实现。具体步骤如下: 1.安装NTP服务:在终端中输入以下命令进行安装 ```shell yum install ntp ``` 2.配置NTP服务:在终端中输入以下命令进行配置 ```shell vi /etc/ntp.conf ``` 在打开的文件中添加如下内容: ```shell server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst ``` 这里以阿里云的NTP服务器为例,你也可以使用其他的NTP服务器。 3.启动NTP服务:在终端中输入以下命令进行启动 ```shell systemctl start ntpd ``` 4.设置NTP服务开机自启:在终端中输入以下命令进行设置 ```shell systemctl enable ntpd ``` 5.让集群自己同步时间:有两种方式 第一种方式:通过crontab来完成同步,推荐这种,容易控制时间同步频率。所有子节点配置定时任务,半分钟同步一次,命令如下: ```shell crontab -e ``` 在打开的文件中添加如下一条信息: ```shell 30 * * * * /usr/sbin/ntpdate master的ip或域名 >> /root/ntpdate.log 2>&1 ``` 后期需要观察一下/root/ntpdate.log中有没有定时同步时间的运行日志。 第二种方式:通过chrony来完成同步,命令如下: ```shell yum install chrony systemctl start chronyd systemctl enable chronyd ``` 6.检查时间同步是否成功:在终端中输入以下命令进行检查 ```shell timedatectl ``` 如果输出结果中的NTP synchronized为yes,则表示时间同步成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值