【使用场景】
在银河麒麟桌面操作系统V10SP1-2303版本中使用脚本一键修改NTP服务器地址为自定义地址。
【操作步骤】
步骤1. 编写shell脚本
```bash
desktop2303@desktop2303-pc:~$ vim setntptimeserver.sh
#!/bin/bashfunction modifykylinconf() {
# 检查是否已存在目标配置
if egrep -q "^NTP\s*=\s$new_ntp" /etc/systemd/timesyncd.conf.d/kylin.conf; then
echo "配置文件中已存在 NTP = $new_ntp,无需更新"
else
# 更新配置文件
if egrep -q "^NTP\s*=\s+.*$" /etc/systemd/timesyncd.conf.d/kylin.conf; then
# sudo sed -ri "s/^\s*NTP=\s+.*$/NTP=$new_ntp/" /etc/systemd/timesyncd.conf.d/kylin.conf
sudo sed -ri "s/^NTP\\s*=\\s.*$/NTP = 80.18.1.11/" /etc/systemd/timesyncd.conf.d/kylin.conf
echo "已更新现有NTP配置"
else
# sudo echo "NTP = $new_ntp" >> /etc/systemd/timesyncd.conf.d/kylin.conf
echo "已添加新的NTP配置"
fi
# # 重启服务使配置生效
sudo systemctl restart systemd-times