Linux服务器开机时间查询方法大全
本文主要介绍Linux服务器开机时间查询方法大全,阐述如何查看开机时间及各种方法的优缺点。从系统自带命令到第三方软件,不同的方法适用于不同场景。
1、系统自带命令查询
Linux系统下有许多可以查询开机时间的命令,其中最常用的是`uptime`和`who -b`。`uptime`命令可以显示系统运行时间、负载平均值等信息,其中`up`表示系统运行时间,单位为天、小时和分钟。例如:
~$ uptime19:58:16 up 2:08, 2 users, load average: 0.00, 0.00, 0.00这个系统的运行时间为2小时08分。
`who -b`则可以显示系统的启动时间:
~$ who -b system boot 2021-07-01 20:43这个系统的启动时间为2021年7月1日20点43分。
这两个命令都是系统自带的,使用方便,但缺点是只能显示系统总运行时间和启动时间,并不能详细地了解系统开机的过程。
2、查看/var/log/dmesg日志文件
/var/log/dmesg包含了系统启动时内核打印的信息,其中也包含了开机时间。可以使用如下命令查看:
~$ cat /var/log/dmesg grep "booted"[ 0.000000] tsc: Fast TSC calibration failed [ 0.000000] ACPI: LAPIC (acpi_id[0x00] lapic_id[0x00] enabled) [ 0.000000] NR_IRQS:4352 nr_irqs:512 16 [ 0.000000] Console: colour VGA+ 80x25 [ 0.000000] bootconsole [earlyser0] enabled [ 0.036004] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes) [ 0.042128] CPU1: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz stepping 0a [ 0.042128] boot_id: 06f872d6260e4cf881bfdc65a19ba92d [ 4.048456] systemd[1]: Started Load Kernel Modules.其中`boot_id` 后面的一串数字就是系统的启动ID,使用下面的命令可以将其转换成正常的日期格式:
~$ systemd-analyze times grep "boot" Startup finished in 4.362s (firmware) + 5.023s (loader) + 6.978s (kernel) + 1.413s (userspace) = 18.777s system-efi-boot\x2d8f8d0203\x2d83f5\x2d440c\x2d9c4c\x2d63c73213f3b1.service loaded active exited EFI System Partition Mount\x2d2.mount system-efi-boot\x2d8f8d0203\x2d83f5\x2d440c\x2d9c4c\x2d63c73213f3b1.swap loaded active waiting /boot/efi.mount systemd-hostnamed.service loaded active running Hostname Service systemd-localed.service loaded active exited Locale Service systemd-timesyncd.service loaded active running Network Time Synchronization systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown systemd-user-sessions.service loaded active exited Permit User Sessions user@1000.service loaded active running User Manager for UID 1000 systemd-update-utmp-runlevel.service loaded inactive dead Update UTMP about System Runlevel Changes system-boot.mount loaded active mounted /boot.mount system-systemd\x2dfsck.slice loaded active active system-systemd\x2dfsck.slice systemd-fsck-root.service loaded active exited File System Check on Root Device systemd-fsck@dev-disk-by\x2duuid-56C1\x2d064F.service loaded inactive dead File System Check on /dev/disk/by-uuid/56C1-064F system.slice loaded active active system.slice user.slice loaded active active user.slice system-serial\x2dgetty.slice loaded active active system-serial\x2dgetty.slice getty@tty2.service loaded active running Getty on tty2 getty@tty1.service loaded active running Getty on tty1 getty@tty3.service loaded active running Getty on tty3 getty@tty4.service loaded active running Getty on tty4 getty@tty5.service loaded active running Getty on tty5 getty@tty6.service loaded active running Getty on tty6 graphical.target loaded active active Graphical Interface multi-user.target loaded active active Multi-User System system-getty.slice loaded active active system-getty.slice -.slice loaded inactive dead -.slice system-efi-bootx2d8f8d0203x2d83f5x2d440cx2d9c4cx2d63c73213f3b1.device loaded active plugged EFI System Partition -.mount loaded inactive dead -.mount systemd-remount-fs.service loaded inactive dead Remount Root and Kernel File Systems systemd-sysctl.service loaded active exited Apply Kernel Variables systemd-udev-trigger.service loaded inactive dead udev Coldplug all Devices sysroot.mount loaded active mounted /sysroot可以看到,这个系统的启动时间为18秒777毫秒,启动时间为2021-07-09 22:31:32。
这种方法可以获取更多的开机信息,但需要查看日志文件,比较麻烦。
3、使用开机时间记录脚本
用户可以在系统启动时使用脚本,将系统启动时间记录到指定文件中。例如:
#!/bin/bashecho "boot_time=`date`" > /var/log/boot_time这个脚本会在系统启动时将当前的日期时间记录到`/var/log/boot_time`文件中。用户只需要在需要查询开机时间时查看该文件即可。
这种方法的优点是方便快捷,并且可以记录更多的启动信息。但需要提前编写脚本并设置开机自启动。
4、使用第三方工具
还有一些第三方工具可以帮助用户查询开机时间,例如`lshw`、`inxi`等系统信息获取工具。
~$ sudo lshw -html > system.html~$ inxi -S System: Host: desktop Kernel: 5.8.0-38-generic x86_64 bits: 64 Desktop: Cinnamon 4.8.6`lshw`可以生成系统的硬件信息报告,并且包括系统启动时间等信息。`inxi`则可以快速获取系统的概要信息,包括启动时间。
这种方法功能强大,但需要安装第三方工具,并且了解工具的使用方法。
总结:
Linux系统提供了多种方式帮助用户查询开机时间,使用的方法取决于使用场景和个人喜好。系统自带命令方便易用,适合简单的查询需求。查看`dmesg`日志文件则可以获取更多的开机信息。使用开机时间记录脚本可以记录更详细的启动信息,但需要编写脚本并设置开机自启动。第三方工具则可以功能更加强大,并且可以获取更多系统信息。
无论使用哪种方法,重要的是能够了解系统的启动情况,并且及时发现问题。
本文皆由ntptimeserver.com作者独自创作为原创,如有侵权请联系我们,转载请注明出处!