Linux限定网络和工具环境下时间同步
使用curl或wget工具同步某网站时间到本地环境。
使用curl工具
#!/bin/bash# Replace 'example.com' with the domain you want to query
url="http://example.com"# Fetch HTTP header and extract Date field
date_str=$(curl -sI "$url" | grep -i "^Date:" | sed 's/Date: //I')if [ -n "$date_str" ]; then# Set system date using the fetched datesudo date -s "$date_str"# Sync hardware clock with system timesudo hwclock --systohcecho "System time synchronized with $url and hardware clock updated"
elseecho "Failed to fetch date from $url"
fi
使用wget工具
#!/bin/bash# Replace 'example.com' with the domain you want to query
url="http://example.com"# Fetch HTTP header and extract Date field
date_str=$(wget --server-response -O /dev/null "$url" 2>&1 | grep -i "^ Date:" | sed 's/^ Date: //I')if [ -n "$date_str" ]; then# Set system date using the fetched datesudo date -s "$date_str"# Sync hardware clock with system timesudo hwclock --systohcecho "System time synchronized with $url and hardware clock updated"
elseecho "Failed to fetch date from $url"
fi