10.6 监控io性能
监控系统状态
查看磁盘使用情况
- 在运维工作中,除了查看CPU和内存之外,磁盘的io也是非常重要的一个指标
- 有时候,CPU和内存有内存,但系统负载就是很高,我们用vmstat命令查看到 b 列或者是 wa 列 较大,这时候就说明磁盘有瓶颈,那么我们就需要更详细的查看磁盘的状态
iostat命令
- iostat命令,在查看系统输入输出设备和CPU的使用情况
- 在安装 sysstat 这个包的时候,就会安装上iostat命令
- iosta t和 sar 属于同一个包——>yum install -y sysstat
- iostat 1 每秒查看磁盘读写速率
[root@localhost ~]# iostat
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain) 2017年11月28日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.02 0.00 0.11 0.06 0.00 99.81
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.40 6.51 3.33 139818 71433
scd0 0.00 0.00 0.00 44 0
[root@localhost ~]# iostat 1 //每秒查看磁盘读写速率
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain) 2017年11月28日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.02 0.00 0.11 0.06 0.00 99.81
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.40 6.51 3.41 139818 73217
scd0 0.00 0.00 0.00 44 0
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
scd0 0.00 0.00 0.00 0 0
^C //按ctrl+c快捷键退出
[root@localhost ~]#
iostat命令参数-x
- iostat -x 1
- 关注其中一个重要指标,就是%util 列
- 它是一个百分比,这一列表示io等待,就是说你这个磁盘有多少时间占用你CPU的
- 这个CPU有一部分是留给进程处理计算的,还有一部分时间是等待io的,等待磁盘读写的,这个就是一个百分比
- 若这个占比达到百分之五六十,就说明这个磁盘io处理太差了(非常的忙碌)。数字很大的情况下,那么读写的数据也会很大。
- 若是读写数据并不大,就是%util占比比较大,那么说明硬盘有着一些问题、故障的存在。
- 硬盘很慢的话,则必然会影响系统性能。那么即使CPU运行速度再快,硬盘跟不上,那么始终会存在瓶颈。
- 按快捷键ctrl+c 退出查看
[root@localhost ~]# iostat -x 1
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain) 2017年11月28日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.04 0.00 0.12 0.05 0.00 99.79
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.02 0.01 0.26 0.12 6.21 3.24 49.21 0.00 12.12 6.39 24.71 2.89 0.11
scd0 0.00 0.00 0.00 0.00 0.00 0.00 8.00 0.00 23.45 23.45 0.00 23.45 0.00
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
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.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
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.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
^C //快捷键ctrl+c退出查看
[root@localhost ~]#
iotop命令
- 在发现磁盘io很忙、很频繁的时候,要想知道是哪一个进程在频繁的读写呢?
- 安装iotop包——>yum install -y iotop
[root@localhost ~]# yum install -y iotop
- iotop命令,是一个用来监视磁盘I/O使用状况的top类工具。
- 动态显示
- iotop具有与top相似的UI,其中包括PID、用户、I/O、进程等相关信息。
- 按 q键 ,退出查看
10.7 free命令
监控系统状态
- free 查看内存使用情况
- free -m / -g / -h
- buffer/cache区别
- 公式:total=used+free+buff/cache
- avaliable包含free和buffer/cache剩余部分
free命令
- free命令,查看内存使用情况
- 在centos7和centos6中显示的结果是不同的
- 在centos7中,则更加直观
- 默认单位:kb
- 共有三行,我们需要关注的是第二行,内存的使用情况
- 第一行,是说明
- 第二行,是内存的使用情况
- 第三行,是swap交换分区的使用情况
[root@localhost ~]# free
total used free shared buffers cached
Mem: 1010860 354592 656268 6824 692 192560
-/+ buffers/cache: 161340 849520
Swap: 2097148 0 2097148
[root@localhost ~]#
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 987 347 639 6 0 188
-/+ buffers/cache: 158 828
Swap: 2047 0 2047
[root@localhost ~]#
- free -h 在具体的数字后面加上单位——>更加直观显示
[root@localhost ~]# free -h
total used free shared buffers cached
Mem: 987M 347M 639M 6.7M 692K 188M
-/+ buffers/cache: 158M 828M
Swap: 2.0G 0B 2.0G
[root@localhost ~]#
- 第一列,是总共有多大
- 第二列,使用了多大
- 第三列,剩余多大
- 在这里会发现,used+free不等于total
- 这是因为在linux操作系统,会把你的内存预分配一部分出来,分配给 buff 和 cache ,
- 第四列,buff 表示 缓冲
- 第五列,cache 表示 缓存
- 区分:数据的流向不同
- 磁盘——>内存(cache)——>cpu
- cpu——>内存(buff)——>磁盘
- 第六列,是free内存 + 预分配的buff/cache内存还没用完的,就是available(在centos7.0版本中,不会显示available,版本太低)
小常识
- 公式:total=used+free+buff/cache
- avaliable包含free和buffer/cache剩余部分
swap部分
- 当swap不够使用了,就会使用到used,再used跑满了,free剩余为0,那swap则肯定需要添加了
- 然而添加swap并不是解决问题的方法,而是增加内存。swap不够使用了,说明内存不够了,或者说你的内存泄漏了,你的程序有BUG,需要去排查
10.8 ps命令
监控系统状态
- ps 查看系统进程
- STAT部分说明
- D 不能中断的进程
- R run状态的进程
- S sleep状态的进程
- T 暂停的进程
- Z 僵尸进程
- < 高优先级进程
- N 低优先级进程
- L 内存中被锁了内存分页
- s 主进程
- l 多线程进程
- 字符+ 前台进程
ps命令
- ps命令,汇报当前系统的进程状态
- ps用法(常用两种方法)
- ps aux 会把系统中所有的进程给列出来,静态的显示出来
[root@localhost ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 50772 7156 ? Ss 15:49 0:01 /usr/lib/systemd/system
root 2 0.0 0.0 0 0 ? S 15:49 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 15:49 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 15:49 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 15:49 0:00 [kworker/u128:0]
root 7 0.0 0.0 0 0 ? S 15:49 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 15:49 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/0]
root 10 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/1]
root 11 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/2]
root 12 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/3]
root 13 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/4]
root 14 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/5]
root 15 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/6]
root 16 0.0 0.0 0 0 ? S 15:49 0:00 [rcuob/7]
等等等,只截取了一部分——>这里显示出了所有的进程
对比
- top命令,动态显示,把cpu的、内存的做一个排行榜出来
- ps命令,是静态的显示,一次性把所有的状况列出来
ps命令 查看系统所有进程(两种)
- ps aux 查看系统所有进程
- ps -elf 显示出的效果和ps aux基本类似
[root@localhost ~]# ps aux //查看系统所有进程
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 50772 7156 ? Ss 11月28 0:01 /usr/lib/systemd/syst
root 2 0.0 0.0 0 0 ? S 11月28 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 11月28 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 11月28 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 11月28 0:00 [kworker/u128:0]
root 7 0.0 0.0 0 0 ? S 11月28 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 11月28 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S 11月28 0:00 [rcuob/0]
root 10 0.0 0.0 0 0 ? S 11月28 0:00 [rcuob/1]
root 11 0.0 0.0 0 0 ? S 11月28 0:00 [rcuob/2]
等等等,只截取了一部分——>这里显示出了所有的进程
[root@localhost ~]# ps -elf //查看系统所有进程
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 12693 ep_pol 11月28 ? 00:00:01 /usr/lib/syst
1 S root 2 0 0 80 0 - 0 kthrea 11月28 ? 00:00:00 [kthreadd]
1 S root 3 2 0 80 0 - 0 smpboo 11月28 ? 00:00:00 [ksoftirqd/0]
1 S root 5 2 0 60 -20 - 0 worker 11月28 ? 00:00:00 [kworker/0:0H
1 S root 6 2 0 80 0 - 0 worker 11月28 ? 00:00:00 [kworker/u128
1 S root 7 2 0 -40 - - 0 smpboo 11月28 ? 00:00:00 [migration/0]
1 S root 8 2 0 80 0 - 0 rcu_gp 11月28 ? 00:00:00 [rcu_bh]
1 S root 9 2 0 80 0 - 0 rcu_no 11月28 ? 00:00:00 [rcuob/0]
1 S root 10 2 0 80 0 - 0 rcu_no 11月28 ? 00:00:00 [rcuob/1]
等等等,只截取了一部分——>这里显示出了所有的进程
- 第一列,显示出进程的运行用户
- 第二列,进程的PID
- 在杀死一个进程的时候,就回需要使用PID,命令 kill PID号
- 例子:kill 1346
- 然后ps aux |grep qmgr 会看到的该进程被杀掉了
- 在系统被黑了,遇到一个不知道的进程(有可能是被入侵的进程),在杀死该进程的时候,就需要先知道PID号
- 先查看PID号,进程在哪里启动起来的
- ls -l /proc/PID号 ,会看到进程启动的目录
- 第三列,CPU百分比
- 第四列,内存百分比
- 第五列,虚拟内存
- 第六列,物理内存
- 第七列,是在哪一个TTY上
- 第八列,STAT,表示进程的状态,是我们需要关注的一列!
- STAT有这几种状态
- D 不能中断的进程 (会直接影响CPU资源)
- R run状态的进程(正在跑的进程,表示某一个时间段内,使用的CPU)
- S sleep状态的进程
- T 暂停的进程
- Z 僵尸进程
- 字符< 高优先级进程
- N 低优先级进程
- L 内存中被锁了内存分页
- s 主进程
- l 多线程进程
- 字符+ 前台进程
- 第九列,什么时候启动的
- 第十列,TIME,运行了多久了
- 第十一列,命令
线程和进程最大的区别:
1 进程里包含了线程,线程是进程的子单元
2 同一个进程下的线程全部共享相同的内存,而进程之间内存相互隔离。
ps命令查看进程是否在存在
- ps aux |grep nginx //查看nginx进程是否在存在
- ps aux |grep mysql //查看mysql进程是否存在
[root@localhost ~]# ps aux |grep nginx //查看nginx进程是否在存在
root 2619 0.0 0.0 112656 984 pts/0 R+ 00:00 0:00 grep --color=auto ngin
[root@localhost ~]# ps aux |grep mysql //查看mysql进程是否存在
root 2643 0.0 0.0 112656 984 pts/0 R+ 00:02 0:00 grep --color=auto mysq
[root@localhost ~]#
重点部分 STAT
10.9 查看网络状态
监控系统状态
- netstat 查看网络状态
- netstat -lnp 查看监听端口
- netstat -an 查看系统的网络连接状况
- netstat -lntp 只看出tcp的,不包含socket
- ss -an和netstat异曲同工
- 技巧:
- netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t" ,sta[key]}'
netstat命令
- netstat命令,查看linux中网络系统的状态信息
netstat命令,查看监听端口
- netstat -lnp 查看监听端口
- l 就是监听的意思
- tcp
- 25端口,是发邮件的端口
- 22 端口,有两个,一个是tcp,另一个是tcp6
- udp
- UNIX中有一种文件叫socket
- 也是进程间相互通信的(前提:同一台服务器,两个进程相互通信,使用socket文件)
[root@hf-01 ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1522/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1178/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1522/master
tcp6 0 0 :::3306 :::* LISTEN 1525/mysqld
tcp6 0 0 :::22 :::* LISTEN 1178/sshd
udp 0 0 0.0.0.0:51407 0.0.0.0:* 557/avahi-daemon: r
udp 0 0 0.0.0.0:5353 0.0.0.0:* 557/avahi-daemon: r
raw6 0 0 :::58 :::* 7 650/NetworkManager
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 18892 1522/master private/tlsmgr
unix 2 [ ACC ] STREAM LISTENING 18880 1522/master public/pickup
unix 2 [ ACC ] STREAM LISTENING 18895 1522/master private/rewrite
unix 2 [ ACC ] STREAM LISTENING 18898 1522/master private/bounce
unix 2 [ ACC ] STREAM LISTENING 18901 1522/master private/defer
unix 2 [ ACC ] STREAM LISTENING 18904 1522/master private/trace
unix 2 [ ACC ] STREAM LISTENING 18907 1522/master private/verify
unix 2 [ ACC ] STREAM LISTENING 18913 1522/master private/proxymap
unix 2 [ ACC ] STREAM LISTENING 18916 1522/master private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 18919 1522/master private/smtp
unix 2 [ ACC ] STREAM LISTENING 18922 1522/master private/relay
unix 2 [ ACC ] STREAM LISTENING 18928 1522/master private/error
unix 2 [ ACC ] STREAM LISTENING 18931 1522/master private/retry
unix 2 [ ACC ] STREAM LISTENING 18934 1522/master private/discard
unix 2 [ ACC ] STREAM LISTENING 18937 1522/master private/local
unix 2 [ ACC ] STREAM LISTENING 11300 1/systemd /run/lvm/lvmpolld.socket
unix 2 [ ACC ] STREAM LISTENING 18940 1522/master private/virtual
unix 2 [ ACC ] STREAM LISTENING 18943 1522/master private/lmtp
unix 2 [ ACC ] STREAM LISTENING 18946 1522/master private/anvil
unix 2 [ ACC ] STREAM LISTENING 18949 1522/master private/scache
unix 2 [ ACC ] STREAM LISTENING 6719 1/systemd /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 11092 1/systemd /run/lvm/lvmetad.socket
unix 2 [ ACC ] STREAM LISTENING 15756 650/NetworkManager /var/run/NetworkManager/private-dhcp
unix 2 [ ACC ] STREAM LISTENING 13965 1/systemd /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 18884 1522/master public/cleanup
unix 2 [ ACC ] STREAM LISTENING 13979 1/systemd /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 18887 1522/master public/qmgr
unix 2 [ ACC ] STREAM LISTENING 18910 1522/master public/flush
unix 2 [ ACC ] STREAM LISTENING 18925 1522/master public/showq
unix 2 [ ACC ] STREAM LISTENING 19266 1525/mysqld /tmp/mysql.sock
unix 2 [ ACC ] SEQPACKET LISTENING 11220 1/systemd /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 15580 650/NetworkManager /var/run/NetworkManager/private
unix 2 [ ACC ] STREAM LISTENING 10994 1/systemd /run/systemd/private
[root@hf-01 ~]#
netstat命令 查看所有的连接状态
[root@hf-01 ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 52 192.168.74.129:22 192.168.74.1:49322 ESTABLISHED
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
udp 0 0 0.0.0.0:51407 0.0.0.0:*
udp 0 0 0.0.0.0:5353 0.0.0.0:*
raw6 0 0 :::58 :::* 7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 18892 private/tlsmgr
unix 2 [ ACC ] STREAM LISTENING 18880 public/pickup
unix 2 [ ACC ] STREAM LISTENING 18895 private/rewrite
unix 2 [ ACC ] STREAM LISTENING 18898 private/bounce
等等等,只截取了一部分
netstat命令 只查看tcp
[root@hf-01 ~]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1522/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1178/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1522/master
tcp6 0 0 :::3306 :::* LISTEN 1525/mysqld
tcp6 0 0 :::22 :::* LISTEN 1178/sshd
[root@hf-01 ~]#
netstat命令 只查看tcp和udp
- netstat -ltunp //只查看tcp和udp
[root@hf-01 ~]# netstat -ltunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1522/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1178/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1522/master
tcp6 0 0 :::3306 :::* LISTEN 1525/mysqld
tcp6 0 0 :::22 :::* LISTEN 1178/sshd
udp 0 0 0.0.0.0:51407 0.0.0.0:* 557/avahi-daemon: r
udp 0 0 0.0.0.0:5353 0.0.0.0:* 557/avahi-daemon: r
[root@hf-01 ~]#
netstat命令和awk相关的
- 查看所有状态的数字
- netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t" ,sta[key]}'
[root@hf-01 ~]# netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t"
LISTEN 5
ESTABLISHED 1
[root@hf-01 ~]#
- 查看netstat需要关注一个值,就是ESTABLISHED,这个数字若是很大,说明系统很忙
- 通常ESTABLISHED 这个数字在一千以内,服务器都是可以接受的
- 网站并发连接数,并发连接数就是指同一时刻有多少个客户端在连接你,而我们就可以拿ESTABLISHED 这个数字去说明,并发连接数有多少
ss -an命令和netstat命令相似
[root@hf-01 ~]# ss -an
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
nl UNCONN 0 0 0:4194954 *
nl UNCONN 0 0 0:650 *
nl UNCONN 0 0 0:557 *
nl UNCONN 0 0 0:0 *
nl UNCONN 4352 0 4:2922 *
nl UNCONN 768 0 4:0 *
nl UNCONN 0 0 6:0 *
nl UNCONN 0 0 7:554 *
nl UNCONN 0 0 7:1 *
nl UNCONN 0 0 7:0 *
nl UNCONN 0 0 9:0 *
nl UNCONN 0 0 9:514 *
nl UNCONN 0 0 9:1 *
nl UNCONN 0 0 10:0 *
nl UNCONN 0 0 11:0 *
nl UNCONN 0 0 15:650 *
等等等,只截取了一部分
[root@hf-01 ~]# ss -an |grep -i listen //因为listen是大写,所以加-i 参数
u_str LISTEN 0 100 private/tlsmgr 18892 * 0
u_str LISTEN 0 100 public/pickup 18880 * 0
u_str LISTEN 0 100 private/rewrite 18895 * 0
u_str LISTEN 0 100 private/bounce 18898 * 0
u_str LISTEN 0 100 private/defer 18901 * 0
u_str LISTEN 0 100 private/trace 18904 * 0
u_str LISTEN 0 100 private/verify 18907 * 0
u_str LISTEN 0 100 private/proxymap 18913 * 0
u_str LISTEN 0 100 private/proxywrite 18916 * 0
等等等,只截取了一部分
10.10 linux下抓包
监控系统状态
- 抓包工具 tcpdump
- 用法:tcpdump -nn
- tcpdump -nn -i ens33
- tcpdump -nn port 80
- tcpdump -nn not port 22 and host 192.168.0.100
- tcpdump -nn -c 100 -w 1.cap
- 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"
- yum install -y wireshark
tcpdump命令
- tcpdump命令,是用来抓包的
- 安装tcpdump包——>yum install -y tcpdump
- tcp -nn -i eno16777736
- 第一个n,表示你的ip用数字的形式显示出来(若是不加,则会显示出主机名)
[root@hf-01 ~]# tcpdump -nn
tcpdump: packet printing is not supported for link type NFLOG: use -w
[root@hf-01 ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.129 netmask 255.255.255.0 broadcast 192.168.74.255
inet6 fe80::20c:29ff:feff:fe93 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ff:fe:93 txqueuelen 1000 (Ethernet)
RX packets 11067 bytes 12250186 (11.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2631 bytes 312427 (305.1 KiB)
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 0 (Local Loopback)
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
[root@hf-01 ~]# tcpdump -nn -i eno16777736
5204, ack 6605, win 159, length 180
06:27:41.186368 IP 192.168.74.129.22 > 192.168.74.1.49322: Flags [P.], seq 45955204:45955384, ack 6605, win 159, length 180
06:27:41.186422 IP 192.168.74.129.22 > 192.168.74.1.49322: Flags [P.], seq 45955384:45955564, ack 6605, win 159, length 180
等等等,运行后,会显示出很多的信息
按ctrl+c退出
- DDos攻击
- udp flood叫udp的洪水攻击
- 在遇到这种攻击的时候,只能接入防攻击的设备(比如:创宇--300G是可以顶住的)
tcpdump命令指定端口
- tcpdump -nn -i eno16777736 port 22
[root@hf-01 ~]# tcpdump -nn -i eno16777736 port 22
4, length 0
06:52:04.034073 IP 192.168.74.129.22 > 192.168.74.1.49322: Flags [P.], seq 228004:228376, ack 1, win 159, length 372
06:52:04.034155 IP 192.168.74.129.22 > 192.168.74.1.49322: Flags [P.], seq 228376:228556, ack 1, win 159, length 180
06:52:04.034302 IP 192.168.74.1.49322 > 192.168.74.129.22: Flags [.], ack 228556, win 252, length 0
06:52:04.034387 IP 192.168.74.129.22 > 192.168.74.1.49322: Flags [P.], seq 228556:228832, ack 1, win 159, length 276
等等等,只截取了一部分
tcpdump命令使用排除法
- tcpdump -nn not port 22 and host 192.168.0.100 除去22端口和只要ip为192.168.0.100的包
tcpdump命令指定长度和个数
- tcpdump -nn -i eno16777736 -c 100 -w 1.cap 指定数据包只要10个,并存入到1.cap文件中去
- 在终端查看的时候,不产生一些数据量的话,就不会产生那么多数据包
- file 1.cap 查看该文件——>文件名称可以随意更改
- 能查看文件的大概信息
- 该文件不能直接 cat命令 查看的,会出现乱码
- 因为1.cap这个文件就是从你网卡里捕捉到的数据包,就是你通信的那个数据
在这里复制另一个终端,并打开,跑vmstat 1命令
在当前终端查看,输入以下命令
[root@hf-01 ~]# tcpdump -nn -i eno16777736 -c 10 -w 1.cap
tcpdump: listening on eno16777736, link-type EN10MB (Ethernet), capture size 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kernel
[root@hf-01 ~]# file 1.cap
1.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)
[root@hf-01 ~]#
- 若要查看这个文件,则可以tcpdump -r 1.cap
[root@hf-01 ~]# tcpdump -r 1.cap //查看1.cap文件
reading from file 1.cap, link-type EN10MB (Ethernet)
07:02:05.134302 IP 192.168.74.129.ssh > 192.168.74.1.49322: Flags [P.], seq 4218852344:4218852492, ack 3076966380, win 159, length 148
07:02:05.134859 IP 192.168.74.1.49322 > 192.168.74.129.ssh: Flags [.], ack 148, win 255, length 0
07:02:05.257655 IP 192.168.74.129.ssh > 192.168.74.1.49322: Flags [P.], seq 148:280, ack 1, win 159, length 132
07:02:05.462113 IP 192.168.74.1.49322 > 192.168.74.129.ssh: Flags [.], ack 280, win 255, length 0
07:02:06.258704 IP 192.168.74.129.ssh > 192.168.74.1.49322: Flags [P.], seq 280:428, ack 1, win 159, length 148
07:02:06.462164 IP 192.168.74.1.49322 > 192.168.74.129.ssh: Flags [.], ack 428, win 254, length 0
07:02:07.259714 IP 192.168.74.129.ssh > 192.168.74.1.49322: Flags [P.], seq 428:560, ack 1, win 159, length 132
07:02:07.460218 IP 192.168.74.1.49322 > 192.168.74.129.ssh: Flags [.], ack 560, win 254, length 0
07:02:08.260717 IP 192.168.74.129.ssh > 192.168.74.1.49322: Flags [P.], seq 560:708, ack 1, win 159, length 148
07:02:08.460263 IP 192.168.74.1.49322 > 192.168.74.129.ssh: Flags [.], ack 708, win 253, length 0
[root@hf-01 ~]#
- 在read的返回的时候,是数据流,从哪到哪(包括tcpdump -nn屏幕上显示的也是数据流)
- 而tcpdump -r的时候,写入的是真正的数据包
tshark命令
- tshark命令
- 安装tshark包——>yum install -y wireshark
- tshark和tcpdump类似的工具,也是用来抓包的
- 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" 查看指定网卡,80端口的一个web访问情况
扩展