ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
常用Linux系统资源查看命令 [[参考文档]](https://segmentfault.com/a/1190000014789482?utm_source=index-hottest) [TOC] ## 查看系统版本 方法1: ~~~ [root@linuxidc ~]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.2.1511 (Core) Release: 7.2.1511 Codename: Core ~~~ 方法2: ~~~ [root@linuxidc ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel Fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" RedHat_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7" ~~~ ## `vmstat` 命令(监控系统资源) 参数: `-a`:使用 inactive/active(活跃与否)替代 buffer/cache 的内存输出信息; `-f`:开机到目前为止系统复制(fork)的进程数; `-s`:将一些事件(开机至目前为止) 导致的内存变化情况列表说明; `-S`:后面可以接单位,让显示的数据有单位.例如K/M 取代 bytes 的容量; `-d`:列出磁盘的读写总量统计表; `-p`:后面列出分区,可显示该分区的读写总量统计表。 ~~~bash [vagrant~] ]$vmstat 1 3 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 259312 28320 108272 0 0 30 4 41 202 0 0 99 0 0 0 0 0 259304 28320 108272 0 0 0 0 50 93 0 1 99 0 0 0 0 0 259300 28320 108272 0 0 0 0 33 73 0 0 100 0 0 ~~~ ## `dmesg` 命令(开机时内核检查信息) 1. 输出所有内核开机时的信息 **由于输出数据较多,可用 less 命令查看:`dmesg | less`** ~~~bash [vagrant~] ]$dmesg | less [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 3.13.0-24-generic (buildd@panlong) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 (Ubuntu 3.13.0-24.46-generic 3.13.9) [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.13.0-24-generic root=UUID=64ecdf77-db7b-48a8-9066-abfb837f2e24 ro ...省略n行... [ 0.000000] Zone ranges: [ 0.000000] DMA [mem 0x00001000-0x00ffffff] [ 0.000000] DMA32 [mem 0x01000000-0xffffffff] [ 0.000000] Normal empty [ 0.000000] Movable zone start for each node : ~~~ 2. 查找包含指定内容的信息 **查找开机时CPU的相关信息:`dmesg | grep CPU`** ~~~ [vagrant~] ]$dmesg | grep CPU [ 0.000000] CPU MTRRs all blank - virtualized system. [ 0.000000] ACPI: SSDT 000000001fff02a0 0001CC (v01 VBOX VBOXCPUT 00000002 INTL 20100528) [ 0.000000] smpboot: Allowing 1 CPUs, 0 hotplug CPUs [ 0.000000] setup_percpu: NR_CPUS:256 nr_cpumask_bits:256 nr_cpu_ids:1 nr_node_ids:1 [ 0.000000] PERCPU: Embedded 29 pages/cpu @ffff88001fc00000 s86336 r8192 d24256 u2097152 [ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1 [ 0.000000] RCU restricting CPUs from NR_CPUS=256 to nr_cpu_ids=1. [ 0.000000] Offload RCU callbacks from all CPUs [ 0.000000] Offload RCU callbacks from CPUs: 0. [ 0.004582] mce: CPU supports 0 MCE banks [ 0.078327] smpboot: CPU0: AMD A10-7870K Radeon R7, 12 Compute Cores 4C+8G (fam: 15, model: 38, stepping: 01) [ 0.083763] x86: Booted up 1 node, 1 CPUs [ 0.844045] microcode: CPU0: patch_level=0x06000626 [ 0.908285] ledtrig-cpu: registered to indicate activity on CPUs [ 3.624181] CPU: 0 PID: 293 Comm: systemd-udevd Tainted: GF O 3.13.0-24-generic #46-Ubuntu ~~~ **查找开机时硬盘的相关信息:`dmesg | grep -i hd`** ~~~ [vagrant~] ]$dmesg | grep -i hd [ 0.084000] NMI watchdog: disabled (cpu0): hardware events not enabled ~~~ **查找开机时网卡的相关信息:`dmesg | grep -i eth`** ~~~ [vagrant~] ]$dmesg | grep -i eth ~~~ ## `free` 命令(查看内存使用状态) ~~~ [vagrant~] ]$free total used free shared buffers cached Mem: 501832 248436 253396 2372 33216 109116 -/+ buffers/cache: 106104 395728 Swap: 522236 0 522236 [vagrant~] ]$free -b total used free shared buffers cached Mem: 513875968 254517248 259358720 2428928 34029568 111755264 -/+ buffers/cache: 108732416 405143552 Swap: 534769664 0 534769664 [vagrant~] ]$free -k total used free shared buffers cached Mem: 501832 248576 253256 2372 33232 109136 -/+ buffers/cache: 106208 395624 Swap: 522236 0 522236 [vagrant~] ]$free -m total used free shared buffers cached Mem: 490 242 247 2 32 106 -/+ buffers/cache: 103 386 Swap: 509 0 509 [vagrant~] ]$free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 0 0 0 ~~~ ## 查看CPU信息 ~~~ [vagrant~] ]$cat /proc/cpuinfo processor : 0 vendor_id : AuthenticAMD cpu family : 21 model : 56 model name : AMD A10-7870K Radeon R7, 12 Compute Cores 4C+8G stepping : 1 microcode : 0x6000626 cpu MHz : 3892.536 cache size : 2048 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 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 rdtscp lm rep_good nopl extd_apicid pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 popcnt aes xsave avx lahf_lm cr8_legacy abm sse4a misalignsse 3dnowprefetch arat fsgsbase bogomips : 7785.07 TLB size : 1536 4K pages clflush size : 64 cache_alignment : 64 address sizes : 48 bits physical, 48 bits virtual power management: ~~~ ## `uptime` 命令(显示系统的启动时间和平均负载) 输出结果与`top`命令和`w`的第一行相同。 ~~~ [vagrant~] ]$uptime 02:35:11 up 2:43, 1 user, load average: 0.00, 0.06, 0.07 [vagrant~] ]$w 02:35:13 up 2:43, 1 user, load average: 0.00, 0.06, 0.07 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT vagrant pts/0 10.0.2.2 23:52 1.00s 0.49s 0.00s w [vagrant~] ]$top top - 02:36:52 up 2:45, 1 user, load average: 0.00, 0.04, 0.06 Tasks: 80 total, 1 running, 79 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.1 us, 0.3 sy, 0.0 ni, 99.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 501832 total, 251856 used, 249976 free, 35560 buffers KiB Swap: 522236 total, 0 used, 522236 free. 109596 cached Mem ...省略n行... ~~~ ## `uname`查看系统与内核相关信息 ### `uname` 命令 ~~~ [root@tatahy-lamp ~]# uname [-asrmpi] ~~~ 参数: `-a`:所有系统相关的信息,包括下面的数据都会被列出来; `-s`:系统内核名称; `-r`:内核的版本; `-m`:本系统的硬件名称,例如1686或x86_64等; `-p`:CPU的类型,与`-m`类似,只是显示的是 CPU 的类型; `-i`:硬件的平台(ix86)。 示例: ~~~ [root@tatahy-lamp ~]# uname -a Linux tatahy-lamp 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux ~~~ 主机内核:Linux 主机名:tata-lamp 内核版本:3.10.0-123.9.3.el7.x86_64 内核版本创建时间:Thu Nov 6 15:06:03 UTC 2014 本系统的硬件名称:x86_64 CPU的类型:x86_64 适用的硬件平台:x86_64 ### 1 判断当前系统的位数 ~~~ [vagrant~] ]$file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=64d095bc6589dd4bfbf1c6d62ae985385965461b, stripped ~~~ ### 2 查看当前Linux系统的发行版本 ~~~ [vagrant~] ]$lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04 LTS Release: 14.04 Codename: trusty ~~~ ## `lsof` 命令(list open file 列出进程打开或使用的文件信息) ### 1 查询系统中所有进程调用的文件 ~~~ [vagrant~] ]$sudo lsof | less COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 8,1 4096 2 / init 1 root rtd DIR 8,1 4096 2 / init 1 root txt REG 8,1 265848 1835051 /sbin/init init 1 root mem REG 8,1 47712 1053013 /lib/x86_64-linux-gnu/libnss_files-2.19.so init 1 root mem REG 8,1 47760 1053033 ...省略n行... khelper 13 root cwd DIR 8,1 4096 2 / khelper 13 root rtd DIR 8,1 4096 2 / khelper 13 root txt unknown /proc/13/exe : ~~~ ### 2 查询某个文件被哪个进程调用 ~~~ [vagrant~] ]$sudo lsof /sbin/init COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root txt REG 8,1 265848 1835051 /sbin/init ~~~ ### 3 查询某个进程调用了哪些文件 ~~~ [vagrant~] ]$sudo lsof -c nginx COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1000 root cwd DIR 8,1 4096 2 / nginx 1000 root rtd DIR 8,1 4096 2 / nginx 1000 root txt REG 8,1 873176 1573477 /usr/sbin/nginx nginx 1000 root mem REG 8,1 47712 1053013 ...省略n行... nginx 1004 www-data 14u unix 0xffff88001b776000 0t0 9508 socket nginx 1004 www-data 15u 0000 0,9 0 5249 anon_inode ~~~ ### 4 查询某个用户的进程调用了哪些文件 ~~~ [vagrant~] ]$sudo lsof -u vagrant COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1516 vagrant cwd DIR 8,1 4096 2 / sshd 1516 vagrant rtd DIR 8,1 4096 2 / sshd 1516 vagrant txt REG 8,1 766784 1583210 /usr/sbin/sshd sshd 1516 vagrant DEL REG 0,4 11386 /dev/zero sshd 1516 vagrant mem REG 8,1 14464 1048876 /lib/x86_64-linux-gnu/security/pam_env.so ...省略n行... bash 1517 vagrant 0u CHR 136,0 0t0 3 /dev/pts/0 bash 1517 vagrant 1u CHR 136,0 0t0 3 /dev/pts/0 bash 1517 vagrant 2u CHR 136,0 0t0 3 /dev/pts/0 bash 1517 vagrant 255u CHR 136,0 0t0 3 /dev/pts/0 ~~~ ### 5 查询某个PID的进程调用了哪些文件 ~~~ [vagrant~] ]$sudo lsof -p 1003 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1003 www-data cwd DIR 8,1 4096 2 / nginx 1003 www-data rtd DIR 8,1 4096 2 / nginx 1003 www-data txt REG 8,1 873176 1573477 /usr/sbin/nginx nginx 1003 www-data mem REG 8,1 47712 1053013 /lib/x86_64-linux-gnu/libnss_files-2.19.so ...省略n行... nginx 1003 www-data 6u IPv4 9497 0t0 TCP *:http (LISTEN) nginx 1003 www-data 7u IPv6 9498 0t0 TCP *:http (LISTEN) nginx 1003 www-data 13u 0000 0,9 0 5249 anon_inode ~~~ ### 更多 lsof 的命令: ~~~ lsof -i:8080:查看8080端口占用 lsof abc.txt:显示开启文件abc.txt的进程 lsof -c abc:显示abc进程现在打开的文件 lsof -c -p 1234:列出进程号为1234的进程所打开的文件 lsof -g gid:显示归属gid的进程情况 lsof +d /usr/local/:显示目录下被进程开启的文件 lsof +D /usr/local/:同上,但是会搜索目录下的目录,时间较长 lsof -d 4:显示使用fd为4的进程 lsof -i -U:显示所有打开的端口和UNIX domain文件 ~~~ ## `ps` 静态查看进程 ### 1 `ps -l`只查看自己bash(操作环境)有关的进程 ~~~ [root@tatahy-lamp ~]# ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 7092 7090 0 80 0 - 29103 wait pts/0 00:00:00 bash 0 R 0 7124 7092 0 80 0 - 34334 - pts/0 00:00:00 ps ~~~ ### 2 `ps aux`或`ps -lA`查看系统所有进程 ~~~ [root@tatahy-lamp ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.7 65028 27552 ? Ss 2018 16:46 /usr/lib/systemd/systemd --switched-root --system - root 2 0.0 0.0 0 0 ? S 2018 0:04 [kthreadd] root 3 0.0 0.0 0 0 ? S 2018 0:16 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 2018 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S 2018 0:00 [kworker/u4:0] root 7 0.0 0.0 0 0 ? S 2018 0:01 [migration/0] root 8 0.0 0.0 0 0 ? S 2018 0:00 [rcu_bh] ...... mysql 4492 0.0 0.0 113120 1592 ? Ss 2018 0:00 /bin/sh /usr/bin/mysqld_safe mysql 4692 0.0 3.0 896772 119540 ? Sl 2018 108:02 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/ apache 32405 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND apache 32406 0.0 0.2 606700 10644 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND apache 32407 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND apache 32408 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND apache 32409 0.0 0.2 606700 10152 ? S Mar24 0:00 /usr/sbin/httpd -DFOREGROUND ~~~ ### 3 `ps axjf`查看系统进程树 ~~~ [root@tatahy-lamp ~]# ps axjf PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 0 2 0 0 ? -1 S 0 0:04 [kthreadd] 2 3 0 0 ? -1 S 0 0:16 \_ [ksoftirqd/0] 2 5 0 0 ? -1 S< 0 0:00 \_ [kworker/0:0H] 2 6 0 0 ? -1 S 0 0:00 \_ [kworker/u4:0] 2 7 0 0 ? -1 S 0 0:01 \_ [migration/0] 2 8 0 0 ? -1 S 0 0:00 \_ [rcu_bh] ...... 1 741 741 741 ? -1 Ss 0 13:19 /usr/sbin/httpd -DFOREGROUND 741 32405 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND 741 32406 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND 741 32407 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND 741 32408 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND 741 32409 741 741 ? -1 S 48 0:00 \_ /usr/sbin/httpd -DFOREGROUND 1 755 755 755 ? -1 Ss 0 0:12 /usr/sbin/sshd -D 755 7090 7090 7090 ? -1 Ss 0 0:00 \_ sshd: root@pts/0 7090 7092 7092 7092 pts/0 7133 Ss 0 0:00 \_ -bash 7092 7133 7133 7092 pts/0 7133 R+ 0 0:00 \_ ps axjf ...... 1 4492 4492 4492 ? -1 Ss 27 0:00 /bin/sh /usr/bin/mysqld_safe 4492 4692 4492 4492 ? -1 Sl 27 108:02 \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/ 1 18460 18460 18460 ? -1 S<sl 0 22:57 /usr/local/aegis/aegis_update/AliYunDunUpdate 1 18481 18481 18481 ? -1 S<sl 0 263:26 /usr/local/aegis/aegis_client/aegis_10_59/AliYunDun 1 16057 16057 16057 ? -1 Ssl 0 38:33 /usr/local/cloudmonitor/CmsGoAgent.linux-amd64 ~~~ ### 4 `pstree`进程树/相关性 ~~~ [root@tatahy-lamp ~]# pstree -Aup systemd(1)-+-AliYunDun(18481)-+-{AliYunDun}(18482) | |-{AliYunDun}(18483) | |-{AliYunDun}(18494) | |-{AliYunDun}(18495) | |-{AliYunDun}(18496) | |-{AliYunDun}(18497) | |-{AliYunDun}(18498) | |-{AliYunDun}(18499) | |-{AliYunDun}(18500) | |-{AliYunDun}(18501) | |-{AliYunDun}(18502) | |-{AliYunDun}(18503) | |-{AliYunDun}(18504) | |-{AliYunDun}(18505) | |-{AliYunDun}(18506) | |-{AliYunDun}(18507) | |-{AliYunDun}(18508) | `-{AliYunDun}(19062) |-AliYunDunUpdate(18460)-+-{AliYunDunUpdate}(18462) | |-{AliYunDunUpdate}(18463) | `-{AliYunDunUpdate}(18466) |-CmsGoAgent.linu(16057)-+-{CmsGoAgent.linu}(16058) | |-{CmsGoAgent.linu}(16059) | |-{CmsGoAgent.linu}(16060) | |-{CmsGoAgent.linu}(16061) | |-{CmsGoAgent.linu}(16062) | |-{CmsGoAgent.linu}(16063) | |-{CmsGoAgent.linu}(16067) | |-{CmsGoAgent.linu}(16068) | |-{CmsGoAgent.linu}(16076) | |-{CmsGoAgent.linu}(16077) | |-{CmsGoAgent.linu}(16091) | |-{CmsGoAgent.linu}(16159) | `-{CmsGoAgent.linu}(16160) |-aliyun-service(30690)-+-{aliyun-service}(30691) | |-{aliyun-service}(30692) | |-{aliyun-service}(30693) | |-{aliyun-service}(30694) | `-{aliyun-service}(30695) ~~~ ### 5 找出与`cron`和`syslog`服务有关的PID ~~~ [root@tatahy-lamp ~]# ps aux | egrep '(cron|syslog)' root 481 0.0 0.2 396480 10424 ? Ssl 2018 0:40 /usr/sbin/rsyslogd -n root 517 0.0 0.0 126300 1604 ? Ss 2018 1:17 /usr/sbin/crond -n root 7142 0.0 0.0 108040 888 pts/0 S+ 14:49 0:00 egrep --color=auto (cron|syslog) ~~~ ### 6 僵尸(zombie)进程 通常, 造成僵尸进程的成因是因为该进程应该已经执行完毕,或者是因故应该要终止了,但是该进程的父进程却无法完整将该进程结束掉,而造成那个进程一直存在内存当中。 如果你发现在某个进程的 CMD 后面还接上`<defunct>`时, 就代表该进程是僵尸进程, 例如: ![](https://box.kancloud.cn/c4fc3022d27681e6bb7e91f82ea663c5_2928x152.jpg) 当系统不稳定的时候就容易造成所谓的僵尸进程。可能的原因: * 程序写得不好 * 用户的操作习惯不良等 如果系统中很多僵尸进程时,要找出该进程的父进程,然后好好做个跟踪,好好进行主机的环境优化。 看看有什么地方需要改善的,不要只是直接将它杀掉而已呢! 不然的话, 万一它一直产生, 那可就麻烦了! 事实上,通常僵尸进程都已经无法控管,而直接是交给`init`这个程序来负责了,偏偏`init`是系统第一个执行的程序,它是所有进程的父进程! 我们无法杀掉该进程 ( 杀掉它,系统就死掉了)。 所以,如果产生僵尸进程,而系统过一阵子还没有办法通过内核非经常性的特殊处理来将该进程删除时,那你只好通过`reboot`的方式来将该进程抹去了! ## `top` 动态查看进程 ~~~ [root@tatahy-lamp ~]# top [-d 数字] | top [-bnp] ~~~ 参数: `-d`:后面可以接秒数,就是整个进程界面更新的秒数,默认是5秒。 `-b`:以批次的方式执行top,还有更多的参数可以使用。 通常会搭配数据流重定向来将批处理的结果输出成为文件。 `-n`:与`-b`搭配,意义是,需要进行几次 top 的输出结果。 `-p`:指定某些个 PID来进行查看监测而已。 在top执行过程当中可以使用的按键命令: `?`:显示在 top 当中可以输入的按键命令; `P`:以CPU 的使用资源排序显示; `M`:以内存的使用资源排序显示; `N`:以PID 来排序; `T`:由该进程使用的 CPU 时间累积(TIME+) 排序; `k`: 给予某个 PID一个信号(signal); `r`:给予某个 PID 重新制定一个 nice 值; `q`:离开top 软件的按键。 ### 1 查看自己bash的PID信息 ~~~ [root@tatahy-lamp ~]# echo $$ 7092 [root@tatahy-lamp ~]# top -d 2 -p 7092 top - 15:11:06 up 275 days, 4:09, 2 users, load average: 0.00, 0.01, 0.05 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 3883400 total, 1799432 used, 2083968 free, 178004 buffers KiB Swap: 0 total, 0 used, 0 free. 1032172 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7092 root 20 0 116412 3148 1672 S 0.0 0.1 0:00.05 bash ~~~ ### 2 将`top`的信息存储到指定文件 ~~~ [root@tatahy-lamp ~]# top -b -n 2 >/tmp/top.txt [root@tatahy-lamp ~]# cat /tmp/top.txt ~~~ ## `netstat` 跟踪网络 常被用于网络监控。 ~~~ [root@tatahy-lamp ~]# netstat [-atunlp] ~~~ 参数: `-a`:将目前系统上所有的连接,监听,Socket 数据都列出来; `-t`:列出 tcp 网络数据包的数据; `-u`:列出udp 网络数据包的数据; `-n`:不列出进程的服务名称,以端口号(port number)来显示; `-l`:列出目前正在网络监听 (listen)的服务; `-p`:列出该网络服务的进程 PID。 ### 1 列出目前系统已经新建的网络连接与unix socket状态 ~~~ [root@tatahy-lamp ~]# netstat Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 240 tatahy-lamp:ssh 121.33.248.6:65422 ESTABLISHED tcp 0 0 tatahy-lamp:ssh 121.33.248.6:65044 ESTABLISHED tcp 0 0 tatahy-lamp:40759 100.100.30.25:http ESTABLISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ] DGRAM 9664 /run/systemd/shutdownd unix 2 [ ] DGRAM 6881 /run/systemd/notify unix 5 [ ] DGRAM 6902 /run/systemd/journal/socket unix 8 [ ] DGRAM 6904 /dev/log unix 2 [ ] DGRAM 11514 /var/run/nscd/socket unix 3 [ ] STREAM CONNECTED 11807 unix 3 [ ] STREAM CONNECTED 10822055 ~~~ ### 2 找出目前系统上已在监听的网络连接及其PID ~~~ [root@tatahy-lamp ~]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 741/httpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 755/sshd tcp6 0 0 :::3306 :::* LISTEN 4692/mysqld ~~~ ## 查看系统的服务和对应端口号 查看文件`/etc/services`内容就可以知道当前系统的服务和对应端口号。也可以通过修改该文件中的内容来更改某个服务的端口号。 ~~~ [root@tatahy-lamp selinux]# cat /etc/services | grep 'mysql' mysql 3306/tcp # MySQL mysql 3306/udp # MySQL mysql-cluster 1186/tcp # MySQL Cluster Manager mysql-cluster 1186/udp # MySQL Cluster Manager mysql-cm-agent 1862/tcp # MySQL Cluster Manager Agent mysql-cm-agent 1862/udp # MySQL Cluster Manager Agent mysql-im 2273/tcp # MySQL Instance Manager mysql-im 2273/udp # MySQL Instance Manager mysql-proxy 6446/tcp # MySQL Proxy mysql-proxy 6446/udp # MySQL Proxy ~~~ >[warning] 关闭服务/端口:就关闭(kill)提供服务的进程就可以了。 ## `service`(进程)显示/管理系统目前的服务 CentOS中提供`service`可以用来启动`stand alone`类型的服务的脚本。`service`就是一个脚本(路径:`/sbin/service`),它通过分析`service`后面的参数再到`/etc/init.d/`目录下取得正确的服务来`start`或`stop`。 ~~~ [root@www~]# service [service name](start|stop|restartl...) [root@www~]# service --status-all ~~~ `–status-all` 将会运行所有的启动脚本来显示各个服务的运行状态: 参数: `service name`:即是需要启动的服务名称,需与 `/etc/init.d/` 对应; `startl...`:即是该服务要进行的工作; `--status-all`:将系统所有的`stand alone`的服务状态全部列出来。 `systemctl`,`init`,`systemd`是什么??