Linux 伺服器 NTP 校時相關指令紀錄

2019/01/24 18:36 0 評論 A+ a-

紀錄一下有關 CentOS 中 NTP 的設定方式
安裝校時服務
yum install ntp

設定 NTP BIOS 的時間也同步調整
vi /etc/sysconfig/ntpd
設定或加上 SYNC_HWCLOCK=yes

加入台灣的校時伺服器
vi /etc/ntp.conf
server tock.stdtime.gov.tw
server watch.stdtime.gov.tw
server time.stdtime.gov.tw
server clock.stdtime.gov.tw
server tick.stdtime.gov.tw
#server 169.254.169.123 #AWS主機可優先使用此AWS校時伺服器

CentOS 6 開機自動啟動 ntp 校時服務
chkconfig ntpd on

CentOS 7 開機自動啟動 ntp 校時服務
systemctl enable ntpd

手動同步系統 & BIOS 時間
ntpdate time.stdtime.gov.tw
hwclock -w

CentOS 6 查看時區
ls -l /etc/localtime

CentOS 7 查看時區
timedatectl

CentOS 6 設定時區 (設定為台北)
cp /etc/localtime /root/old.timezone
rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime

CentOS 7 設定時區 (設定為台北)
timedatectl set-timezone Asia/Taipei

設定防火牆 CentOS6
iptables -A INPUT -m udp -p udp --sport 123 -d 0/0 --dport 123 -j ACCEPT
iptables-save

設定防火牆 CentOS7
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload

CentOS 6 重新啟動校時伺服器
service ntpd restart

CentOS 7 重新啟動校時伺服器
systemctl restart ntpd

確認是否成功 (需等待五分鐘在確認狀態)
ntpstat

如果沒有使用 ntp 做校時, 則要手動排程校時, crontab 設定範例如下
#校時
55 4 * * * root /usr/sbin/ntpdate server tick.stdtime.gov.tw && /sbin/hwclock -w

歡迎留下寶貴的建議,或者給我一個鼓勵喔!