ps -aux

  1. 用于查看系统中进程状态。Linux 系统中运行着许多进程,合理的管理进程,可以优化系统性能,在Linux 系统中有常见的5中进程状态,分别为运行、中断、不可中断、僵尸与停止
参数 命令
-a 显示所有进程(包括其他用户进程)
-u 显示用户及其他详细信息
-x 显示没有控制终端的进程
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
进程的所有者 进程ID号 运算器占用率 内存占用率 虚拟内存使用量(单位是KB) 占用的固定内存量(单位是KB) 所在终端 进程
状态
被启动
的时间
实际使用CPU的时间 命令名称与参数
root 1 0.0 0.4 53684 7628 ? Ss 07 :22 0:02 /usr/lib/systemd/systemd
root 2 0.0 0.0 0 0 ? S 07:22 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:22 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 07:22 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 07:22 0:00 [migration/0]

top

top 命令用于动态的监视进程活动与系统负载等信息。

  • PID 进程ID
  • USER 进程所有者的用户名,例如root
  • PR 进程调度优先级
  • NI 进程nice值(优先级),越小的值代表越高的优先级
  • VIRT 进程使用的虚拟内存
  • RES 进程使用的物理内存(不包括共享内存)
  • SHR 进程使用的共享内存
  • CPU 进程使用的CPU占比
  • MEM 进程使用的内存占比
  • TIME 进程启动后到现在所用的全部CPU时间
  • COMMAND 进程的启动命令(默认只显示二进制,top -c 能够显示命令行和启动参数)
    1. h|? #帮助
    2. M #按内存的使用排序
    3. T #按CPU的使用排序
    4. N #以PID的大小排序
    5. R #对排序进行反转
    6. r #修改进程优先级
    7. k #杀死进程
    8. s #多长时间更新一次信息,秒为单位
    9. f #自定义显示字段
    10. 1 #显示所有CPU的负载
    11. < #向前
    12. > #向后
    13. z #彩色
    14. W #保存top环境设置~/.toprc
    15. q #退出
    PR
    The scheduling priority of the task. If you see `rt’ in this
    field, it means the task is running under real time
    scheduling priority.
    NI — Nice Value
    The nice value of the task. A negative nice value means
    higher priority, whereas a positive nice value means lower
    priority. Zero in this field simply means priority will not
    be adjusted in determining a task’s dispatch-ability.

进程优先级的取值范围是通过一个宏定义的,这个宏的名称是MAX_PRIO,它的值为140。
而这个值又是由另外两个值相加组成的,一个是代表nice值取值范围的NICE_WIDTH宏,另一个是代表实时进程(realtime)优先级范围的MAX_RT_PRIO宏。
说白了就是,Linux实际上实现了140个优先级范围,取值范围是从0-139,这个值越小,优先级越高。nice值的-20到19,映射到实际的优先级范围是100-139。

新产生进程的默认优先级被定义为:
#define DEFAULT_PRIO (MAX_RT_PRIO + NICE_WIDTH / 2)
实际上对应的就是nice值的0。
正常情况下,任何一个进程的优先级都是这个值,即使我们通过nice和renice命令调整了进程的优先级,它的取值范围也不会超出100-139的范围,除非这个进程是一个实时进程,那么它的优先级取值才会变成0-99这个范围中的一个。

PR is calculated as follows:
for normal processes: PR = 20 + NI (NI is nice and ranges from -20 to 19)
for real time processes: PR = - 1 - real_time_priority (real_time_priority ranges from 1 to 99)

大多数情况下PR的值可以通过如下公式计算 PR=NI+20 ,我们可以在/etc/security/limits.conf 中配置非root进程允许的最小的niceness值。
理论上内核可以自己改变PR值,但不能改变NI。例如某个进程消耗过多的CPU,则它可能会降低优先级。在这种情况下PR=NI+20 将不正确。所以NI值可以解释为内核提示进程应该具有什么优先级,但内核可以根据情况自行选择实际优先级(PR值)。但通常公式“PR = 20 + NI”是正确的。
https://superuser.com/questions/203657/difference-between-nice-value-and-priority-in-the-top-output

如果我们需要1-99 的优先级,我们需要将程序声明为实时。
Linux 有几种调度策略如下:

  1. [ec2-user@ip-172-16-1-245 ~]$ chrt -m
  2. SCHED_OTHER min/max priority : 0/0
  3. SCHED_FIFO min/max priority : 1/99
  4. SCHED_RR min/max priority : 1/99
  5. SCHED_BATCH min/max priority : 0/0
  6. SCHED_IDLE min/max priority : 0/0
  7. SCHED_DEADLINE min/max priority : 0/0

其中2-3 是实时策略。其他的为普通策略,实时策略总是要优于普通策略。如果需要指定我们程序为实时策略,我们可以按照如下方式:

  1. chrt --rr <priority between 1-99> ./myProgram

Where rt_prior corresponds to the priority between 1 and 99. For that reason the process which will have the higher priority over other processes will be the one called with the number 99.
It is important to note that for real time processes, the nice value is not used.
https://superuser.com/questions/203657/difference-between-nice-value-and-priority-in-the-top-output
SCHED_FIFO
以先进先出的队列方式进行调度,在优先级一样的情况下,谁先执行的就先调度谁,除非它退出或者主动释放CPU。
SCHED_RR
以时间片轮转的方式对相同优先级的多个进程进行处理。时间片长度为100ms。
https://www.cnblogs.com/lcword/p/8267342.html

ifconfig

ifconfig 用于获取网卡配置和网络状态等信息

  1. ifconfig
  2. ifconfig eth0
  3. ifconfig eth0 up/down

uname

用于查看系统内核与系统版本信息。

  1. root@kali:~# uname -a
  2. Linux kali 5.2.0-kali2-amd64 #1 SMP Debian 5.2.9-2kali1 (2019-08-22) x86_64 GNU/Linux

uptime

用于查看系统时间、系统运行时间、平均负载信息、启用终端数量。负载信息显示近1分钟、5分钟、15分钟内服务器的压力情况,负载值越低越好。该数字一般要小于CPU核心数目。

  1. [root@linux Desktop]# uptime
  2. 20:40:44 up 1:58, 2 users, load average: 0.00, 0.01, 0.05

netstatus

netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

  1. root@kali:~# netstat -pantu
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp6 0 0 ::1:5432 :::* LISTEN 620/postgres
  5. udp 0 0 10.10.10.131:68 10.10.10.254:67 ESTABLISHED 542/NetworkManager
  6. udp6 0 0 ::1:60324 ::1:60324 ESTABLISHED 620/postgres

ss

ss命令用于显示socket状态. 他可以显示PACKET sockets, TCP sockets, UDP sockets, DCCP sockets, RAW sockets, Unix domain sockets等等统计. 它比其他工具展示等多tcp和state信息. 它是一个非常实用、快速、有效的跟踪IP连接和sockets的新工具.SS命令可以提供如下信息:

  • 所有的TCP sockets
  • 所有的UDP sockets
  • 所有ssh/ftp/ttp/https持久连接
  • 所有连接到Xserver的本地进程
  • 使用state(例如:connected, synchronized, SYN-RECV, SYN-SENT,TIME-WAIT)、地址、端口过滤
  • 所有的state FIN-WAIT-1 tcpsocket连接以及更多
    1. root@kali:~# ss -pantl
    2. State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
    3. LISTEN 0 244 [::1]:5432 [::]:* users:(("postgres",pid=620,fd=3))

who

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

  1. [root@linux Desktop]# who
  2. linux :0 2018-06-22 17:23 (:0)
  3. linux pts/0 2018-06-22 17:23 (:0)

last

last 命令用于查看系统登陆记录

  1. [root@linux Desktop]# last
  2. linux pts/1 :0 Fri Jun 22 20:37 - 20:37 (00:00)
  3. linux pts/1 :0 Fri Jun 22 20:00 - 20:00 (00:00)
  4. linux pts/1 :0 Fri Jun 22 20:00 - 20:00 (00:00)
  5. linux pts/0 :0 Fri Jun 22 17:23 still logged in
  6. linux :0 :0 Fri Jun 22 17:23 still logged in
  7. (unknown :0 :0 Fri Jun 22 17:23 - 17:23 (00:00)
  8. reboot system boot 3.10.0-123.el7.x Fri Jun 22 17:23 - 20:49 (03:26)
  9. reboot system boot 3.10.0-123.el7.x Fri Jun 22 17:22 - 20:49 (03:27)
  10. (unknown :0 :0 Fri Jun 22 17:20 - 17:20 (00:00)
  11. reboot system boot 3.10.0-123.el7.x Fri Jun 22 17:20 - 17:20 (00:00)
  12. linux pts/0 :0 Fri Jun 22 17:18 - 17:18 (00:00)
  13. linux :0 :0 Fri Jun 22 17:17 - 17:18 (00:00)
  14. (unknown :0 :0 Fri Jun 22 17:16 - 17:17 (00:00)
  15. reboot system boot 3.10.0-123.el7.x Fri Jun 22 17

lastb

用来显示用户登陆失败的记录

  1. [root@ip-172-16-1-245 log]# lastb
  2. user ssh:notty 161.35.212.214 Wed Jan 19 15:32 - 15:32 (00:00)
  3. user ssh:notty 161.35.212.214 Wed Jan 19 15:32 - 15:32 (00:00)
  4. user ssh:notty 161.35.212.214 Wed Jan 19 15:32 - 15:32 (00:00)
  5. user ssh:notty 161.35.212.214 Wed Jan 19 15:32 - 15:32 (00:00)

lastlog

当前系统下用户上次的登陆记录。不记录sudo su这种的登录。

  1. root@test:/var/log# lastlog
  2. Username Port From Latest
  3. root **Never logged in**
  4. daemon **Never logged in**
  5. bin **Never logged in**
  6. sys **Never logged in**
  7. sync **Never logged in**
  8. games **Never logged in**
  9. man **Never logged in**
  10. lp **Never logged in**
  11. mail **Never logged in**
  12. news **Never logged in**
  13. uucp **Never logged in**
  14. proxy **Never logged in**
  15. www-data **Never logged in**
  16. backup **Never logged in**
  17. list **Never logged in**
  18. irc **Never logged in**
  19. gnats **Never logged in**
  20. nobody **Never logged in**
  21. systemd-network **Never logged in**
  22. systemd-resolve **Never logged in**
  23. syslog **Never logged in**
  24. messagebus **Never logged in**
  25. _apt **Never logged in**
  26. lxd **Never logged in**
  27. uuidd **Never logged in**
  28. dnsmasq **Never logged in**
  29. landscape **Never logged in**
  30. sshd **Never logged in**
  31. pollinate **Never logged in**
  32. ubuntu pts/0 114.251.173.134 Tue Oct 12 06:42:22 +0000 2021
  33. cwagent **Never logged in**

history

用于显示历史命令执行记录,默认显示1000条。如果需要自定义,可以编辑/etc/profile文件中修改HISTORY的值。
history -c # 用于清空历史命令记录

  1. [root@linux Desktop]# history
  2. 1 systemctl is-enabled
  3. 2 systemctl is-enabled network-online.target
  4. 3 man man
  5. 4 echo 123
  6. 5 $a=123

dmesg

用于显示开机信息,kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。开机信息亦保存在/var/log目录中,名称为dmesg的文件里。

  1. [root@localhost ~]# dmesg
  2. [ 0.000000] Initializing cgroup subsys cpuset
  3. [ 0.000000] Initializing cgroup subsys cpu
  4. [ 0.000000] Initializing cgroup subsys cpuacct
  5. [ 0.000000] Linux version 3.10.0-862.el7.x86_64 (mockbuild@x86-034.build.eng.bos.redhat.com) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Wed Mar 21 18:14:51 EDT 2018
  6. [ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
  7. [ 0.000000] Disabled fast string operations
  8. [ 0.000000] e820: BIOS-provided physical RAM map:

sosreport

  1. 用于收集系统配置及架构信息并输出诊断文档
  1. yum install sos
  2. sosreport