Linux常用系统相关命令

uptime

一般直接使用
[root@centos ~]# uptime
 13:08:12 up 6 min,  2 users,  load average: 0.04, 0.17, 0.12
[当前时间] [运行时间]	[在线用户]	[平均负载 1分钟,5分钟,15分钟 ps:一般来说1核时大于3认为负载过高,] 

uname

[root@centos ~]# uname -a
Linux centos 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
可以看到操作系统版本,centos 3.1 el7 CPU(x86_64) 

who

who命令可以看到哪些用户通过哪个IP登陆了设备
[root@centos ~]# who -a
           system boot  2019-09-05 22:01
root     + tty1         2019-09-05 22:04  old         1133
           run-level 3  2019-09-05 22:02
           pts/0        2019-09-05 13:09              2897 id=ts/0  term=0 exit=0
root     + pts/1        2019-09-05 22:05  old         2793 (192.168.220.1)

export:查看当前环境变量


[root@centos ~]# export
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="centos"
declare -x LANG="en_US.UTF-8"
declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
......

ps:查看进程

常用ps aux
[root@centos ~]# ps aux | more
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0 190948  3852 ?        Ss   13:01   0:02 /usr/lib/systemd/
systemd --switched-root --system --deserialize 22
root          2  0.0  0.0      0     0 ?        S    13:01   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    13:01   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   13:01   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    13:01   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    13:01   0:00 [rcu_bh]

USER:    用户名
PID:    进程ID(Process ID)
%CPU:    进程的cpu占用率
%MEM:    进程的内存占用率
VSZ:    进程所使用的虚存的大小(Virtual Size)
RSS:    进程使用的驻留集大小或者是实际内存的大小,Kbytes字节。
TTY:    与进程关联的终端(tty)
STAT:    进程的状态:进程状态使用字符表示的(STAT的状态码)
TIME:    进程使用的总cpu时间
COMMAND:    正在执行的命令行命令

使用ps命令按一定序列排序
例如:按VSZ虚拟内存排序
ps aux --sort -[一般是第一行的简写] 去掉“-”为升序 带“-”为降序
[root@centos ~]# ps axu --sort -vsz | more
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       1365  0.3  0.8 694712 32440 ?        Ssl  13:02   0:20 /usr/bin/dockerd-
current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default
-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/
usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init
-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=
journald --signature-verification=false --insecure-registry 192.168.220.132:5000 --
storage-driver overlay2
root       1363  0.0  0.4 555116 18328 ?        Ssl  13:02   0:00 /usr/bin/python -
Es /usr/sbin/tuned -l -P
polkitd    1164  0.0  0.2 524860 10972 ?        Ssl  13:01   0:00 /usr/lib/polkit-1
/polkitd --no-debug

free:显示系统内存使用情况

[root@centos ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        154M        3.2G        8.7M        338M        3.3G
Swap:          3.9G          0B        3.9G

mpstat:显示各个cpu使用

[root@centos ~]# mpstat -P ALL
Linux 3.10.0-327.el7.x86_64 (centos) 	09/05/2019 	_x86_64_	(4 CPU)

02:21:56 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:21:56 PM  all    0.07    0.00    0.21    0.20    0.00    0.00    0.00    0.00    0.00   99.52
02:21:56 PM    0    0.05    0.00    0.22    0.24    0.00    0.00    0.00    0.00    0.00   99.49
02:21:56 PM    1    0.05    0.00    0.18    0.17    0.00    0.00    0.00    0.00    0.00   99.60
02:21:56 PM    2    0.09    0.00    0.21    0.16    0.00    0.00    0.00    0.00    0.00   99.54
02:21:56 PM    3    0.08    0.00    0.24    0.23    0.00    0.00    0.00    0.00    0.00   99.44

top

[root@centos ~]# top
top - 13:43:32 up 41 min,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 367 total,   1 running, 366 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.3 sy,  0.0 ni, 99.3 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3866948 total,  3366436 free,   157064 used,   343448 buff/cache
KiB Swap:  4063228 total,  4063228 free,        0 used.  3487252 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND      
  3335 root      20   0  148328   2144   1364 R   9.5  0.1   0:00.04 top          
     1 root      20   0  190948   3852   2516 S   0.0  0.1   0:02.66 systemd      
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.03 kthreadd     
     3 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/0  
     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
     6 root      rt   0       0      0      0 S   0.0  0.0   0:00.35 migration/0 
     
第一行:类似uptime [当前时间] [运行时间]	[在线用户]	[平均负载 1分钟,5分钟,15分钟]
第二行:进程总数,运行的,睡眠的,停止的,僵尸(zombie)进程。
第三行:cpu状态;
us — 用户空间占用CPU的百分比。
sy — 内核空间占用CPU的百分比。
ni — 改变过优先级的进程占用CPU的百分比
id — 空闲CPU百分比
wa — IO等待占用CPU的百分比
hi — 硬中断(Hardware IRQ)占用CPU的百分比
si — 软中断(Software Interrupts)占用CPU的百分比
第四行与第五行	类似free命令
第四行:内存
total — 物理内存总量
used — 使用中的内存总量
free — 空闲内存总量
buffers — 缓存的内存量
第五行:交换分区
total — 交换区总量
used — 使用的交换区总量
free — 空闲交换区总量
cached — 缓冲的交换区总量

状态说明:
PID:进程ID,进程的唯一标识符
USER:进程所有者的实际用户名。
PR:进程的调度优先级。这个字段的一些值是'rt'。这意味这这些进程运行在实时态。
NI:进程的nice值(优先级)。越小的值意味着越高的优先级。负值表示高优先级,正值表示低优先级
VIRT:进程使用的虚拟内存。进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
RES:驻留内存大小。驻留内存是任务使用的非交换物理内存大小。进程使用的、未被换出的物理内存大小,单位kb。
SHR:SHR是进程使用的共享内存。共享内存大小,单位kb
S:这个是进程的状态。它有以下不同的值:
	D - 不可中断的睡眠态。
	R – 运行态
	S – 睡眠态
	T – 被跟踪或已停止
	Z – 僵尸态
%CPU:自从上一次更新时到现在任务所使用的CPU时间百分比。
%MEM:进程使用的可用物理内存百分比。
TIME+:任务启动后到现在所使用的全部CPU时间,精确到百分之一秒。
COMMAND:运行进程所使用的命令。进程名称(命令名/命令行)

iostat:查看磁盘信息

[root@centos ~]# iostat  -p ALL
Linux 3.10.0-327.el7.x86_64 (centos) 	09/05/2019 	_x86_64_	(4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.06    0.00    0.20    0.18    0.00   99.56

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.01         0.00         44          0
sda               2.14        47.72        26.87     261001     146941
sda1              0.76         4.88         0.38      26695       2081
sda2              1.37        42.69        26.48     233498     144860
dm-0              1.41        42.41        26.48     231938     144860
dm-1              0.02         0.20         0.00       1108          0
kB_read/s:每秒从磁盘读入的数据量
kB_wrtn/s:每秒向磁盘写入的数据量
kB_read:读入数据总量
kB_wrtn:写入数据总量			

iotop:查看进程使用磁盘的读写

常用参数:
-o 只显示在读写硬盘的进程
-d 设定时间间隔
  
[root@centos ~]# iotop
Total DISK READ :	0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND            
     1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --~erialize 22
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
     3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
     7 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
     9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcuob/0]
    10 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcuob/1]

netstat:查看网络相关

常用组合netsta -antup
[root@centos ~]# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3313/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2495/master         
tcp        0     52 192.168.220.132:22      192.168.220.1:53919     ESTABLISHED 4308/sshd: root@pts 
tcp        0      0 192.168.220.132:22      192.168.220.1:52868     ESTABLISHED 2791/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      3313/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2495/master         
udp        0      0 0.0.0.0:63509           0.0.0.0:*                           3270/dhclient       
udp        0      0 0.0.0.0:68              0.0.0.0:*                           3270/dhclient       
udp6       0      0 :::36432                :::*                                3270/dhclient 

nload:监控总体带宽

[root@centos ~]# nload

Device eno16777736 [192.168.220.132] (2/3):
===================================================================================
Incoming:

                                                         Curr: 944.00 Bit/s
                                                         Avg: 1.78 kBit/s
                                                         Min: 944.00 Bit/s
                                                         Max: 4.09 kBit/s
                                                         Ttl: 283.16 MByte
Outgoing:

                                                         Curr: 8.06 kBit/s
                                                         Avg: 8.13 kBit/s
                                                         Min: 4.27 kBit/s
                                                         Max: 9.21 kBit/s
                                                         Ttl: 3.95 MByte

nethogs:监控进程所使用的流量

[root@centos ~]# nethogs
Ethernet link detected Waiting for first packet to arrive (see sourceforge.net bug 1019381)

NetHogs version 0.8.5

    PID USER     PROGRAM                       DEV        SENT      RECEIVED       
   4308 root     sshd: root@pts/0              eno167	   0.165       0.088 KB/sec
   4508 root     wget                          eno167	   0.000       0.000 KB/sec
      ? root     unknown TCP                               0.000       0.000 KB/sec

  TOTAL                                                    0.165       0.088 KB/sec
 

vmstat:查看系统整体运行状态

vmstat :命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括 服务器的CPU使用率,MEM内存使用,VMSwap虚拟内存交换情况,IO读写情况。
使用vmstat可以看到整个机器的CPU,内存,IO的使用情况,而不是单单看到各个进程的CPU使 用率和内存使用率。 比top命令节省资源

[root@centos ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 3230876    948 469696    0    0     7     5   23   35  0  0 100  0  0
 
每一列参数作用: 
r 运行状态的进程个数 。展示了正在执行和等待cpu资源的任务个数。
b 不可中断睡眠 正在进行i/o等待--阻塞状态的进程个数 进程读取外设上的数据,等待时
free 剩余内存,单位是KB 
buffers #内存从磁盘读出的内容
cached #内存需要写入磁盘的内容
si swapin swap换入到内存 
so swapout 内存换出到swap 换出的越多,内存越不够用
bi blockin 从硬盘往内存读。 单位是块。 把磁盘中的数据读入内存 
bo blockout 从内存拿出到硬盘 (周期性的有值) 写到硬盘 #判断是读多还是写多,是否有i/o瓶颈
in 系统的中断次数,cpu调度的次数多 
cs 每秒的上下文切换速度 CPU上下文切换--程序在运行的时候,
CPU对每个程序切换的过程。 

sar:记录系统一段时间的运行状态

sysstat 工具包可以把检查到的信息保存下来! 存在/var/log/sa目录下,文件名为sa当 前日期 sar 默认显示每10分钟统计一次状态信息 (从装sysstat 包开始) 
sar 命令行的常用格式: sar [options] [-A] [-o file] t [n] 在命令行中,n 和t 两个参数组合起来定义采样间隔和次数,t为采样间隔,是必须有的参数,n为采样 次数,是可选的,默认值是1,-o file表示将命令结果以二进制格式存放在文件中,file 在此处不是关键 字,是文件名。options 为命令行选项,sar命令的选项很多,下面只列出常用选项:  
-A:所有报告的总和。 
-n:网络接口的情况。 
-u:CPU利用率 
-v:进程、I节点、文件和锁表状态。 
-d:硬盘使用报告。 
-r:没有使用的内存页面和硬盘块。 
-g:串口I/O的情况。 
-b:缓冲区使用情况。 
-a:文件读写情况。 
-c:系统调用情况。 
-R:进程的活动情况。 
-y:终端设备活动情况。 
-w:系统交换活动。 
-o 文件名:打印到屏幕并将采样结果以二进制形式存入当前目录下的文件中。 
-f 文件名:查看之前保存的二进制文件。


2秒钟采集一次,采集5次到sar.tar文件
[root@centos ~]# sar -u 2 5 -o sar.tar
Linux 3.10.0-327.el7.x86_64 (centos) 	09/05/2019 	_x86_64_	(4 CPU)

04:39:03 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:39:05 PM     all      0.00      0.00      0.12      0.12      0.00     99.75
04:39:07 PM     all      0.12      0.00      0.12      0.00      0.00     99.75
04:39:09 PM     all      0.00      0.00      0.12      0.00      0.00     99.88
04:39:11 PM     all      0.00      0.00      0.13      0.00      0.00     99.87
04:39:13 PM     all      0.00      0.00      0.25      0.00      0.00     99.75
Average:        all      0.03      0.00      0.15      0.03      0.00     99.80

从文件中读取信息
sar -f 【文件】
[root@centos ~]# sar -f sar.tar 
Linux 3.10.0-327.el7.x86_64 (centos) 	09/05/2019 	_x86_64_	(4 CPU)

04:39:03 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:39:05 PM     all      0.00      0.00      0.12      0.12      0.00     99.75
04:39:07 PM     all      0.12      0.00      0.12      0.00      0.00     99.75
04:39:09 PM     all      0.00      0.00      0.12      0.00      0.00     99.88
04:39:11 PM     all      0.00      0.00      0.13      0.00      0.00     99.87
04:39:13 PM     all      0.00      0.00      0.25      0.00      0.00     99.75
Average:        all      0.03      0.00      0.15      0.03      0.00     99.80


发布了65 篇原创文章 · 获赞 48 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/DoloresOOO/article/details/100561472