01-命令了解-一些软硬件信息如何查看

134 阅读6分钟

发行版本与内核版本的查看

可以了解系统版本、内核版本等一系列信息。

或许,在某些软件要求制定版本的OS的时候,这个命令可以帮助你进一步核实自己的环境是否匹配。

cat /etc/centos-release
cat /etc/redhat-release 
hostnamectl
cat /proc/version
uname -r

虚拟机展现的信息如下:

[watcher@localhost ~]$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
[watcher@localhost ~]$ 
[watcher@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[watcher@localhost ~]$ 
[watcher@localhost ~]$ hostnamectl 
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: cb77d1e16bf4417087f1b096ff9bb79c
           Boot ID: 5d3e2dde535b4830ba82df1bf1cd6676
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64
[watcher@localhost ~]$ 
[watcher@localhost ~]$ uname -r
3.10.0-1160.el7.x86_64
[watcher@localhost ~]$ 
[watcher@localhost ~]$ cat /proc/version
Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020
[watcher@localhost ~]$

CPU信息的查看

诸如架构(64位)、CPU数量、核心数、线程数等等信息。

lscpu
cat /proc/cpuinfo
[watcher@localhost ~]$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             AuthenticAMD
CPU family:            25
Model:                 97
Model name:            AMD Ryzen 5 7500F 6-Core Processor
Stepping:              2
CPU MHz:               3699.999
BogoMIPS:              7399.99
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              1024K
L3 cache:              32768K
NUMA node0 CPU(s):     0
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc extd_apicid eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext retpoline_amd ssbd ibrs ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 clzero arat avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq overflow_recov succor flush_l1d

下文的缩进可能有些问题。编写和展示的排版不一致。看着怪难受的。

[watcher@localhost ~]$ cat /proc/cpuinfo 
processor	: 0
vendor_id	: AuthenticAMD
cpu family	: 25
model		: 97
model name	: AMD Ryzen 5 7500F 6-Core Processor
stepping	: 2
cpu MHz		: 3699.999
cache size	: 1024 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 16
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc extd_apicid eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext retpoline_amd ssbd ibrs ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 clzero arat avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq overflow_recov succor flush_l1d
bogomips	: 7399.99
TLB size	: 3584 4K pages
clflush size	: 64
cache_alignment	: 64
address sizes	: 45 bits physical, 48 bits virtual
power management:

image.png

CPU占用信息查看-top

top是一个可以查看系统运行状态的命令,负载啊,CPU占用啊,内存占用啊啥的,都可以通过top展示出来。

下面的例子中,展现了一个处于空闲状态的Linux机器,其监控出来的占用信息。

[watcher@localhost ~]$ top
top - 08:03:07 up  2:00,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 159 total,   1 running, 158 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995672 total,    67540 free,   515920 used,   412212 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   304460 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 51257 watcher   20   0  162100   2300   1572 R  0.3  0.2   0:00.33 top
     1 root      20   0  128236   6940   4200 S  0.0  0.7   0:01.10 systemd
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
     4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.07 ksoftirqd/0
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.22 rcu_sched
    10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain
    11 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 watchdog/0
    13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
    14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
    15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd
    16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
    17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
    18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
    19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
    20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
    21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
    22 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md
    23 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 edac-poller
    24 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 watchdogd
    30 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kswapd0
    31 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd
    32 root      39  19       0      0      0 S  0.0  0.0   0:00.08 khugepaged
    33 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto
    41 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kthrotld
    43 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kmpath_rdacd
    44 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kaluad
    45 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kpsmoused
    47 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ipv6_addrconf
    60 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 deferwq
  • 下列片段中,我重点关注时间。

top - 08:03:07 up 2:00, 1 user, load average: 0.00, 0.01, 0.05

08:03:07为当前Linux时间;

2:00为Linux从开机到现在的统计时间,即2小时。

后面的user和load average,一个指的是当前在线的用户人数,一个是平均负载的数值。这个负载数值越大,代表Linux现在负载越高。

  • 下列片段中,我重点关注英文意思。

Tasks: 159 total, 1 running, 158 sleeping, 0 stopped, 0 zombie

大概意思就是,总共159个进程,1个正在运行,158个休眠,0个停止,0个僵住(僵尸进程)。

  • 下列片段中,我重点关注英文意思。

%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

us:归属于某个用户(user)的进程,占用CPU时间的百分比。这些进程,在用户权限下运行。

sy:归属于系统(system)的进程,占用CPU时间的百分比。这些进程就是系统内核的进程。

ni:被调整过优先级(niced)的进程,所占用CPU时间的百分比。优先级越高,越会优先被CPU关注,进而执行任务。nice值可以调整进程的优先级。

id:空闲(idle)CPU时间的百分比,这段时间意味着CPU没事干。

wa:等待I/O(IO-wait)操作完成,所占用CPU时间的百分比。我简单理解为,等待读写的时间。

hi:处理硬件中断(hardware interrupt)所占用的CPU时间的百分比。

si:处理软件中断(software interrupt)所占用的CPU时间的百分比。

st:在虚拟环境中,被其他虚拟机偷走(stolen)的 CPU 时间百分比。

这里列举下,man指令中有关上述指令的描述:

image.png

           us, user    : time running un-niced user processes
           sy, system  : time running kernel processes
           ni, nice    : time running niced user processes
           id, idle    : time spent in the kernel idle handler
           wa, IO-wait : time waiting for I/O completion
           hi : time spent servicing hardware interrupts
           si : time spent servicing software interrupts
           st : time stolen from this vm by the hypervisor

常见内存信息的查看

free
dmidecode

free命令可以查看Linux可用的内存总量,包括空闲内存、已使用内存、交换(swap)内存等信息。

我通过man命令发现,free命令的数据来源,是/proc/meminfo

image.png

--help来看,最常用的应该就是-h了,用人类易读的格式,展示内存的信息。

[watcher@localhost ~]$ free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        442M        210M         10M        319M        378M
Swap:          2.0G          0B        2.0G

相比较来看,/proc/meminfo的数据就多很多。

[watcher@localhost ~]$ cat /proc/meminfo 
MemTotal:         995672 kB
MemFree:           77248 kB
MemAvailable:     313936 kB
Buffers:              88 kB
Cached:           352420 kB
SwapCached:            8 kB
Active:           427204 kB
Inactive:         186544 kB
Active(anon):     232520 kB
Inactive(anon):    46688 kB
Active(file):     194684 kB
Inactive(file):   139856 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2097148 kB
SwapFree:        2096884 kB
Dirty:                 4 kB
Writeback:             0 kB
AnonPages:        261264 kB
Mapped:           105676 kB
Shmem:             17968 kB
Slab:             117688 kB
SReclaimable:      59692 kB
SUnreclaim:        57996 kB
KernelStack:        6848 kB
PageTables:        20424 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2594984 kB
Committed_AS:    2166768 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      213524 kB
VmallocChunk:   34359277564 kB
Percpu:            55296 kB
HardwareCorrupted:     0 kB
AnonHugePages:     59392 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      100224 kB
DirectMap2M:      948224 kB
DirectMap1G:           0 kB

不过,free可以看的内存数据,是Linux可分配的。跟我想的,VMware中分配的“1GB”,还是有区别。

image.png

free内存和available内存的区别

既然能区分开来,肯定是不一样的。

free   Unused memory (MemFree and SwapFree in /proc/meminfo)
available
              Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or  free  fields,  this  field
              takes  into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, avail‐
              able on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

从解释来看,free内存很简单:没有被使用的内存,也包括交换内存中未被使用的部分。

但是available内存比较复杂。我的理解是,available内存不仅包含free内存,更包含了部分正在使用的内存。这部分内存虽然正在被占用,但是,如果出现了一个新的程序,需要更多的内存,那么在不使用交换内存(swap)的情况下,Linux系统依旧可以将其回收,用作新程序的运行。

从描述来看,page cache内存和部分slabs内存也是被计算在available内存内的。

后续的描述就介绍了,内核版本不同时,available内存的计算不同。

swap内存是啥

我百度了下,感觉跟Windows上面的某个内存概念很类似:虚拟内存

都是内存不够用,从存储硬盘里面再划取部分大小,用作内存的策略。

不过,在消费级PC都可以安装128G内存的今天,我感觉swap没必要开了。放到企业级上面,内存怕不是都可以进入TB级别了。

硬件上的内存信息查看

我不想看Linux可分配的内存。我实际通过VMware分配的内存是1G。

我希望看到这个1GB,不含任何“水分”的。

dmidecode命令就可以解决我的问题,不过没有完全解决。它可以提供的硬件信息太多了,加上虚拟机的DMI似乎没有那么准,我只能用这个命令来看一个大概。

在没有不得不看的信息之前,我不会过于关注dmidecode命令。

[watcher@localhost ~]$ sudo dmidecode -t 17 | more -10
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.7 present.

Handle 0x01A3, DMI type 17, 34 bytes
Memory Device
	Array Handle: 0x01A2
	Error Information Handle: No Error
	Total Width: 32 bits
	Data Width: 32 bits
	Size: 1024 MB
	Form Factor: DIMM
	Set: None
	Locator: RAM slot #0
	Bank Locator: RAM slot #0
	Type: DRAM
	Type Detail: EDO
	Speed: Unknown
	Manufacturer: Not Specified
	Serial Number: Not Specified
	Asset Tag: Not Specified
	Part Number: Not Specified
	Rank: Unknown
	Configured Memory Speed: Unknown

常见存储信息的查看

df
du

不太清楚为什么是df,disk free?

如果服务器出现存储告警(硬盘不够用了),或许我得第一时间找到大文件,然后酌情删掉。

[watcher@localhost ~]$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  471M     0  471M   0% /dev
tmpfs          tmpfs     487M     0  487M   0% /dev/shm
tmpfs          tmpfs     487M  8.3M  478M   2% /run
tmpfs          tmpfs     487M     0  487M   0% /sys/fs/cgroup
/dev/sda3      xfs        18G  4.4G   14G  25% /
/dev/sda1      xfs       297M  152M  145M  52% /boot
tmpfs          tmpfs      98M   12K   98M   1% /run/user/42
tmpfs          tmpfs      98M     0   98M   0% /run/user/1000

在学习命令的过程中,发现一个很相似的命令:du

[watcher@localhost ~]$ sudo du -sh 
5.6M	.

df和du的异同

经过一段时间和豆包AI的battle,大概知道了df和du的异同点。

相同点是:两者都是可以查看存储大小的命令。

不同点是:

  • du侧重于,查看当前目录(或者指定目录)下的文件/目录占据磁盘空间的大小。适用于想要了解某个文件或目录占据多少磁盘空间的场景。
  • df侧重于,查看整个文件系统下,各个设备的使用情况,例如sda设备,使用多少空间,还有多少空间,挂载哪一个目录之类的。适用于想要了解整个文件系统磁盘使用的场景。

文件系统类型的区别

在大概了解文件系统和挂载之后,我开始好奇devtmpfstmpfs是怎样的设备了。

tmpfs是建立在内存里面的,虚拟文件系统类,相关数据是直接保存在内存里的。

devtmpfs是一个专门用来动态更新dev设备文件的一种文件系统类。

这两个都属于底层的文件系统类,对于监控这一类的观测动作来说,似乎没有什么多大的用处。

基于此,如果要监控客户端的硬盘使用情况,是否要针对这些特殊的文件系统类,做例外呢?我搜了下,互联网上,有一个例外的集合:

ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]

快速定位分区大文件的小命令

某个分区快满了,如何快速定位文件或者目录呢?

除了cd / && du -sh *来查看各个目录的大小外,还可以用下列方法。

[watcher@localhost var]$ sudo du -ax /var/ | sort -rn -k1 | head 
385380	/var/
229148	/var/cache
226520	/var/cache/yum/x86_64/7
226520	/var/cache/yum/x86_64
226520	/var/cache/yum
187608	/var/cache/yum/x86_64/7/updates
159508	/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
159508	/var/cache/yum/x86_64/7/updates/gen
151112	/var/lib
134184	/var/lib/rpm

通过这个方法,可以过滤出指定目录下,前10的文件/目录大小。

-a代表着统计所有文件和目录;

-x代表着统计指定文件目录挂载的文件类型下的文件或目录。按照例子来说,如果/var/目录挂载的是设备sda,但是var目录下,有个别子目录挂载的是设备sdb,那么这里的-x就可以剔除这些挂载到其他文件类型(设备)的目录。

sort是用来排序的命令,此处不过多介绍。

常见网络信息的查看

本机名字的查看和修改

hostname
hostnamectl set-hostname xx

一个是快速查看本机名字的命令。

另一个修改系统主机名字的命令。

[watcher@localhost ~]$ hostname
localhost.localdomain
[watcher@localhost ~]$ 
[watcher@localhost ~]$ hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: cb77d1e16bf4417087f1b096ff9bb79c
           Boot ID: 6b6ab7e45dec42758769fb919b4a7f9b
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64
      
[watcher@localhost ~]$ hostnamectl set-hostname functionA
==== AUTHENTICATING FOR org.freedesktop.hostname1.set-static-hostname ===
Authentication is required to set the statically configured local host name, as well as the pretty host name.
Authenticating as: root
Password: 
==== AUTHENTICATION COMPLETE ===
[watcher@localhost ~]$ hostname
functiona

IP地址的查看

ifconfig

这个命令跟Windows的ipconfig类似。

经过简单的了解,发现ifconfig出现的时间更早。它意味着interface configuration,即接口配置。对于Linux,乃至UNIX来说,网络配置层面或许用接口interface来解释更为贴切。

[watcher@localhost ~]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.160.129  netmask 255.255.255.0  broadcast 192.168.160.255
        inet6 fe80::1df1:800b:4509:39af  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:06:16:62  txqueuelen 1000  (Ethernet)
        RX packets 1225  bytes 112889 (110.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 485  bytes 45814 (44.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5568 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5568 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:8c:3d:8c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

路由的查看

route
route -n
ip route

路由表的查看也很直白,route命令即可。

如果多输出一个r,它还能提示相似的命令,略感意外

[watcher@localhost ~]$ router
bash: router: command not found...
Similar command is: 'route'
[watcher@localhost ~]$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.160.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

从路由表中可以看到,默认default接口使用的接口名字叫ens33,后面的UG也说明了,这一条路由,是默认网关路由。

返回上一标题,看接口配置,就可以瞄到接口ens33下的对应网络配置。这个时候,可以判断Linux主机的本机IP(内网IP),为192.168.160.129

网关为0.0.0.0时,代表着对应IP的数据,将不会经过任何中间节点,而是直接转发给对应的IP服务器。

有两个内网IP段的数据,可以不经过网关,直接通过对应的网络接口发出。

默认情况下,都得经过gateway网关。但是这里没有展示IP地址,我决定换一个命令。

[watcher@localhost ~]$ ip route show
default via 192.168.160.2 dev ens33 proto dhcp metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.160.0/24 dev ens33 proto kernel scope link src 192.168.160.129 metric 100

如上,通过ip route show,我可以看到默认状态下的网关为192.168.160.2。即默认下的数据包,都得经由这个网关转发,转发的接口为ens33

后续看route的参数介绍时,发现-n的命令,它也能直接展示纯IP的信息。

[watcher@localhost ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.160.2   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.160.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

机器时间的查看

date

使用date就可以直接查看时间。

[watcher@localhost ~]$ date
Thu Apr 10 00:02:35 CST 2025

这个时间,其实有调整过。一开始展示的并不是+8时区的时间,看着有点难受。

如何校准时间

ntpdate ntp.aliyun.com
chronyd -q 'server ntp.aliyun.com iburst'

两个命令,都是校准时间的命令。两者也有不同之处。

[watcher@localhost ~]$ sudo ntpdate ntp.aliyun.com
10 Apr 00:12:21 ntpdate[3135]: adjust time server 203.107.6.88 offset 0.013410 sec

ntpdate命令,侧重于一次性。同步成功后,直接修改当前的时间,然后任务结束,不会持续跟踪和调整时间。同时,因为直接修改了系统时间,如果当时的Linux本地时间和校准时间相差过大,调整后时间出现跳跃,对于一些需要持续记录的程序,会出现较大影响。

比如日志的记录,前脚还是早上9点,一个校准时间,直接把时间调整到下午18点了。然后在某个日志记录上,会出现前一个9点的记录,后脚就变成了18点的记录。

[watcher@localhost ~]$ sudo chronyd -q 'server ntp.aliyun.com iburst'
2025-04-09T16:10:26Z chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2025-04-09T16:10:26Z Initial frequency 2.961 ppm
2025-04-09T16:10:31Z System clock wrong by 0.002729 seconds (step)
2025-04-09T16:10:31Z chronyd exitin

chronyd命令是chrony时间同步程序的一部分。它可以持续地同步时间,可以在后台持续运行。

在时间调整的手段上,chronyd命令除了可以直接调整(step模式,即步进模式)外,还能逐渐调整(slew模式,即微调模式)。

不过,因为chronyd是有后台的,在用-q同步之前,或许要先使用systemctl stop chronyd来暂时关闭对应的进程。