Chrony 是一个用于时间同步的软件,它旨在提供高精度的系统时钟同步。Chrony 软件包括一个 NTP(Network Time Protocol,网络时间协议)服务器和客户端,可以帮助计算机系统与网络时间服务器同步,以确保系统时钟的准确性。
以下是 Chrony 的一些关键特点和功能:
总之,Chrony 是一个功能强大的时间同步工具,适用于需要高精度时钟同步的计算机系统,尤其是对时间同步精度要求较高的服务器和网络设备。通过使用 Chrony,用户可以确保其系统时钟与外部时间源同步,从而提高计算机系统的时间准确性和稳定性。
chrony官网:https://chrony-project.org
chrony官方文档:https://chrony-project.org/documentation.html
包:chrony
两个主要程序:chronyd和chronyc
服务unit 文件: /usr/lib/systemd/system/chronyd.service
监听端口: 服务端: 123/udp,客户端: 323/udp
配置文件: /etc/chrony.conf
server - 可用于时钟服务器,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据包。 包间隔通常为2秒,可加快初始同步速度
driftfile - 根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作出补偿
rtcsync - 启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)
allow / deny - 指定一台主机、子网,或者网络以允许或拒绝访问本服务器
cmdallow / cmddeny - 可以指定哪台主机可以通过chronyd使用控制命令
bindcmdaddress - 允许chronyd监听哪个接口来接收由chronyc执行的命令
makestep - 通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时调整系统时钟
local stratum 10 - 即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端
[root@rocky10 ~]# mkdir -p /etc/systemd/network/
[root@rocky10 ~]# touch /etc/systemd/network/70-eth0.link
[root@rocky10 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:02:55:30 brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname enx000c29025530
inet 172.31.15.1/20 brd 172.31.15.255 scope global dynamic noprefixroute ens160
valid_lft 1776sec preferred_lft 1776sec
inet6 fe80::20c:29ff:fe02:5530/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@rocky10 ~]# cat > /etc/systemd/network/70-eth0.link << EOF
[Match]
MACAddress=00:0c:29:f8:60:8f
[Link]
Name=eth0
EOF
# 也可以执行下面命令
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
ETHMAC=`ip addr show ${ETHNANME} | awk -F' ' '/ether/{print $2}'`
mkdir /etc/systemd/network/
touch /etc/systemd/network/70-eth0.link
cat > /etc/systemd/network/70-eth0.link << EOF
[Match]
MACAddress=${ETHMAC}
[Link]
Name=eth0
EOF
[root@rocky10 ~]# mv /etc/NetworkManager/system-connections/ens160.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection
[root@rocky10 ~]# sed -i.bak 's/'ens160'/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection
# 也可以执行下面命令
mv /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection
/etc/default/grub
文件,添加以下内容到 GRUB_CMDLINE_LINUX
行net.ifnames=0 biosdevname=0
执行以下命令:
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grub
然后更新 GRUB 配置:
grub2-mkconfig -o /boot/grub2/grub.cfg
如果是 UEFI 引导系统,更新命令为:
# Rocky
grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg
# Almalinux
grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg
# CentOS
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
# openEuler
grub2-mkconfig -o /boot/efi/EFI/openEuler/grub.cfg
# AnolisOS
grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg
# OpenCloudOS
grub2-mkconfig -o /boot/efi/EFI/opencloudos/grub.cfg
# Kylin Server
grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg
# UOS Server
grub2-mkconfig -o /boot/efi/EFI/uos/grub.cfg
# UEFI 引导系统可以直接执行下面命令:
EFI_DIR=`find /boot/efi/ -name "grub.cfg" | awk -F"/" '{print $5}'`
grub2-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="<网卡MAC地址>", NAME="eth0"
注意:创建Udev这个步骤只在AnolisOS 8上执行。
将 <网卡MAC地址>
替换为实际网卡的 MAC 地址。
[root@anolisos8 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:47:02:3f brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 172.31.15.25/20 brd 172.31.15.255 scope global dynamic noprefixroute ens33
valid_lft 1785sec preferred_lft 1785sec
inet6 fe80::20c:29ff:fe47:23f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@anolis8 ~]# cat >> /etc/udev/rules.d/10-network.rules << EOF
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="00:0c:29:c5:38:3b", NAME="eth0"
EOF
# 也可以执行下面命令
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
ETHMAC=`ip addr show ${ETHNANME} | awk -F' ' '/ether/{print $2}'`
cat >> /etc/udev/rules.d/10-network.rules << EOF
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${ETHMAC}", NAME="eth0"
EOF
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/sysconfig/network-scripts/ifcfg-eth0
# Ubuntu和Debian先启用root用户,并设置密码
raymond@ubuntu2404:~$ cat set_root_login.sh
#!/bin/bash
read -p "请输入密码: " PASSWORD
echo ${PASSWORD} |sudo -S sed -ri 's@#(PermitRootLogin )prohibit-password@\1yes@' /etc/ssh/sshd_config
sudo systemctl restart sshd
sudo -S passwd root <<-EOF
${PASSWORD}
${PASSWORD}
EOF
raymond@ubuntu2404:~$ bash set_root_login.sh
请输入密码: 123456
[sudo] password for raymond: New password: Retype new password: passwd: password updated successfully
net.ifnames=0 biosdevname=0
执行以下命令:
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@net.ifnames=0 biosdevname=0"@' /etc/default/grub
然后更新 GRUB 配置:
grub-mkconfig -o /boot/grub/grub.cfg
如果是 UEFI 引导系统,更新命令为:
# Ubuntu
grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfg
# Debian
grub-mkconfig -o /boot/efi/EFI/debian/grub.cfg
# UEFI 引导系统可以直接执行下面命令:
EFI_DIR=`find /boot/efi/ -name "grub.cfg" | awk -F"/" '{print $5}'`
grub-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg
# Ubuntu Server 24.04 LTS
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/50-cloud-init.yaml
# Ubuntu Server 22.04 LTS
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
touch /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
cat > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg <<-EOF
network: {config: disabled}
EOF
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/50-cloud-init.yaml
# Ubuntu Server 20.04 LTS
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/00-installer-config.yaml
# Ubuntu Server 18.04 LTS
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/01-netcfg.yaml
# Debian
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/network/interfaces
reboot
重启后,网卡名称将变为 eth0
。
1ETHNAME=`ip addr |awk -F"[ :]"'/^2/{print $3}'`
2
3# IP变量是ip地址,PREFIX变量是子网掩码位数;GATEWAY变量是网关地址;PRIMARY_DNS变量是主DNS,BACKUP_DNS变量是备用DNS,根据自己的需求修改。
4IP=172.31.0.10
5PREFIX=20
6GATEWAY=172.31.0.2
7PRIMARY_DNS=223.5.5.5
8BACKUP_DNS=180.76.76.76
Rocky Linux 9/10、Almalinux 9/10、CentOS Stream 9/10、AnolisOS 23、OpenCloudOS 9:
1cat> /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection <<-EOF
2[connection]
3id=${ETHNAME}
4type=ethernet
5interface-name=${ETHNAME}
6
7[ipv4]
8address1=${IP}/${PREFIX},${GATEWAY}
9dns=${PRIMARY_DNS};${BACKUP_DNS};
10method=manual
11EOF
Rocky Linux 8、Almalinux 8、CentOS 7、CentOS Stream 8、openEuler 22.03和24.03 LTS、AnolisOS 8、OpenCloudOS 8、Kylin Server v10/v11、UOS Server v20:
1cat> /etc/sysconfig/network-scripts/ifcfg-${ETHNAME}<<-EOF
2NAME=${ETHNAME}
3DEVICE=${ETHNAME}
4ONBOOT=yes
5BOOTPROTO=none
6TYPE=Ethernet
7IPADDR=${IP}
8PREFIX=${PREFIX}
9GATEWAY=${GATEWAY}
10DNS1=${PRIMARY_DNS}
11DNS2=${BACKUP_DNS}
12EOF
Ubuntu:
1# Ubuntu Server 18.04 LTS
2cat> /etc/netplan/01-netcfg.yaml <<-EOF
3network:
4 version: 2
5 renderer: networkd
6 ethernets:
7${ETHNAME}:
8 dhcp4: no
9 dhcp6: no
10 addresses: [${IP}/${PREFIX}]
11 gateway4: ${GATEWAY}
12 nameservers:
13 addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
14
15# Ubuntu Server 20.04 LTS
16cat> /etc/netplan/00-installer-config.yaml <<-EOF
17network:
18 version: 2
19 renderer: networkd
20 ethernets:
21${ETHNAME}:
22 dhcp4: no
23 dhcp6: no
24 addresses: [${IP}/${PREFIX}]
25 gateway4: ${GATEWAY}
26 nameservers:
27 addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
28EOF
29
30# Ubuntu Server 22.04/24.04 LTS
31cat> /etc/netplan/50-cloud-init.yaml <<-EOF
32network:
33 version: 2
34 renderer: networkd
35 ethernets:
36${ETHNAME}:
37 dhcp4: no
38 dhcp6: no
39 addresses: [${IP}/${PREFIX}]
40 routes:
41 - to: default
42 via: ${GATEWAY}
43 nameservers:
44 addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
45EOF
Debian:
1sed-ri-e"s/allow-hotplug/auto/g"-e"s/dhcp/static/g" /etc/network/interfaces
2sed-i'/static/a\address '${IP}'/'${PREFIX}'\ngateway '${GATEWAY}'\ndns-nameservers '${PRIMARY_DNS}' '${BACKUP_DNS}'\n' /etc/network/interfaces
openSUSE:
1cat> /etc/sysconfig/network/ifcfg-${ETHNAME}<<-EOF
2STARTMODE='auto'
3BOOTPROTO='static'
4IPADDR='${IP}/${PREFIX}'
5EOF
6touch /etc/sysconfig/network/routes
7cat> /etc/sysconfig/network/routes <<-EOF
8default ${GATEWAY} - -
9EOF
10sed-ri's/(NETCONFIG_DNS_STATIC_SERVERS=).*/\1"'${PRIMARY_DNS}' '${BACKUP_DNS}'"/g' /etc/sysconfig/network/config
Rocky:
1# 阿里云镜像站Rocky系统镜像源地址是:mirrors.aliyun.com/rockylinux,火山引擎镜像站Rocky系统镜像源地址是:mirrors.volces.com/rockylinux,后面地址是rockylinux,使用下面命令执行
2MIRROR=mirrors.aliyun.com
3sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repo
4
5# 搜狐镜像站Rocky系统镜像源地址是:mirrors.sohu.com/Rocky,后面地址是Rocky,使用下面命令执行
6MIRROR=mirrors.sohu.com
7sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^#baseurl=http://'${OLD_MIRROR}'/$contentdir|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo
8
9# 其余镜像站Rocky系统镜像源地址后面地址都是rocky,使用下面命令执行
10MIRROR=mirrors.tencent.com
11sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo
12
13dnf clean all && dnf makecache
Almalinux:
1MIRROR=mirrors.tencent.com
2OLD_MIRROR=$(sed-rn'/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/almalinux*.repo |head-1)
3sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^# baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/almalinux*.repo
CentOS Stream 9和10:
1cat update_mirror.pl
2#!/usr/bin/perl
3
4use strict;
5use warnings;
6use autodie;
7
8# 要修改镜像源,请去修改url变量!
9my $url='mirrors.aliyun.com';
10my $mirrors="https://$url/centos-stream";
11
12if(@ARGV <1){
13 die "Usage: $0...\n";
14}
15
16while(my $filename=shift @ARGV){
17 my $backup_filename=$filename.'.bak';
18rename$filename, $backup_filename;
19
20open my $input, "<", $backup_filename;
21open my $output, ">", $filename;
22
23while(<$input>){
24 s/^metalink/# metalink/;
25
26if(m/^name/){
27 my (undef, $repo, $arch)=split /-/;
28$repo=~ s/^\s+|\s+$//g;
29($arch= defined $arch ? lc($arch):'')=~ s/^\s+|\s+$//g;
30
31if($repo=~ /^Extras/){
32$_ .="baseurl=${mirrors}/SIGs/\$releasever-stream/extras".($arch eq 'source' ? "/${arch}/":"/\$basearch/")."extras-common\n";
33}else{
34$_ .="baseurl=${mirrors}/\$releasever-stream/$repo".($arch eq 'source' ? "/":"/\$basearch/").($arch ne '' ? "${arch}/tree/":"os")."\n";
35}
36}
37
38 print $output$_;
39}
40}
41
42rpm-q perl &> /dev/null ||{echo-e"\\033[01;31m "安装perl工具,请稍等..."\033[0m";yum -yinstall perl ;}
43
44perl ./update_mirror.pl /etc/yum.repos.d/centos*.repo
45
46dnf clean all && dnf makecache
CentOS Stream 8:
1MIRROR=mirrors.aliyun.com
2sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://'${MIRROR}'/centos-vault|g' /etc/yum.repos.d/CentOS-*.repo
3
4dnf clean all && dnf makecache
CentOS 7:
1MIRROR=mirrors.aliyun.com
2OS_RELEASE_FULL_VERSION=`cat /etc/centos-release |sed-rn's/^(CentOS Linux release )(.*)( \(Core\))/\2/p'`
3sed-i.bak-e's|^mirrorlist=|#mirrorlist=|g'-e's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://'${MIRROR}'/centos-vault|g'-e"s/\$releasever/${OS_RELEASE_FULL_VERSION}/g" /etc/yum.repos.d/CentOS-*.repo
4
5yum clean all && yum makecache
Ubuntu Server 24.04 LTS:
1MIRROR=mirrors.aliyun.com
2sed-ri"s@^(URIs: )(http.*://)(.*)(/ubuntu).?@\1https://${MIRROR}\4@g" /etc/apt/sources.list.d/ubuntu.sources
3
4apt update
Ubuntu Server 18.04/20.04/22.04 LTS:
1MIRROR=mirrors.aliyun.com
2OLD_MIRROR=`sed-rn"s@^deb http(.*)://(.*)/ubuntu/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`
3SECURITY_MIRROR=`sed-rn"s@^deb http(.*)://(.*)/ubuntu.* $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`
4sed-i.bak-e's@http.*://'${OLD_MIRROR}'@https://'${MIRROR}'@g'-e's@http.*://'${SECURITY_MIRROR}'@https://'${MIRROR}'@g' /etc/apt/sources.list
5
6apt update
Debian:
1MIRROR=mirrors.aliyun.com
2OLD_MIRROR=`sed-rn"s@^deb http(.*)://(.*)/debian/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`
3SECURITY_MIRROR=`sed-rn"s@^deb http(.*)://(.*)/debian-security $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`
4sed-ri.bak-e's/'${OLD_MIRROR}'/'${MIRROR}'/g'-e's/'${SECURITY_MIRROR}'/'${MIRROR}'/g'-e's/^(deb cdrom.*)/#\1/g' /etc/apt/sources.list
5
6apt update
openEuler:
1MIRROR=mirrors.aliyun.com
2OLD_MIRROR=$(awk -F'/''/^baseurl=/{print $3}' /etc/yum.repos.d/openEuler.repo |head-1)
3sed-i.bak-e's|^metalink=|#metalink=|g'-e's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'/openeuler|g' /etc/yum.repos.d/openEuler.repo
4
5dnf clean all && dnf makecache
AnolisOS:
1OLD_MIRROR=$(sed-rn'/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/AnolisOS*.repo |head-1)
2
3# 中国科学院软件研究所镜像站AnolisOS系统镜像源地址是:mirror.iscas.ac.cn/openanolis,后面地址是openanolis,使用下面命令执行
4MIRROR=mirror.iscas.ac.cn
5sed-i.bak-e's|http.*://'${OLD_MIRROR}'/anolis|https://'${MIRROR}'/openanolis|g' /etc/yum.repos.d/AnolisOS*.repo
6
7# 其余镜像站AnolisOS系统镜像源地址后面地址都是anolis,使用下面命令执行
8MIRROR=mirrors.aliyun.com
9sed-i.bak-e's|http.*://'${OLD_MIRROR}'/anolis|https://'${MIRROR}'/anolis|g' /etc/yum.repos.d/AnolisOS*.repo
10
11dnf clean all && dnf makecache
OpenCloudOS:
1MIRROR=mirrors.tencent.com
2OLD_MIRROR=$(awk -F'/''/^baseurl=/{print $3}' /etc/yum.repos.d/OpenCloudOS*.repo |head-1)
3sed-i.bak-e's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'|g' /etc/yum.repos.d/OpenCloudOS*.repo
4
5dnf clean all && dnf makecache
openSUSE:
1MIRROR=mirrors.aliyun.com
2OLD_MIRROR=$(awk -F'/''/^baseurl=/{print $3}' /etc/zypp/repos.d/repo-*.repo |head-1)
3sed-i.bak's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'/opensuse|g' /etc/zypp/repos.d/repo-*.repo
4
5zypper clean &&zypper refresh
1# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、openSUSE、Kylin Server、UOS Server
2systemctl disable --now firewalld
3
4# CentOS 7还要执行下面命令
5systemctl disable --now NetworkManager
6
7# Ubuntu
8systemctl disable --now ufw
9
10# Debian默认没有安装防火墙,不用设置
1# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、UOS Server
2setenforce 0
3sed-i's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
4
5# Ubuntu、Debian和openSUSE默认没有安装SELinux,不用设置
1# openSUSE发行版默认不启用SELinux,而是使用AppArmor来提供强制访问控制(MAC)功能,需要禁用AppArmor。
2systemctl disable --now apparmor
1timedatectl set-timezone Asia/Shanghai
2echo'Asia/Shanghai'>/etc/timezone
3
4# Ubuntu还要设置下面内容
5cat>> /etc/default/locale <<-EOF
6LC_TIME=en_DK.UTF-8
7EOF
1[root@rocky9 ~]# hostname -I
2172.31.0.9
3
4# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、UOS Server
5[root@rocky9 ~]# yum install -y chrony
6
7# openSUSE
8zypperinstall-y chrony
9
10# Ubuntu和Debian
11aptinstall-y chrony
12
13[root@rocky9 ~]# vim /etc/chrony.conf
14-bash: vim: command not found
15# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、Uos Server默认没有安装vim
16
17# 安装vim
18[root@rocky9 ~]# yum install -y vim
19
20# Rocky、Almalinux、CentOS、openEuler、AnolisOS、openSUSE、Kylin Server、Uos Server
21[root@rocky9 ~]# vim /etc/chrony.conf
22# Please consider joining the pool (https://www.pool.ntp.org/join.html). # 在这行下面
23# 删除pool行内容
24pool 2.rocky.pool.ntp.org iburst
25# 添加下面的ntp服务器
26server ntp.aliyun.com iburst
27server ntp.tencent.com iburst
28server ntp.tuna.tsinghua.edu.cn iburst
29
30# 把下面内容
31#allow 192.168.0.0/16
32# 修改为
33allow 0.0.0.0/0 # 指定允许同步的网段
34
35# 把下面内容去掉注释
36#local stratum 10
37# 修改为
38local stratum 10# 当互联网无法连接,仍然可以为客户端提供时间同步服务
39
40# OpenCloudOS
41[root@opencloudos9 ~]# vim /etc/chrony.conf
42# Use public servers # 在这行下面
43# 删除下面所有的server的行
44server ntp1.tencent.com iburst
45server ntp2.tencent.com iburst
46server cn.pool.ntp.org iburst
47
48server time1.tencentyun.com iburst
49server time2.tencentyun.com iburst
50server time3.tencentyun.com iburst
51# 添加下面的ntp服务器
52server ntp.aliyun.com iburst
53server ntp.tencent.com iburst
54server ntp.tuna.tsinghua.edu.cn iburst
55
56# 把下面内容
57#allow 192.168.0.0/16
58# 修改为
59allow 0.0.0.0/0 # 指定允许同步的网段
60
61# 把下面内容去掉注释
62#local stratum 10
63# 修改为
64local stratum 10# 当互联网无法连接,仍然可以为客户端提供时间同步服务
65
66# Ubuntu
67root@ubuntu2404:~# vim /etc/chrony/chrony.conf
68# See http://www.pool.ntp.org/join.html for more information. # 在这行下面
69# 删除pool行内容
70pool ntp.ubuntu.com iburst maxsources 4
71pool 0.ubuntu.pool.ntp.org iburst maxsources 1
72pool 1.ubuntu.pool.ntp.org iburst maxsources 1
73pool 2.ubuntu.pool.ntp.org iburst maxsources 2
74# 添加下面的ntp服务器
75server ntp.aliyun.com iburst
76server ntp.tencent.com iburst
77server ntp.tuna.tsinghua.edu.cn iburst
78
79# 在最后面添加下面内容
80allow 0.0.0.0/0
81local stratum 10
82
83# Debian
84root@debian12:~# vim /etc/chrony/chrony.conf
85# Use Debian vendor zone. # 在这行下面
86# 删除pool行内容
87pool 2.debian.pool.ntp.org iburst
88# 添加下面的ntp服务器
89server ntp.aliyun.com iburst
90server ntp.tencent.com iburst
91server ntp.tuna.tsinghua.edu.cn iburst
92
93# 在最后面添加下面内容
94allow 0.0.0.0/0
95local stratum 10f
96
97# 使用下面命令直接替换
98NTP_SERVER1=ntp.aliyun.com
99NTP_SERVER2=ntp.tencent.com
100NTP_SERVER3=ntp.tuna.tsinghua.edu.cn
101
102# Rocky、Almalinux、CentOS、openEuler、AnolisOS、openSUSE、Kylin Server、UOS Server
103sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Please consider .*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER2}' iburst'-e's@^#allow.*@allow 0.0.0.0/0@'-e's@^#local.*@local stratum 10@' /etc/chrony.conf
104
105# OpenCloudOS
106sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Use public.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst'-e's@^#allow.*@allow 0.0.0.0/0@'-e's@^#local.*@local stratum 10@' /etc/chrony.conf
107
108# Ubuntu
109sed-i-e'/^pool.*/d'-e'/^# See http:.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst' /etc/chrony/chrony.conf
110echo"allow 0.0.0.0/0">> /etc/chrony/chrony.conf
111echo"local stratum 10">> /etc/chrony/chrony.conf
112
113# Debian
114sed-i-e'/^pool.*/d'-e'/^# Use Debian.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst' /etc/chrony/chrony.conf
115echo"allow 0.0.0.0/0">> /etc/chrony/chrony.conf
116echo"local stratum 10">> /etc/chrony/chrony.conf
117
118[root@rocky9 ~]# systemctl restart chronyd
119[root@rocky9 ~]# systemctl enable --now chronyd
120
121[root@rocky9 ~]# ss -ntul
122Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
123udp UNCONN 00127.0.0.1:323 0.0.0.0:*
124udp UNCONN 000.0.0.0:123 0.0.0.0:*
125udp UNCONN 00[::1]:323 [::]:*
126tcp LISTEN 01280.0.0.0:22 0.0.0.0:*
127tcp LISTEN 0128[::]:22 [::]:*
128# 服务启动后会打开端口123/udp
129
130[root@rocky9 ~]# chronyc sources -nv
131MS Name/IP address Stratum Poll Reach LastRx Last sample
132===============================================================================
133^* 203.107.6.88 2637734 +1042us[+3109us] +/- 63ms # 带*(星号)表示是从这个时间服务器同步的时间。
134^- 106.55.184.199 2637799 +5187us[+7169us] +/- 60ms
135^? 101.6.6.172 080 - +0ns[ +0ns] +/- 0ns
# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、UOS Server
2[root@rocky9 ~]# yum install -y chrony
3
4# openSUSE
5zypperinstall-y chrony
6
7# Ubuntu和Debian
8aptinstall-y chrony
9
10[root@rocky9 ~]# vim /etc/chrony.conf
11-bash: vim: command not found
12# Rocky和CentOS默认没有安装vim
13
14# 安装vim
15[root@rocky9 ~]# yum install -y vim
16
17# Rocky、Almalinux、CentOS、openEuler、AnolisOS、openSUSE、Kylin Server、Uos Server
18[root@rocky9 ~]# vim /etc/chrony.conf
19# Please consider joining the pool (https://www.pool.ntp.org/join.html). # 在这行下面
20# 删除pool行内容
21pool 2.rocky.pool.ntp.org iburst
22# 添加ntp Server端
23server 172.31.0.9 iburst
24
25# OpenCloudOS
26[root@opencloudos9 ~]# vim /etc/chrony.conf
27# Use public servers # 在这行下面
28# 删除下面所有的server的行
29server ntp1.tencent.com iburst
30server ntp2.tencent.com iburst
31server cn.pool.ntp.org iburst
32
33server time1.tencentyun.com iburst
34server time2.tencentyun.com iburst
35server time3.tencentyun.com iburst
36# 添加ntp Server端
37server 172.31.0.9 iburst
38
39# Ubuntu
40root@ubuntu2404:~# vim /etc/chrony/chrony.conf
41# See http://www.pool.ntp.org/join.html for more information. # 在这行下面
42# 删除pool行内容
43pool ntp.ubuntu.com iburst maxsources 4
44pool 0.ubuntu.pool.ntp.org iburst maxsources 1
45pool 1.ubuntu.pool.ntp.org iburst maxsources 1
46pool 2.ubuntu.pool.ntp.org iburst maxsources 2
47# 添加ntp Server端
48server 172.31.0.9 iburst
49
50# Debian
51root@debian12:~# vim /etc/chrony/chrony.conf
52# Use Debian vendor zone. # 在这行下面
53# 删除pool行内容
54pool 2.debian.pool.ntp.org iburst
55# 添加ntp Server端
56server 172.31.0.9 iburst
57
58# 使用下面命令直接替换
59SERVER=172.31.0.9
60
61# Rocky、Almalinux、CentOS、openEuler、AnolisOS、openSUSE、Kylin Server、UOS Server
62sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Please consider .*/a\server '${SERVER}' iburst' /etc/chrony.conf
63
64# OpenCloudOS
65sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Use public.*/a\server '${SERVER}' iburst' /etc/chrony.conf
66
67# Ubuntu
68sed-i-e'/^pool.*/d'-e'/^# See http:.*/a\server '${SERVER}' iburst' /etc/chrony/chrony.conf
69
70# Debian
71sed-i-e'/^pool.*/d'-e'/^# Use Debian.*/a\server '${SERVER}' iburst' /etc/chrony/chrony.conf
72
73[root@rocky9 ~]# systemctl restart chronyd
74[root@rocky9 ~]# systemctl enable --now chronyd
75
76# 确认同步成功
77[root@rocky9 ~]# chronyc sources -nv
78MS Name/IP address Stratum Poll Reach LastRx Last sample
79===============================================================================
80^* 172.31.0.9 361718 -47us[ +43us] +/- 60ms
81# 带*(星号)表示是从这个时间服务器同步的时间。
Shell脚本源码地址:
Gitee:https://gitee.com/raymond9/shell
Github:https://github.com/raymond999999/shell
可以去上面的Gitee或Github代码仓库拉取脚本。
1[root@rocky9 ~]# cat install_chrony_server_v3.sh
2#!/bin/bash
3#
4#**********************************************************************************
5#Author: Raymond
6#QQ: 88563128
7#MP: Raymond运维
8#Date: 2025-09-22
9#FileName: install_chrony_server_v3.sh
10#URL: https://wx.zsxq.com/group/15555885545422
11#Description: The chrony server script install supports
12# “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7,
13# CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS,
14# AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11,
15# UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,
16# Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
17#Copyright (C): 2025 All rights reserved
18#**********************************************************************************
19COLOR="echo -e \\033[01;31m"
20END='\033[0m'
21
22os(){
23. /etc/os-release
24MAIN_NAME=`sed-rn'/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
25if[${MAIN_NAME}=="Kylin"];then
26MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
27else
28MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
29fi
30if[${MAIN_NAME}=="Ubuntu"-o${MAIN_NAME}=="Debian"];then
31FULL_NAME="${PRETTY_NAME}"
32elif[${MAIN_NAME}=="UOS"];then
33FULL_NAME="${NAME}"
34else
35FULL_NAME="${NAME}${VERSION_ID}"
36fi
37}
38
39os
40NTP_SERVER1=ntp.aliyun.com
41NTP_SERVER2=ntp.tencent.com
42NTP_SERVER3=ntp.tuna.tsinghua.edu.cn
43
44install_chrony(){
45if[${MAIN_NAME}=="Rocky"-o${MAIN_NAME}=="AlmaLinux"-o${MAIN_NAME}=="CentOS"-o${MAIN_NAME}=="openEuler"-o${MAIN_NAME}=="Anolis"-o${MAIN_NAME}=="OpenCloudOS"-o${MAIN_NAME}=="openSUSE"-o${MAIN_NAME}=="Kylin"-o${MAIN_NAME}=="UOS"];then
46if[${MAIN_NAME}=="openSUSE"];then
47INSTALL_TOOL='zypper'
48else
49INSTALL_TOOL='yum'
50fi
51rpm-q chrony &> /dev/null ||{${COLOR}"安装chrony包,请稍等......"${END};${INSTALL_TOOL}install-y chrony &> /dev/null;}
52if[${MAIN_NAME}=="OpenCloudOS"];then
53sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Use public.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst'-e's@^#allow.*@allow 0.0.0.0/0@'-e's@^#local.*@local stratum 10@' /etc/chrony.conf
54else
55sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Please consider .*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst'-e's@^#allow.*@allow 0.0.0.0/0@'-e's@^#local.*@local stratum 10@' /etc/chrony.conf
56fi
57else
58 dpkg -s chrony &>/dev/null ||{${COLOR}"安装chrony包,请稍等......"${END};aptinstall-y chrony &> /dev/null;}
59if[${MAIN_NAME}=="Ubuntu"];then
60sed-i-e'/^pool.*/d'-e'/^# See http:.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst' /etc/chrony/chrony.conf
61else
62sed-i-e'/^pool.*/d'-e'/^# Use Debian.*/a\server '${NTP_SERVER1}' iburst\nserver '${NTP_SERVER2}' iburst\nserver '${NTP_SERVER3}' iburst' /etc/chrony/chrony.conf
63fi
64echo"allow 0.0.0.0/0">> /etc/chrony/chrony.conf
65echo"local stratum 10">> /etc/chrony/chrony.conf
66fi
67 systemctl restart chronyd && systemctl enable--now chronyd &> /dev/null
68 systemctl is-active chronyd &> /dev/null ||{${COLOR}"chrony 启动失败,退出!"${END};exit;}
69${COLOR}"${FULL_NAME}操作系统,chrony服务端安装完成!"${END}
70}
71
72main(){
73 install_chrony
74}
75
76if[${MAIN_NAME}=="Rocky"];then
77if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
78 main
79fi
80elif[${MAIN_NAME}=="AlmaLinux"];then
81if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
82 main
83fi
84elif[${MAIN_NAME}=="CentOS"];then
85if[${MAIN_VERSION_ID}==7-o${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
86 main
87fi
88elif[${MAIN_NAME}=="openEuler"];then
89if[${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
90 main
91fi
92elif[${MAIN_NAME}=="Anolis"];then
93if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==23];then
94 main
95fi
96elif[${MAIN_NAME}=='OpenCloudOS'];then
97if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9];then
98 main
99fi
100elif[${MAIN_NAME}=="Kylin"];then
101if[${MAIN_VERSION_ID}==10-o${MAIN_VERSION_ID}==11];then
102 main
103fi
104elif[${MAIN_NAME}=="UOS"];then
105if[${MAIN_VERSION_ID}==20];then
106 main
107fi
108elif[${MAIN_NAME}=="openSUSE"];then
109if[${MAIN_VERSION_ID}==15];then
110 main
111fi
112elif[${MAIN_NAME}=="Ubuntu"];then
113if[${MAIN_VERSION_ID}==18-o${MAIN_VERSION_ID}==20-o${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
114 main
115fi
116elif[${MAIN_NAME}=='Debian'];then
117if[${MAIN_VERSION_ID}==11-o${MAIN_VERSION_ID}==12-o${MAIN_VERSION_ID}==13];then
118 main
119fi
120else
121${COLOR}"此脚本不支持${FULL_NAME}操作系统!"${END}
122fi
1[root@rocky9 ~]# cat install_chrony_client_v3.sh
2#!/bin/bash
3#
4#**********************************************************************************
5#Author: Raymond
6#QQ: 88563128
7#MP: Raymond运维
8#Date: 2025-09-22
9#FileName: install_chrony_client_v3.sh
10#URL: https://wx.zsxq.com/group/15555885545422
11#Description: The chrony client script install supports
12# “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7,
13# CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS,
14# AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11,
15# UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,
16# Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
17#Copyright (C): 2025 All rights reserved
18#**********************************************************************************
19COLOR="echo -e \\033[01;31m"
20END='\033[0m'
21
22os(){
23. /etc/os-release
24MAIN_NAME=`sed-rn'/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
25if[${MAIN_NAME}=="Kylin"];then
26MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
27else
28MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
29fi
30if[${MAIN_NAME}=="Ubuntu"-o${MAIN_NAME}=="Debian"];then
31FULL_NAME="${PRETTY_NAME}"
32elif[${MAIN_NAME}=="UOS"];then
33FULL_NAME="${NAME}"
34else
35FULL_NAME="${NAME}${VERSION_ID}"
36fi
37}
38
39os
40SERVER=172.31.0.9
41
42install_chrony(){
43if[${MAIN_NAME}=="Rocky"-o${MAIN_NAME}=="AlmaLinux"-o${MAIN_NAME}=="CentOS"-o${MAIN_NAME}=="openEuler"-o${MAIN_NAME}=="Anolis"-o${MAIN_NAME}=="OpenCloudOS"-o${MAIN_NAME}=="openSUSE"-o${MAIN_NAME}=="Kylin"-o${MAIN_NAME}=="UOS"];then
44if[${MAIN_NAME}=="openSUSE"];then
45INSTALL_TOOL='zypper'
46else
47INSTALL_TOOL='yum'
48fi
49rpm-q chrony &> /dev/null ||{${COLOR}"安装chrony包,请稍等......"${END};${INSTALL_TOOL}install-y chrony &> /dev/null;}
50if[${MAIN_NAME}=="OpenCloudOS"];then
51sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Use public.*/a\server '${SERVER}' iburst' /etc/chrony.conf
52else
53sed-i-e'/^pool.*/d'-e'/^server.*/d'-e'/^# Please consider .*/a\server '${SERVER}' iburst' /etc/chrony.conf
54fi
55else
56 dpkg -s chrony &>/dev/null ||{${COLOR}"安装chrony包,请稍等..."${END};aptinstall-y chrony &> /dev/null;}
57if[${MAIN_NAME}=="Ubuntu"];then
58sed-i-e'/^pool.*/d'-e'/^# See http:.*/a\server '${SERVER}' iburst' /etc/chrony/chrony.conf
59else
60sed-i-e'/^pool.*/d'-e'/^# Use Debian.*/a\server '${SERVER}' iburst' /etc/chrony/chrony.conf
61fi
62fi
63 systemctl restart chronyd && systemctl enable--now chronyd &> /dev/null
64 systemctl is-active chronyd &> /dev/null ||{${COLOR}"chrony 启动失败,退出!"${END};exit;}
65${COLOR}"${FULL_NAME}操作系统,chrony客户端安装完成!"${END}
66}
67
68main(){
69 install_chrony
70}
71
72if[${MAIN_NAME}=="Rocky"];then
73if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
74 main
75fi
76elif[${MAIN_NAME}=="AlmaLinux"];then
77if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
78 main
79fi
80elif[${MAIN_NAME}=="CentOS"];then
81if[${MAIN_VERSION_ID}==7-o${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
82 main
83fi
84elif[${MAIN_NAME}=="openEuler"];then
85if[${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
86 main
87fi
88elif[${MAIN_NAME}=="Anolis"];then
89if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==23];then
90 main
91fi
92elif[${MAIN_NAME}=='OpenCloudOS'];then
93if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9];then
94 main
95fi
96elif[${MAIN_NAME}=="Kylin"];then
97if[${MAIN_VERSION_ID}==10-o${MAIN_VERSION_ID}==11];then
98 main
99fi
100elif[${MAIN_NAME}=="UOS"];then
101if[${MAIN_VERSION_ID}==20];then
102 main
103fi
104elif[${MAIN_NAME}=="openSUSE"];then
105if[${MAIN_VERSION_ID}==15];then
106 main
107fi
108elif[${MAIN_NAME}=="Ubuntu"];then
109if[${MAIN_VERSION_ID}==18-o${MAIN_VERSION_ID}==20-o${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
110 main
111fi
112elif[${MAIN_NAME}=='Debian'];then
113if[${MAIN_VERSION_ID}==11-o${MAIN_VERSION_ID}==12-o${MAIN_VERSION_ID}==13];then
114 main
115fi
116else
117${COLOR}"此脚本不支持${FULL_NAME}操作系统!"${END}
118fi
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。