linux系统学习笔记(二):系统状态检测命令-百越白狼社区-弘客联盟新手区-弘客联盟

linux系统学习笔记(二):系统状态检测命令

ifconfig

用来获取网卡配置和网络状态信息。

实际演示:

[root@linuxprobe ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:91:4e:47  txqueuelen 1000  (Ethernet)
        RX packets 399  bytes 65365 (63.8 KiB)
        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

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 36  bytes 3204 (3.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 36  bytes 3204 (3.1 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:b3:1c:01  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

我们主要查看的信息有:

  • 网卡名称:ens160
  • inet后面的ip地址
  • ether后面的网卡物理地址(mac地址)

uname

用于查看系统内核版本和系统架构等信息

在使用uname命令时,一般要固定搭配上-a参数来完整地查看当前系统的内核名称、主机名、内核发行版本、节点名、压制时间、硬件名称、硬件平台、处理器类型以及操作系统名称等信息:

[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 4.18.0-80.el8.x86_64 #1 SMP Wed Mar 13 12:02:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

如果要查看当前系统版本的详细信息,则需要查看redhat-release文件,其命令以及相应的结果如下:

[root@linuxprobe ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.0 (Ootpa)

uptime

该命令用来显示系统负载信息

实际演示:

[root@linuxprobe ~]# uptime
 14:07:28 up  8:48,  1 user,  load average: 0.00, 0.00, 0.00

这些信息依次为:当前系统时间、系统已运行时间、启用终端数量、平均负载值。平均负载值指的是系统最近1分钟、5分钟、15分钟内的压力情况,负载值越低越好。

free

该命令用于显示当前系统内存使用量

通常使用free -h 来输出信息:

[root@linuxprobe ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       1.3Gi       104Mi        30Mi       570Mi       475Mi
Swap:         2.0Gi          0B       2.0Gi

如果不用-h,默认是以kb为单位,看起来数字很大影响阅读。

这些字段的解释为:

  • total:内存总量
  • used:已用量
  • free:空闲量
  • shared:共享使用内存
  • buff/cache:磁盘缓存内存 / 缓存的内存量
  • available:可用量

who

用于查看当前登录主机的用户终端信息

[root@linuxprobe ~]# who
root     tty2         2023-09-14 11:35 (tty2)

这里只有一行数据,如果有人远程登录也会显示来访ip。

这里的三条数据代表着:登录用户、终端设备、登录到系统的时间

last

用于调取主机的被访记录

Linux系统会将每次的登录信息都记录到日志文件中,如果哪天想翻阅了,直接执行这条命令就行:

[root@linuxprobe ~]# last
root     tty2         tty2             Thu Sep 14 11:35    gone - no logout
reboot   system boot  4.18.0-80.el8.x8 Thu Sep 14 11:33   still running
root     tty2         tty2             Thu Sep 14 11:32 - 11:33  (00:00)
reboot   system boot  4.18.0-80.el8.x8 Thu Sep 14 11:31 - 11:33  (00:01)
root     tty2         tty2             Tue Sep 12 09:28 - crash (2+02:03)
reboot   system boot  4.18.0-80.el8.x8 Tue Sep 12 09:26 - 11:33 (2+02:06)
root     tty2         tty2             Wed Aug 30 16:29 - crash (12+16:57)
reboot   system boot  4.18.0-80.el8.x8 Wed Aug 30 16:27 - 11:33 (14+19:05)
root     tty2         tty2             Mon Aug  7 09:29 - crash (23+06:58)
reboot   system boot  4.18.0-80.el8.x8 Mon Aug  7 09:28 - 11:33 (38+02:05)
reboot   system boot  4.18.0-80.el8.x8 Mon Aug  7 09:19 - 09:28  (00:08)
root     tty2         tty2             Sat Aug  5 09:55 - crash (1+23:23)
reboot   system boot  4.18.0-80.el8.x8 Sat Aug  5 09:54 - 09:28 (1+23:33)
root     tty2         tty2             Thu Aug  3 23:02 - crash (1+10:52)
reboot   system boot  4.18.0-80.el8.x8 Thu Aug  3 23:01 - 09:28 (3+10:26)

wtmp begins Thu Aug  3 23:01:03 2023

ping

用于测试主机之间的连通性

这个命令只要学过计算机的即便没接触过Linux也应该多少会了解一些。

它的参数有:

  • -c:总共发送次数(如果不指定会一直Ping)
  • -l:指定网卡名称
  • -i:每次间隔时间(秒)
  • -W:最长等待时间(秒)

我的linux已经联网,如果你的linux还没有联网,应该查阅相关的网卡配置教程。

ping一个存在的网站:

[root@linuxprobe ~]# ping -c 4 www.baidu.com
PING www.baidu.com (14.119.104.189) 56(84) bytes of data.
64 bytes from www.baidu.com (14.119.104.189): icmp_seq=1 ttl=55 time=8.06 ms
64 bytes from www.baidu.com (14.119.104.189): icmp_seq=2 ttl=55 time=11.1 ms
64 bytes from www.baidu.com (14.119.104.189): icmp_seq=3 ttl=55 time=14.7 ms
64 bytes from www.baidu.com (14.119.104.189): icmp_seq=4 ttl=55 time=13.8 ms

--- www.baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 11ms
rtt min/avg/max/mdev = 8.059/11.907/14.654/2.590 ms

ping一个不存在的ip:

[root@linuxprobe ~]# ping -c 4 111.22.33.44
PING 111.22.33.44 (111.22.33.44) 56(84) bytes of data.

--- 111.22.33.44 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 62ms

tracepath

用于显示数据包到达目的主机时途中经过的所有路由信息

当两台主机无法互相ping通时,要考虑两台主机之间是否有错误的路由信息,导致数据被某一台设备错误的丢弃。

实际演示:

[root@linuxprobe ~]# tracepath www.baidu.com
 1?: [LOCALHOST]                      pmtu 1500
 1:  _gateway                                              9.344ms 
 1:  _gateway                                             16.865ms 
 2:  192.168.1.1                                           2.707ms 
 3:  192.168.1.1                                           4.054ms pmtu 1492
 3:  100.64.0.1                                            8.770ms 
 4:  202.105.157.221                                       8.850ms 
 5:  119.145.47.73                                         7.546ms 
 6:  no reply
 7:  86.96.135.219.broad.fs.gd.dynamic.163data.com.cn     16.409ms 
 8:  121.14.67.182                                        16.055ms 
 9:  no reply
10:  no reply
11:  no reply
12:  no reply
13:  no reply
14:  no reply
15:  no reply

netstat

用于显示如网络连接、路由表、接口状态等的网络相关信息

只要使用得当,netstat可以查看网络的方方面面。

一些常用参数:

  • -a:显示所有连接中的socket
  • -p:显示正在使用的socket信息
  • -t:显示tcp协议的连接状态
  • -u:显示UDP协议的连接状态
  • -n:使用IP地址,不使用域名
  • -l:仅列出正在监听的服务状态
  • -i:显示网卡列表信息
  • -r:显示路由表信息

例如:显示所有连接中的soket:

[root@linuxprobe ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 linuxprobe.com:domain   0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN     
udp        0      0 linuxprobe.com:domain   0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 linuxprobe.com:bootpc   0.0.0.0:*                          
udp        0      0 0.0.0.0:46153           0.0.0.0:*                          
udp        0      0 0.0.0.0:sunrpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp6       0      0 [::]:52245              [::]:*                             
udp6       0      0 [::]:sunrpc             [::]:*                             
udp6       0      0 [::]:mdns               [::]:*                             
raw6       0      0 [::]:ipv6-icmp          [::]:*

显示网卡列表:

[root@linuxprobe ~]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens160           1500     6043      0      0 0          2042      0      0      0 BMRU
lo              65536       48      0      0 0            48      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU

history

该命令用于显示执行过的命令

  • 执行history命令能显示出当前用户在本地计算机中执行过的最近1000条命令记录
  • 可以使用-c参数清空所有的命令历史记录
  • 可以使用“!编码数字”的方式来重复执行某一次的命令

    如果觉得1000不够用,可以自定义/etc/profile文件中的HISTSIZE变量值。

实际演示、直接使用:

[root@linuxprobe ~]# history 
    1  ll
    2  poweroff 
    3  ps
    4  ps -a
    5  ps -u
    6  pstree
    7  top
    8  echo hello world
    9  echo $SHELL
   10  date
   11  date "+%Y-%m-%d %H:%M:%S"
   12  date -s "20000101 00:00:00"
   13  timedatectl status
   14  timedatectl set-timezone Asia/Shanghai
   15  timedatectl set-time 2023-09-14
   16  timedatectl set-time 16:54
   17  date "+%Y-%m-%d %H:%M:%S"
   18  ps -aux
   19  pstree
   20  top
   21  pidof sshd
   22  kill 1070
   23  kill -9 1070
   24  pidof sshd
   25  pidof httpd
   26  ifconfig
   27  uname -a
   28  cat /etc/redhat-access-insights/
   29  cat /etc/redhat-release 
   30  uptime
   31  free -h
   32  wog
   33  who
   34  last
   35  shutdown 
   36  poweroff 
   37  ll
   38  ping www.baidu.com
   39  ifconfig
   40  cd /etc/sysconfig/
   41  ll
   42  cd network-scripts/
   43  ll
   44  vi ifcfg-ens160 
   45  serivce networt restart
   46  service networt restart
   47  service network restart
   48  reboot 
   49  ping -c 4 www.baidu.com
   50  ping 111.22.33.44
   51  ping -c 4 -W 1 111.22.33.44
   52  ping -c 4 111.22.33.44
   53  tracepath
   54  tracepath www.baidu.com
   55  netstat 
   56  netstat -a
   57  netstat -i
   58  history

历史命令会被保存到用户家目录中的.bash_history文件中。Linux系统中以点(.)开头的文件均代表隐藏文件,这些文件大多数为系统服务文件,可以用cat命令查看其文件内容:

[root@linuxprobe ~]# cat ~/.bash_history

要清空当前用户在本机上执行的Linux命令历史记录信息,可执行如下命令:

[root@linuxprobe ~]# history -c

sosreport

用于收集系统配置及架构信息并输出诊断文档

能够简单的收集系统运行状态以及服务配置信息:

[root@linuxprobe ~]# sosreport

sosreport (version 3.6)

This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp/sos.z5qj8ggc and may be provided to a Red Hat support
representative.

Any information provided to Red Hat will be treated in accordance with
the published support policies at:

  https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press ENTER to continue, or CTRL-C to quit.

Please enter the case id that you are generating this report for []: 

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Starting 2/95  anaconda        [Running: anaconda]                              Starting 1/95  alternatives    [Running: anaconda alternatives]                 Starting 3/95  anacron         [Running: anaconda alternatives anacron]         Starting 4/95  ata             [Running: anaconda alternatives anacron ata]     Starting 5/95  auditd          [Running: anaconda alternatives ata auditd]      Starting 6/95  block           [Running: anaconda alternatives auditd block]    Starting 7/95  boot            [Running: anaconda alternatives block boot]      Starting 8/95  buildah         [Running: alternatives block boot buildah]       Starting 9/95  ceph            [Running: block boot buildah ceph]               Starting 10/95 cgroups         [Running: block boot buildah cgroups]            Starting 11/95 chrony          [Running: block boot cgroups chrony]             Starting 12/95 cron            [Running: block boot cgroups cron]               Starting 13/95 crypto          [Running: block boot cgroups crypto]             Starting 14/95 cups            [Running: boot cgroups crypto cups]              Starting 15/95 date            [Running: boot cgroups cups date]                Starting 16/95 dbus            [Running: boot cgroups date dbus]                Starting 17/95 devicemapper    [Running: boot cgroups date devicemapper]        Starting 18/95 devices         [Running: boot cgroups date devices]             Starting 19/95 dnf             [Running: boot cgroups devices dnf]              Starting 20/95 dracut          [Running: boot cgroups dnf dracut]               Starting 21/95 filesys         [Running: boot dnf dracut filesys]               Starting 22/95 firewalld       [Running: boot dnf dracut firewalld]             Starting 23/95 gdm             [Running: dnf dracut firewalld gdm]              Starting 24/95 gluster         [Running: dnf dracut firewalld gluster]          Starting 25/95 grub2           [Running: dnf dracut gluster grub2]              Starting 26/95 gssproxy        [Running: dracut gluster grub2 gssproxy]         Starting 27/95 hardware        [Running: dracut gluster grub2 hardware]         Starting 28/95 host            [Running: gluster grub2 hardware host]           Starting 29/95 hts             [Running: gluster grub2 host hts]                Starting 30/95 i18n            [Running: gluster grub2 host i18n]               Starting 31/95 iscsi           [Running: gluster grub2 host iscsi]              Starting 32/95 jars            [Running: gluster grub2 host jars]               Starting 33/95 java            [Running: gluster grub2 host java]               Starting 34/95 kdump           [Running: gluster grub2 host kdump]              Starting 35/95 kernel          [Running: gluster grub2 host kernel]             Starting 36/95 keyutils        [Running: gluster grub2 kernel keyutils]         Starting 37/95 kpatch          [Running: gluster grub2 kernel kpatch]           Starting 38/95 krb5            [Running: gluster grub2 kernel krb5]             Starting 39/95 last            [Running: gluster grub2 kernel last]             Starting 40/95 ldap            [Running: gluster grub2 kernel ldap]             Starting 41/95 libraries       [Running: gluster grub2 kernel libraries]        Starting 42/95 libvirt         [Running: gluster grub2 kernel libvirt]          Starting 43/95 logrotate       [Running: gluster grub2 kernel logrotate]        Starting 44/95 logs            [Running: gluster grub2 kernel logs]             Starting 45/95 lvm2            [Running: gluster grub2 kernel lvm2]             Starting 46/95 md              [Running: gluster grub2 kernel md]               Starting 47/95 megacli         [Running: gluster grub2 kernel megacli]          Starting 48/95 memory          [Running: gluster grub2 kernel memory]           Starting 49/95 mrggrid         [Running: gluster grub2 kernel mrggrid]          Starting 50/95 mrgmessg        [Running: gluster grub2 kernel mrgmessg]         Starting 51/95 multipath       [Running: gluster grub2 kernel multipath]        Starting 52/95 networking      [Running: gluster grub2 kernel networking]       Starting 53/95 networkmanager  [Running: gluster grub2 networking networkmanag  Starting 54/95 nfs             [Running: gluster networking networkmanager nfs  Starting 55/95 nis             [Running: gluster networking networkmanager nis  Starting 56/95 nss             [Running: gluster networking networkmanager nss  Starting 57/95 ntb             [Running: gluster networking networkmanager ntb  Starting 58/95 numa            [Running: gluster networking networkmanager num  Starting 59/95 opengl          [Running: gluster networking networkmanager ope  Starting 60/95 openhpi         [Running: gluster networking networkmanager ope  Starting 61/95 openshift       [Running: gluster networking networkmanager ope  Starting 62/95 openssl         [Running: gluster networking networkmanager ope  Starting 63/95 pam             [Running: gluster networking networkmanager pam  Starting 64/95 pci             [Running: gluster networking pam pci]            Starting 65/95 perl            [Running: gluster networking pci perl]           Starting 66/95 podman          [Running: gluster networking pci podman]         Starting 67/95 process         [Running: gluster pci podman process]            Starting 68/95 processor       [Running: gluster podman process processor]      Starting 69/95 psacct          [Running: gluster podman processor psacct]       Starting 70/95 release         [Running: gluster podman processor release]      Starting 71/95 rpm             [Running: gluster podman processor rpm]          Starting 72/95 runc            [Running: gluster processor rpm runc]            Starting 73/95 samba           [Running: gluster processor rpm samba]           Starting 74/95 scsi            [Running: processor rpm samba scsi]              Starting 75/95 selinux         [Running: processor rpm scsi selinux]            Starting 76/95 services        [Running: processor scsi selinux services]       Starting 77/95 soundcard       [Running: processor scsi selinux soundcard]      Starting 78/95 ssh             [Running: processor scsi selinux ssh]            Starting 79/95 sssd            [Running: processor scsi selinux sssd]           Starting 80/95 subscription_manager [Running: processor scsi selinux subscript  Starting 81/95 sunrpc          [Running: processor selinux subscription_manage  Starting 82/95 system          [Running: processor selinux subscription_manage  Starting 83/95 systemd         [Running: processor selinux subscription_manage  Starting 84/95 sysvipc         [Running: processor selinux systemd sysvipc]     Starting 85/95 teamd           [Running: processor selinux systemd teamd]       Starting 86/95 tuned           [Running: processor selinux systemd tuned]       Starting 87/95 udev            [Running: processor selinux systemd udev]        Starting 88/95 unpackaged      [Running: processor selinux systemd unpackaged]  Starting 89/95 usb             [Running: processor selinux systemd usb]         Starting 90/95 vdo             [Running: processor selinux systemd vdo]         Starting 91/95 vhostmd         [Running: processor selinux vdo vhostmd]         Starting 92/95 x11             [Running: processor selinux vdo x11]             Starting 93/95 xen             [Running: processor selinux x11 xen]             Starting 94/95 xfs             [Running: processor selinux x11 xfs]             Finishing plugins              [Running: processor selinux xfs]                 Starting 95/95 yum             [Running: processor selinux xfs yum]             Finishing plugins              [Running: processor selinux yum]                 Finishing plugins              [Running: processor selinux]                     Finishing plugins              [Running: selinux]                                       
  Finished running plugins                                                               
Creating compressed archive...

Your sosreport has been generated and saved in:
  /var/tmp/sosreport-linuxprobe-2023-09-18-nyhoksn.tar.xz

The checksum is: 785dd5452887d24e8f458f005f347f20

Please send this file to your support representative.

会输出很多信息,结尾处的压缩文件路径保存了此次诊断的数据,将他发给技术人员可以让技术远程诊断一些小问题。

d2b5ca33bd153035

请登录后发表评论

    没有回复内容

随便看看