AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 792723
Accepted
Fravadona
Fravadona
Asked: 2025-03-20 06:03:17 +0800 CST2025-03-20 06:03:17 +0800 CST 2025-03-20 06:03:17 +0800 CST

如何确定 Linux 计算机的安装时间?[重复]

  • 772
此问题这里已有答案:
如何查找 Linux 系统安装的时间? (18 个答案)
4 天前关闭。

有什么好方法可以知道 Linux 计算机的安装时间?

我能想到的任何解决方案都将基于stat -c %y,例如。

stat --format %y /etc/os-release /etc/skel
2022-04-06 12:00:18.402055811 +0200
2022-04-06 12:00:19.895055839 +0200

但使用修改时间有明显的缺点,出生日期可能不受支持。

linux
  • 2 2 个回答
  • 1062 Views

2 个回答

  • Voted
  1. Best Answer
    Stephen Kitt
    2025-03-20T06:33:32+08:002025-03-20T06:33:32+08:00

    我认为您能得到的最接近的(或者更准确地说,您所能期望的最好的)是根文件系统本身的创建时间戳,假设该文件系统是在安装期间创建的(并且在创建时时钟设置正确)。

    因此,如果在 Ext4 上:

    sudo dumpe2fs -h "$(awk '$2 == "/" { print $1 }' /proc/mounts)" | grep "Filesystem created"
    

    如果在 btrfs 上:

    sudo btrfs subvolume show / | grep 'Creation time'
    

    如果在 zfs 上:

    date -d "@$(zfs get -Hpo value creation "$(awk '$2 == "/" {print $1}' /proc/mounts)")" +%FT%T%:z
    

    在没有单独存储创建时间戳的文件系统上,可以使用根 inode 的创建(“诞生”)时间进行替换:

    stat -c %w /
    

    (或者任何其他文件系统挂载点/)。

    如果文件系统是从其他地方复制过来的,这种方法就行不通了;在这种情况下,系统中有些文件在安装过程中应该被设置为系统独有的,但这也不是 100% 可靠的。 其中一个文件是/etc/machine-id,因此您可以查看该文件的修改日期:

    stat -c %y /etc/machine-id
    

    但请记住,系统安装后可以删除并重新创建该文件。

    您还可以通过检查已安装的硬件(假设是物理系统)来确定下限。不幸的是,没有可靠的方法来确定您在系统中找到的任何时间戳是否是可靠的指标!

    如果系统是虚拟机,您可能能够通过查看虚拟机管理程序的元数据来确定其创建日期。

    • 6
  2. ReflectYourCharacter
    2025-03-20T08:06:04+08:002025-03-20T08:06:04+08:00

    一个非常有趣的问题。我不知道是否 100% 可以找到答案,这取决于是否使用预配置的内核、initrd、GRUB、文件系统等,以及哪个系统使用哪个时间戳。

    而且@Stephen Kitt已经提到了最重要的命令之一,我现在没有将其包括在内。

    也许并非所有命令本身都有意义,而应该组合使用。

    我在这里使用 Debian 12.x GNU/Linux 执行这些命令,并且以 root 身份运行所有命令。

    以 root 身份运行脚本,并将其替换/dev/sda2为您的设备/分区:

    我从以下位置获取了最早的日期:文件系统创建时间 ext4

    /sbin/tune2fs -l /dev/sda2 | grep 'Filesystem created'
    

    创建运行脚本system_ctrl_date:

    #!/bin/bash
    
    # 1. About the installation time of files
    echo "1. Installation time of /etc/os-release:"
    stat -c %y /etc/os-release
    echo ""
    
    # 2. If the filesystem stores birth timestamps
    echo "2. Oldest file in /etc (birth timestamp):"
    ls -lt --time=birth /etc/ | tail -1
    echo ""
    
    # 3. About the filesystem, in this case ext4
    echo "3. Filesystem creation time (ext4):"
    /sbin/tune2fs -l /dev/sda2 | grep 'Filesystem created'
    echo ""
    
    # 4. About the package manager
    echo "4. Oldest file in /var/log/installer/:"
    ls -lt /var/log/installer/
    echo ""
    
    # 5. About the bootloader/GRUB installation time
    echo "5. GRUB installation time:"
    stat /boot/grub | grep 'Birth'
    echo ""
    
    # 6. EFI System Partition installation date
    echo "6. EFI System Partition installation date:"
    ls -lt /boot/efi
    echo ""
    
    # 7. If it is not mounted under /boot/efi and is vfat
    echo "7. Oldest file in vfat filesystem (if not mounted under /boot/efi):"
    ls -lt $(findmnt -no TARGET -t vfat)
    echo ""
    
    # 8. About the creation time of the root filesystem
    echo "8. Creation time of the root filesystem:"
    stat -c %w /
    echo ""
    
    # 9. About the oldest file in the home directory
    echo "9. Oldest file in /home:"
    find /home -type f -printf '%T+ %p\n' | sort | head -n 1
    echo ""
    
    # 10. About the creation time of the kernel image
    echo "10. Creation time of the kernel image:"
    stat -c %w /boot/vmlinuz-$(uname -r)
    echo ""
    
    # 11. About the creation time of the initramfs
    echo "11. Creation time of the initramfs:"
    stat -c %w /boot/initrd.img-$(uname -r)
    echo ""
    
    # 12. About the creation time of the systemd directory
    echo "12. Creation time of the systemd directory:"
    stat -c %w /etc/systemd
    echo ""
    
    # 13. About the creation time of the user account
    echo "13. Creation time of /etc/passwd:"
    stat -c %w /etc/passwd
    echo ""
    
    # 14. About the creation time of the log directory
    echo "14. Creation time of /var/log:"
    stat -c %w /var/log
    echo ""
    
    # 15. About the creation time of the package manager cache
    echo "15. Creation time of /var/cache/apt:"
    stat -c %w /var/cache/apt
    echo ""
    
    # 16. About the creation time of the cron directory
    echo "16. Creation time of /etc/cron.d:"
    stat -c %w /etc/cron.d
    echo ""
    
    # 17. About the creation time of the network configuration directory
    echo "17. Creation time of /etc/network:"
    stat -c %w /etc/network
    echo ""
    

    我还有一个读取 BIOS、UEFI、引导加载程序和硬件信息的脚本,但我认为它没有意义,因为它来自制造商。

    • 5

相关问题

  • 有没有办法让 ls 只显示某些目录的隐藏文件?

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

  • 需要一些系统调用

  • astyle 不会更改源文件格式

  • 通过标签将根文件系统传递给linux内核

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve