linux 自动同步时间脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

OS_NAME=$( cat /etc/os-release | grep ^NAME | cut -d'=' -f2 | sed 's/\"//gI' )

case "$OS_NAME" in
"CentOS Linux")
sudo yum install ntpdate ntp -y

sudo timedatectl set-timezone UTC
sudo ntpdate -u ntp.aliyun.com time.windows.com time.apple.com time.google.com time.cloudflare.com
sudo hwclock --systohc # 写入硬件

sudo systemctl enable ntpd
sudo systemctl start ntpd
;;
"Ubuntu" | "Linux Mint")
sudo apt install ntpdate ntp -y

sudo timedatectl set-timezone UTC
sudo ntpdate -u ntp.aliyun.com time.windows.com time.apple.com time.google.com time.cloudflare.com
sudo hwclock --systohc # 写入硬件

sudo systemctl enable ntp
sudo systemctl start ntp
;;
esac