uptime、who -b、systemctl status、/proc/uptime和last命令均可获取Linux系统启动时间;uptime -s显示精确启动时间戳,who -b读取utmp中init写入的启动记录,systemctl status的Since字段标识systemd初始化完成时刻,/proc/uptime结合date可反推启动时间,last reboot追溯wtmp中的重启历史。
如果您需要确认Linux系统自上次启动以来已运行多久,或精确获知系统开机的具体时间点,则需调用内核或系统服务记录的原始启动信息。以下是多种可靠方法:
一、使用uptime命令查看运行时长及启动时间
uptime命令直接读取内核维护的运行计时器,输出中包含系统当前时间、运行时长(up字段)以及平均负载。配合-p和-s选项可分别提取简洁运行时长和精确启动时间戳。
1、执行基础uptime命令查看概览:
uptime
2、仅显示系统运行时间(格式化为“X days, Y hours, Z minutes”):
uptime -p
3、显示系统启动的完整日期与时间:
uptime -s
二、使用who -b命令获取精确开机时间
who -b从/var/run/utmp文件中读取最近一次系统引导记录,该时间由init进程在启动时写入,具有高可信度,适用于审计与故障复盘。
1、运行命令查询启动时间:
who -b
2、输出中“system boot”后的内容即为确切开机时间,例如:system boot 2026-01-05 09:23:17
三、通过systemctl status提取Since字段
在基于systemd的系统中,systemctl status会显示系统服务管理器的启动上下文,其开头的Since字段明确标注了systemd初始化完成的时间,等同于用户空间服务就绪时刻。
1、执行状态查询:
systemctl status