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

相关推荐

  1. Linux限定网络工具环境时间同步

    2024-02-23 02:18:01       54 阅读
  2. ubuntu同步网络时间

    2024-02-23 02:18:01       38 阅读
  3. linux时间同步

    2024-02-23 02:18:01       58 阅读
  4. Linux-时间同步服务器

    2024-02-23 02:18:01       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 02:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 02:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 02:18:01       82 阅读
  4. Python语言-面向对象

    2024-02-23 02:18:01       91 阅读

热门阅读

  1. vue3 #组件通信#子传父#defineEmits

    2024-02-23 02:18:01       44 阅读
  2. 学习总结21

    2024-02-23 02:18:01       40 阅读
  3. 力扣代码学习日记五

    2024-02-23 02:18:01       49 阅读
  4. 【ElasticSearch】常用查询格式介绍

    2024-02-23 02:18:01       52 阅读
  5. 动态规划-背包问题-二维费用背包 & 分组背包

    2024-02-23 02:18:01       55 阅读
  6. 前端常见面试题之vue2

    2024-02-23 02:18:01       54 阅读
  7. GB/T 43565-2023 中小学合成材料面层篮球场地检测

    2024-02-23 02:18:01       58 阅读
  8. Oracle使用exp和imp命令实现数据库导出导入

    2024-02-23 02:18:01       45 阅读
  9. 【面试题】谈谈MySQL的事务

    2024-02-23 02:18:01       46 阅读
  10. 简单介绍一下WebRTC中NACK机制

    2024-02-23 02:18:01       44 阅读