[root@localhost ~]# w 22:02:05 up 1:12, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.66.1 20:51 5.00s 0.03s 0.00s w
第一行从左面开始显示的信息依次为:时间,系统运行时间,登陆用户数,系统平均负载(1分钟、5分钟、15分钟、使用cpu的活动的进程有多少个)。
第二行开始以及下面全部的行,当前登陆的都有哪些用户,以及他们是从哪里登陆的等等。linux
load averag第一个数值表示1分钟内系统的平均负载值;第二个数值表示5分钟内系统的平均负载值;第三个数值表示15分钟系统的平均负载值。这个值的意义是,单位时间段内CPU活动进程数。固然这个值越大就说明你的服务器压力越大。通常状况下这个值只要不超过服务器的cpu数量就没有关系。
查看cup信息
#cat /proc/cpuinfoios
[root@localhost ~]# cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 60 model name : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz stepping : 3 microcode : 0x1e cpu MHz : 2393.712 cache size : 6144 KB physical id : 0 siblings : 1 core id : 0
processor数量+1表示有多少个逻辑cpu,若是数字为1,则表示有2个逻辑cpu,以此类推,物理cpu能够查看physical id这个字段
#uptime命令能够查看机器运行时间,登录用户有几个,系统平均负载web
[root@localhost ~]# uptime 20:24:09 up 1:37, 2 users, load average: 0.00, 0.01, 0.05
[root@localhost ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 775360 2076 121164 0 0 16 3 56 61 0 0 100 0 0
vmstat命令打印的结果共分为6部分:procs, memory, swap, io, system, cpu. 重点关注一下r b si so bi bo几列
1)procs 显示进程相关信息缓存
r :表示运行和等待cpu时间片的进程数,若是长期大于服务器cpu的个数,则说明cpu不够用了;bash
b :表示等待资源的进程数,好比等待I/O, 内存等,这列的值若是长时间大于1,则须要关注一下了;服务器
2)memory 内存相关信息网络
swpd :表示切换到交换分区中的内存数量 ;多线程
free :当前空闲的内存数量;运维
buff :缓冲大小,(即将写入磁盘的);dom
cache :缓存大小,(从磁盘中读取的);
3)swap 内存交换状况
si :由交换区写入到内存的数据量;
so :由内存写入到交换区的数据量;
4)io 磁盘使用状况
bi :从块设备读取数据的量(读磁盘);
bo: 从块设备写入数据的量(写磁盘);
5)system 显示采集间隔内发生的中断次数
in :表示在某一时间间隔中观测到的每秒设备中断数;
cs :表示每秒产生的上下文切换次数;
6)CPU 显示cpu的使用状态
us :显示了用户下所花费 cpu 时间的百分比;
sy :显示系统花费cpu时间百分比;
id :表示cpu处于空闲状态的时间百分比;
wa :表示I/O等待所占用cpu时间百分比;
st :表示被偷走的cpu所占百分比
vmstat 查看系统状态的时候,一般都是使用这样的形式来看的
[root@localhost ~]# vmstat 1 5
或者
[root@localhost ~]# vmstat 1
前面表示,每隔一秒钟打印一次状态,共打印5次,然后面的表示每隔1秒打印一次状态,一直打印,能够按 Ctrl + c 结束
top 命令能够动态显示进程所占系统资源
[root@localhost ~]# top top - 20:39:26 up 1:52, 2 users, load average: 0.00, 0.01, 0.05 Tasks: 87 total, 1 running, 86 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 : 999700 total, 774940 free, 101488 used, 123272 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 750596 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1604 root 20 0 157632 2184 1548 R 0.3 0.2 0:00.01 top 1 root 20 0 125376 3812 2540 S 0.0 0.4 0:01.16 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.06 ksoftirqd/0 5 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.14 kworker/u256: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.27 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/0 12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
从上面能够看出
总共87个任务(进程),1个任务处于运行中,86个进程处于休眠状态,0个中止的,0个僵尸进程
zombie:僵尸进程,主进程已经意外中止,剩下一些子进程
st:表示被偷走的cpu百分比,若是服务器作了虚拟化,那么一些子机会偷走主机的cpu
若是us长时间占用cpu长期大于20-30%,会使cpu匮乏,对cpu有害
进程排序:默认按cpu占用率排序,若是按下M键,则按照内存占用大小排序,再按下P,恢复为cpu排序
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 778 root 20 0 562428 18644 5936 S 0.0 1.9 0:00.99 tuned 509 root 20 0 210220 13912 3244 S 0.0 1.4 0:00.40 rsyslogd 526 polkitd 20 0 534172 12040 4628 S 0.0 1.2 0:00.13 polkitd 515 root 20 0 556272 9308 6952 S 0.0 0.9 0:00.67 NetworkManager 522 root 20 0 305332 6280 4900 S 0.0 0.6 0:05.76 vmtoolsd 521 root 20 0 99644 6116 4512 S 0.0 0.6 0:00.09 VGAuthService 848 root 20 0 148360 5412 4112 S 0.0 0.5 0:00.27 sshd 1515 root 20 0 148360 5408 4112 S 0.0 0.5 0:00.17 sshd 780 root 20 0 106040 4096 3116 S 0.0 0.4 0:00.08 sshd
RES :实际占用的物理内存大小,以KB为单位
按下数字1:显示全部cpu的占用率
%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
按下q键能够退出top
top -c 能够查看到全局的进程命令
[root@localhost ~]# top -c top - 20:59:03 up 2:12, 2 users, load average: 0.00, 0.01, 0.05 Tasks: 88 total, 1 running, 87 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 999700 total, 774800 free, 101620 used, 123280 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 750460 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 125376 3812 2540 S 0.0 0.4 0:01.17 /usr/lib/systemd/systemd --switch+ 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kthreadd] 3 root 20 0 0 0 0 S 0.0 0.0 0:00.07 [ksoftirqd/0] 5 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.18 [kworker/u256: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.29 [rcu_sched] 10 root rt 0 0 0 0 S 0.0 0.0 0:00.03 [watchdog/0] 12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kdevtmpfs] 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns] 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [khungtaskd]
top -bn1 静态的一次性地显示全部的信息,适合写脚本时使用
[root@localhost ~]# top -bn1 top - 20:59:43 up 2:13, 2 users, load average: 0.00, 0.01, 0.05 Tasks: 88 total, 1 running, 87 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 : 999700 total, 774924 free, 101496 used, 123280 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 750584 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 125376 3812 2540 S 0.0 0.4 0:01.17 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.07 ksoftirqd/0 5 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.18 kworker/u256: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.29 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/0 12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
yum install -y sysstat 若是没有sar命令能够安装这个包 sar -n DEV 网卡流量 sar -q 系统负载 sar -b 磁盘读写 sar -f /var/log/sa/saxx 历史文件
sar命令第一次使用会提示没有文件或目录,那是由于sar不加选项会调用系统保留的历史文件 /var/log/sa下的文件,要想正常调用,必须等到10分钟的整数时间点才会有这个文件
[root@localhost ~]# sar 没法打开 /var/log/sa/sa10: 没有那个文件或目录
sar -n DEV 1 10 查看网卡流量,每一秒打印一次,总共打印10次
[root@localhost ~]# sar -n DEV 1 10 Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) 21时10分31秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 21时10分32秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21时10分32秒 ens33 1.00 1.00 0.06 0.20 0.00 0.00 0.00 21时10分32秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 21时10分33秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21时10分33秒 ens33 1.00 1.00 0.06 0.40 0.00 0.00 0.00 21时10分33秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 21时10分34秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21时10分34秒 ens33 1.00 1.00 0.06 0.40 0.00 0.00 0.00 21时10分34秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 21时10分35秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21时10分35秒 ens33 1.01 1.01 0.06 0.40 0.00 0.00 0.00 21时10分35秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 21时10分36秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21时10分36秒 ens33 2.00 2.00 0.12 0.46 0.00 0.00 0.00
其中IFACE表示网卡的名字,r表示recieve接受的,t表示transport传输出去的
rxpck表示接收到的数据包量,几千的数据包量属于正常的,若是上万就可能被×××了,须要借用抓包工具查看
txpck表示传输出去的数据包量
rxkB表示接收到的数据量为多少KB
txkB表示传输出去的数据量为多少KB
sar -f /var/log/sa/saxx 查看历史信息
[root@localhost ~]# sar -n DEV -f /var/log/sa/sa10 Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) 20时20分01秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 20时30分01秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 20时30分01秒 ens33 0.79 0.24 0.05 0.02 0.00 0.00 0.00 20时40分01秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 20时40分01秒 ens33 0.71 0.22 0.04 0.13 0.00 0.00 0.00 20时50分01秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sar -n DEV -f /var/log/sa/sa10 能够查看历史文件10号的网卡流量信息,sa10文件为10号的文件,/var/log/sa目录下的文件最多保留1个月, 当天/var/log/sa/目录下会生成相似日期为sa10的二进制文件,次日这个目录下会生成sar10的文件,sar10的文件能够cat查看
sar -q查看历史负载
[root@localhost ~]# sar -q Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) 20时20分01秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked 20时30分01秒 1 103 0.00 0.01 0.05 0 20时40分01秒 2 104 0.00 0.01 0.05 0 20时50分01秒 1 105 0.02 0.03 0.05 0 21时00分01秒 1 104 0.00 0.01 0.05 0 21时10分01秒 1 105 0.00 0.02 0.05 0 21时20分01秒 1 104 0.00 0.01 0.05 0 21时30分01秒 1 103 0.00 0.01 0.05 0 平均时间: 1 104 0.00 0.01 0.05 0
sar -b 查看磁盘
[root@localhost ~]# sar -b Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) 20时20分01秒 tps rtps wtps bread/s bwrtn/s 20时30分01秒 0.08 0.00 0.07 0.04 0.81 20时40分01秒 0.05 0.00 0.05 0.11 0.69 20时50分01秒 0.05 0.00 0.05 0.00 0.46
若是没有nload命令,须要安装包,由于默认源里面没有nload包,因此须要安装扩展源
yum install epel-release yum install nload
Device ens33 [192.168.66.130] (1/2): ======================================================================================================= Incoming: Curr: 944.00 Bit/s Avg: 1.11 kBit/s Min: 936.00 Bit/s Max: 1.84 kBit/s Ttl: 529.79 kByte Outgoing: Curr: 8.27 kBit/s Avg: 8.14 kBit/s Min: 4.23 kBit/s Max: 8.73 kBit/s Ttl: 3.89 MByte
按左右方向键能够切换网卡,按q退出
有×××时Incoming的流量会比较大,
占用带宽看的是Outgoing的流量
有时候查看到的cpu和内存还有剩余,可是系统负载仍是很高,咱们用vmstat命令查看发现b列或wa列值比较大,说明系统磁盘有瓶颈,这时候就须要去查看磁盘的状态了
安装包:yum install -y sysstat 和sar命令是同一个包
直接输入iostat命令
[root@localhost ~]# iostat Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.03 0.00 0.10 0.17 0.00 99.70 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 1.06 10.01 2.35 97333 22884 scd0 0.00 0.11 0.00 1028 0
能够看到cpu负载和磁盘的读写数据信息
iostat -x
加上-x选项指标会更多,其中一个比较重要的是%util,这个指标表明的是cpu等待io进程的时间比例,若是这个值很大,说明磁盘io很忙,则读写rkB/s wkB/s 也会很大,若是读写值不大,则说明磁盘有问题或故障
[root@localhost ~]# iostat -x Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2018年06月10日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.03 0.00 0.10 0.16 0.00 99.70 Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util sda 0.00 0.02 0.47 0.57 9.83 2.35 23.37 0.01 12.47 12.36 12.56 2.96 0.31 scd0 0.00 0.00 0.00 0.00 0.10 0.00 114.22 0.00 23.83 23.83 0.00 22.78 0.00
iotop
安装包yum install -y iotop
iotop动态显示磁盘信息,将磁盘使用状况根据占用IO的比例大小排序后展现出来
[root@localhost ~]# 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: 190.20 K/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 --switched-r~stem --deserialize 21 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] 6 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/u256:0] 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 % [rcu_sched] 10 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/0] 523 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % rsyslogd -n [rs:main Q:Reg] 12 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kdevtmpfs] 13 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [netns] 14 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [khungtaskd] 15 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [writeback] 16 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kintegrityd] 17 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [bioset] 18 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kblockd] 19 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [md] 533 be/4 chrony 0.00 B/s 0.00 B/s 0.00 % 0.00 % chronyd 535 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % agetty --nocl
[root@localhost ~]# free total used free shared buff/cache available Mem: 999700 102480 608556 6868 288664 725444 Swap: 2097148 0 2097148 [root@localhost ~]# free -m total used free shared buff/cache available Mem: 976 100 594 6 281 708 Swap: 2047 0 2047 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 976M 100M 594M 6.7M 281M 708M Swap: 2.0G 0B 2.0G
直接输入free能够看到内存的使用信息,默认为kb,-m又MB显示,-h又合适的单位显示,其中:
total=used+free+buff/cache,available包含了free和buff/cache剩余部分
buff:缓冲,当cpu写数据到磁盘时,被暂放在内存中 的数据
cache:缓存,当cpu从磁盘读取数据时,被暂放在内存中的数据
ps命令用于报告当前系统的进程状态。能够搭配kill指令随时中断、删除没必要要的程序。ps命令是最基本同时也是很是强大的进程查看命令,使用该命令能够肯定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等,总之大部分信息都是能够经过执行该命令获得的。
用法:ps aux或者ps -elf
a:显示现行终端机下的全部程序,包括其余用户的程序
u:以用户为主的格式来显示程序情况
x:显示全部程序,不以终端机来区分
-e:此选项的效果和a选项相同
e:列出程序时,显示每一个程序所使用的环境变量
-f:显示UID,PPIP,C与STIME栏位
f:用ASCII字符显示树状结构,表达程序间的相互关系
-l或l:采用详细的格式来显示程序情况
[root@localhost ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 125376 3812 ? Ss 18:46 0:01 /usr/lib/systemd/systemd --switched-r root 2 0.0 0.0 0 0 ? S 18:46 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 18:46 0:00 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 18:46 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S 18:46 0:00 [kworker/u256:0] root 7 0.0 0.0 0 0 ? S 18:46 0:00 [migration/0] root 8 0.0 0.0 0 0 ? S 18:46 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? R 18:46 0:00 [rcu_sched] root 10 0.0 0.0 0 0 ? S 18:46 0:00 [watchdog/0] root 12 0.0 0.0 0 0 ? S 18:46 0:00 [kdevtmpfs] root 13 0.0 0.0 0 0 ? S< 18:46 0:00 [netns] root 14 0.0 0.0 0 0 ? S 18:46 0:00 [khungtaskd] root 15 0.0 0.0 0 0 ? S< 18:46 0:00 [writeback] root 16 0.0 0.0 0 0 ? S< 18:46 0:00 [kintegrityd] root 17 0.0 0.0 0 0 ? S< 18:46 0:00 [bioset] root 18 0.0 0.0 0 0 ? S< 18:46 0:00 [kblockd] root 19 0.0 0.0 0 0 ? S< 18:46 0:00 [md] root 25 0.0 0.0 0 0 ? S 18:46 0:00 [kswapd0] root 26 0.0 0.0 0 0 ? SN 18:46 0:00 [ksmd] root 27 0.0 0.0 0 0 ? SN 18:46 0:00 [khugepaged] root 28 0.0 0.0 0 0 ? S< 18:46 0:00 [crypto] root 36 0.0 0.0 0 0 ? S< 18:46 0:00 [kthrotld] root 37 0.0 0.0 0 0 ? S 18:46 0:00 [kworker/u256:1]
PID :进程的id,这个id颇有用,在linux中内核管理进程就得靠pid来识别和管理某一个程,好比我想终止某一个进程,则用 ‘kill 进程的pid 有时并不能杀掉,则须要加一个-9选项了 kill -9 进程pid
STAT :表示进程的状态,进程状态分为如下几种(不要求记住,但要了解)
D 不能中断的进程(一般为IO)
R 正在运行中的进程
S 已经中断的进程,一般状况下,系统中大部分进程都是这个状态
T 已经中止或者暂停的进程,若是咱们正在运行一个命令,好比说 sleep 10 若是咱们按一下ctrl -z 让他暂停,那么咱们用ps查看就会显示T这个状态
W 这个好像是说,从内核2.6xx 之后,表示为没有足够的内存页分配
X 已经死掉的进程(这个好像历来不会出现)
Z 僵尸进程,杀不掉,打不死的垃圾进程,占系统一小点资源,不过没有关系。若是太多,就有问题了。通常不会出现。
< 高优先级进程
N 低优先级进程
L 在内存中被锁了内存分页
s 主进程
l 多线程进程
[root@localhost ~]# ps aux |grep sshd root 780 0.0 0.4 106040 4096 ? Ss 18:46 0:00 /usr/sbin/sshd -D root 848 0.0 0.5 148360 5412 ? Ss 18:48 0:00 sshd: root@pts/0 root 1515 0.0 0.5 148360 5408 ? Ss 20:16 0:00 sshd: root@pts/1 root 1958 0.0 0.0 112720 968 pts/1 R+ 22:03 0:00 grep --color=auto sshd
ls -l /proc/进程号,能够查看某个进程的启动位置
[root@localhost ~]# ls -l /proc/780 总用量 0 dr-xr-xr-x 2 root root 0 6月 10 22:04 attr -rw-r--r-- 1 root root 0 6月 10 22:04 autogroup -r-------- 1 root root 0 6月 10 22:04 auxv -r--r--r-- 1 root root 0 6月 9 20:49 cgroup --w------- 1 root root 0 6月 10 22:04 clear_refs -r--r--r-- 1 root root 0 6月 9 20:49 cmdline -rw-r--r-- 1 root root 0 6月 9 20:49 comm -rw-r--r-- 1 root root 0 6月 10 22:04 coredump_filter -r--r--r-- 1 root root 0 6月 10 22:04 cpuset lrwxrwxrwx 1 root root 0 6月 10 22:04 cwd -> / -r-------- 1 root root 0 6月 10 21:53 environ lrwxrwxrwx 1 root root 0 6月 9 20:49 exe -> /usr/sbin/sshd ...
netstat -lnp查看监听端口,l表示listen,n以数字显示,p表示port端口
[root@localhost ~]# netstat -lnp 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:22 0.0.0.0:* LISTEN 780/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1475/sendmail: acce tcp6 0 0 :::22 :::* LISTEN 780/sshd udp 0 0 127.0.0.1:323 0.0.0.0:* 533/chronyd udp6 0 0 ::1:323 :::* 533/chronyd raw6 0 0 :::58 :::* 7 515/NetworkManager Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node PID/Program name Path unix 2 [ ACC ] STREAM LISTENING 12554 1/systemd /run/systemd/private unix 2 [ ACC ] SEQPACKET LISTENING 12570 1/systemd /run/udev/control unix 2 [ ACC ] STREAM LISTENING 14921 1/systemd /var/run/dbus/system_bus_socket unix 2 [ ACC ] STREAM LISTENING 8423 1/systemd /run/systemd/journal/stdout unix 2 [ ACC ] STREAM LISTENING 16111 521/VGAuthService /var/run/vmware/guestServicePipe
同一台服务器的两个进程间使用socket协议通讯
netstat -lntp 只查看tcp协议端口状态,t表示tcp
[root@localhost ~]# netstat -lntp 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:22 0.0.0.0:* LISTEN 780/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1475/sendmail: acce tcp6 0 0 :::22 :::* LISTEN 780/sshd
netstat -an 查看全部的链接状态
[root@localhost ~]# netstat -an Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 52 192.168.66.130:22 192.168.66.1:55443 ESTABLISHED tcp 0 0 192.168.66.130:22 192.168.66.1:52857 ESTABLISHED tcp6 0 0 :::22 :::* LISTEN udp 0 0 127.0.0.1:323 0.0.0.0:* udp6 0 0 ::1:323 :::* raw6 0 0 :::58 :::* 7 Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ] DGRAM 15872 /var/run/chrony/chronyd.sock unix 2 [ ACC ] STREAM LISTENING 12554 /run/systemd/private unix 2 [ ACC ] SEQPACKET LISTENING 12570 /run/udev/control unix 2 [ ] DGRAM 12613 /run/systemd/shutdownd unix 2 [ ACC ] STREAM LISTENING 14921 /var/run/dbus/system_bus_socket unix 2 [ ] DGRAM 8413 /run/systemd/notify unix 2 [ ] DGRAM 8415 /run/systemd/cgroups-agent unix 2 [ ACC ] STREAM LISTENING 8423 /run/systemd/journal/stdout unix 5 [ ] DGRAM 8426 /run/systemd/journal/socket unix 13 [ ] DGRAM 8428 /dev/log unix 2 [ ACC ] STREAM LISTENING 16111 /var/run/vmware/guestServicePipe unix 3 [ ] STREAM CONNECTED 18586 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 14992 unix 3 [ ] STREAM CONNECTED 15309 unix 3 [ ] STREAM CONNECTED 14953 unix 2 [ ] DGRAM 28422 unix 3 [ ] STREAM CONNECTED 15310 /run/systemd/journal/stdout unix 2 [ ] DGRAM 16114 unix 3 [ ] STREAM CONNECTED 15124 /var/run/dbus/system_bus_socket unix 2 [ ] DGRAM 27899 unix 3 [ ] STREAM CONNECTED 15560 /run/systemd/journal/stdout unix 2 [ ] DGRAM 28249 unix 2 [ ] DGRAM 19064 unix 3 [ ] STREAM CONNECTED 15559 unix 2 [ ] DGRAM 16113 unix 3 [ ] STREAM CONNECTED 15358 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 15357 unix 3 [ ] STREAM CONNECTED 17883 unix 3 [ ] STREAM CONNECTED 17953 unix 3 [ ] STREAM CONNECTED 18585 unix 3 [ ] STREAM CONNECTED 14993 /run/systemd/journal/stdout unix 2 [ ] DGRAM 12955 unix 3 [ ] STREAM CONNECTED 16180 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 17884 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 17954 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 16010 /var/run/dbus/system_bus_socket unix 2 [ ] DGRAM 16024 unix 3 [ ] STREAM CONNECTED 15821 unix 3 [ ] STREAM CONNECTED 15822 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 14784 unix 3 [ ] STREAM CONNECTED 15180 unix 3 [ ] STREAM CONNECTED 16009 unix 3 [ ] STREAM CONNECTED 14783 unix 3 [ ] STREAM CONNECTED 16043 unix 3 [ ] STREAM CONNECTED 15123 unix 2 [ ] DGRAM 15995 unix 2 [ ] DGRAM 15661 unix 3 [ ] STREAM CONNECTED 15181 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 13347 /run/systemd/journal/stdout unix 2 [ ] DGRAM 15183 unix 2 [ ] DGRAM 15933 unix 3 [ ] STREAM CONNECTED 16179 unix 3 [ ] DGRAM 13373 unix 2 [ ] DGRAM 14774 unix 3 [ ] STREAM CONNECTED 15668 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 13346 unix 3 [ ] STREAM CONNECTED 15667 unix 3 [ ] STREAM CONNECTED 16044 /var/run/dbus/system_bus_socket unix 3 [ ] DGRAM 13374 unix 3 [ ] STREAM CONNECTED 15122 unix 2 [ ] DGRAM 15824 unix 2 [ ] DGRAM 13353
关于网络链接的一些状态能够查看 tcp三次握手四次挥手的文章 :
http://www.javashuo.com/article/p-ucrnmzvv-gg.html
统计tcp链接各类状态的数量
netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
[root@localhost ~]# netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}' LISTEN 3 ESTABLISHED 2
若是处于ESTABLISHED的状态不少,说明系统很忙,通常1000之内 的数字服务器都可以接受,若是几万个说明有可能受到×××
ss -an 用这个命令查看网络状态时不会显示进程的名字和PID
[root@localhost ~]# ss -an Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port nl UNCONN 0 0 0:2055209475 * nl UNCONN 0 0 0:0 * nl UNCONN 0 0 0:2055209475 * nl UNCONN 768 0 4:0 * nl UNCONN 4352 0 4:2025 * nl UNCONN 0 0 6:0 * nl UNCONN 0 0 7:0 * nl UNCONN 0 0 9:1 * nl UNCONN 0 0 9:0 * nl UNCONN 0 0 9:486 * nl UNCONN 0 0 10:0 * nl UNCONN 0 0 11:0 * nl UNCONN 0 0 15:-4117 * nl UNCONN 0 0 15:527 * nl UNCONN 0 0 15:-4119 * nl UNCONN 0 0 15:-4120 * nl UNCONN 0 0 15:0 * nl UNCONN 0 0 15:778 * nl UNCONN 0 0 15:-4107 * nl UNCONN 0 0 15:515 *
抓包工具:tcpdump
用法:tcpdump -nn
[root@localhost ~]# tcpdump -nn tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
若是出现上面状况,说明网卡名字比较特殊,须要在命令中指定网卡名字
tcpdump -nn -i 网卡名字
命令中的两个n表示将下面中的第一IP和端口以数字的方式展现出来,不指定的话会显示为主机名和端口名称
22:22:04.599754 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1205320, win 2049, length 0 22:22:04.599762 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205320:1205500, ack 157, win 274, length 180 22:22:04.599866 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205500:1205776, ack 157, win 274, length 276 22:22:04.599975 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1205776, win 2047, length 0 22:22:04.599983 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205776:1205956, ack 157, win 274, length 180 22:22:04.600087 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205956:1206232, ack 157, win 274, length 276 22:22:04.600199 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1206232, win 2053, length 0 22:22:04.600207 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206232:1206412, ack 157, win 274, length 180 22:22:04.600310 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206412:1206688, ack 157, win 274, length 276 22:22:04.600418 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1206688, win 2051, length 0 22:22:04.600426 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206688:1206868, ack 157, win 274, length 180 22:22:04.600535 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206868:1207144, ack 157, win 274, length 276 22:22:04.600644 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1207144, win 2049, length 0 22:22:04.600653 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1207144:1207324, ack 157, win 274, length 180 22:22:04.600757 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1207324:1207600, ack 157, win 274, length 276
咱们主要查看的是两个IP间数据的流向和length,通常咱们看到的包都是TCP的包,若是看到UDP的包,颇有多是被×××了
# tcpdump -nn port 端口号 //抓取指定端口号
# tcpdump -nn not port 端口号 //不抓取指定端口号
指定包的长度
\ # tcpdump -nn -c 长度值
将抓取到的数据存储到指定文件-w
\ # tcpdump -nn -w /tmp/1.cap
[root@localhost ~]# tcpdump -nn -i ens33 -w /tmp/1.cap tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes ^C6 packets captured 7 packets received by filter 0 packets dropped by kernel
1.cap文件是不能直接cat的,不然会乱码,由于这个文件是从服务器网卡中捕获的数据包,就是咱们真正通讯的数据,若是想查看这个文件,使用命令# tcpdump -r /tmp/1.cap
[root@localhost ~]# tcpdump -r /tmp/1.cap reading from file /tmp/1.cap, link-type EN10MB (Ethernet) 22:26:18.404881 IP localhost.localdomain.ssh > 192.168.66.1.55443: Flags [P.], seq 831942262:831942410, ack 1313270563, win 296, length 148 22:26:18.405084 IP 192.168.66.1.55443 > localhost.localdomain.ssh: Flags [.], ack 148, win 2053, length 0 22:26:18.683738 ARP, Request who-has gateway tell 192.168.66.1, length 46 22:26:19.395351 ARP, Request who-has gateway tell 192.168.66.1, length 46 22:26:20.394756 ARP, Request who-has gateway tell 192.168.66.1, length 46 22:26:21.684527 ARP, Request who-has gateway tell 192.168.66.1, length 46
tshark命令
先要安装# yum install wireshark 包
wireshark工具和tcpdump工具相似,wireshark就有一个tshak
使用下述命令能够查看80端口web的访问状况
\ # tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"
[root@izwz97d3catjonpf5xybg0z ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri" tshark: -R without -2 is deprecated. For single-pass filtering use -Y. Running as user "root" and group "root". This could be dangerous. Capturing on 'eth0' "Jun 10, 2018 22:31:37.137837401 CST" 172.18.158.103 POST /agent/metrics/putLines
一、使用# ifconfig查看网卡ip
若是没有命令须要安装对应的包# yum install net-tools
# ifconfig
[root@izwz97d3catjonpf5xybg0z ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.158.103 netmask 255.255.240.0 broadcast 172.18.159.255 ether 00:16:3e:0e:82:2c txqueuelen 1000 (Ethernet) RX packets 3686075 bytes 766773356 (731.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3150028 bytes 890353393 (849.1 MiB) 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 loop txqueuelen 1 (Local Loopback) RX packets 692866 bytes 42992964 (41.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 692866 bytes 42992964 (41.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
二、 ifconfig -a
选项-a表示查看全部的网卡,包括被down掉的网卡或者没有ip的网卡
三、 ifdown 网卡名 && ifup 网卡名
开关闭卡后当即开启网卡,以防止主机断网而致使远程登陆终端链接不到主机
[root@localhost ~]# ifdown ens33 && ifup ens33 成功断开设备 'ens33'。 链接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
四、查看网卡是否链接
mii-tool 网卡名字
[root@localhost ~]# mii-tool ens33 ens33: negotiated 1000baseT-FD flow-control, link ok
link ok说明链接正常
或者另外一个命令查看# ethtool 网卡名字
[root@localhost ~]# ethtool ens33 Settings for ens33: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 0 Transceiver: internal Auto-negotiation: on MDI-X: off (auto) Supports Wake-on: d Wake-on: d Current message level: 0x00000007 (7) drv probe link Link detected: yes
五、更改主机名
# hostnamectl set-hostname 新主机名
[root@localhost ~]# hostnamectl set-hostname luo [root@localhost ~]# bash [root@luo ~]#
退出当前终端后从新登陆或者从新打开一个新的bash便可看到新的主机名
要想永久生效,编辑配置文件/etc/hostname
六、DNS配置文件/etc/resolv.conf
# Generated by NetworkManager nameserver 114.114.114.114 nameserver 8.8.8.8
七、/etc/hosts文件
这个文件用来定义域名指向到哪一个ip
[root@localhost ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
八、其余一些扩展的知识
ip命令使用