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 date
    sudo date -s "$date_str"

    # Sync hardware clock with system time
    sudo hwclock --systohc
    echo "System time synchronized with $url and hardware clock updated"
else
    echo "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 date
    sudo date -s "$date_str"

    # Sync hardware clock with system time
    sudo hwclock --systohc
    echo "System time synchronized with $url and hardware clock updated"
else
    echo "Failed to fetch date from $url"
fi

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值