lspci | grep -i eth #查询网卡信息,可查询网卡数量,型号等
ethtool ethX #查询eth*网口基本信息
ethtool –i ethX #查询ethX网口的相关信息
ethtool –d ethX #查询ethX网口注册性信息
ethtool –S ethX #查询ethX网口收发包统计
dmesg | grep -i eth
//更改网卡名:前提网卡down掉且onboot=yes
ip link set [$现在的名字] name [$预期的名字] #重启会失效
//查询网口对应的交换机口
tcpdump -i eth0 -c 1 -n -e -v ether proto 0x88cc 2>/dev/null | egrep 'Subtype Interface Name|System Name'|awk -F':' '{print $2}'
参数选项 | 解释说明 |
---|---|
-b | 以Byte为单位显示内存的使用情况 |
-m | 以MB为单位显示内存的使用情况@ |
-K | 以KB为单位显示内存的使用情况 |
-h | 以人类可读的形式显示内存的使用情况@ |
-t | 显示内存总和列 |
-s <间隔秒数> | 根据指定的间隔秒数持续显示内存的使用情况@ |
-o | 不显示系统缓冲区列 |
(1)查看系统内存
[root@nginx ~]# free #不加参数默认显示的是字节数,很难读懂
total used free shared buffers cached
Mem: 1004412 85788 918624 0 6168 22904
-/+ buffers/cache: 56716 947696
Swap: 2031608 0 2031608
[root@nginx ~]# free -m #使用-m选项,以MB为单位显示内存的使用情况
total used free shared buffers cached
Mem: 980 83 897 0 6 22
-/+ buffers/cache: 55 925
Swap: 1983 0 1983
(2)定时查询内存
[root@nginx ~]# free -s 10 #使用-s选项定时刷新内存的使用情况,单位为秒
total used free shared buffers cached
Mem: 1004412 85888 918524 0 6168 22932
-/+ buffers/cache: 56788 947624
Swap: 2031608 0 2031608
(3)检查使用超过100m的进程
cat /proc/slabinfo |awk '{if($3*$4/1024/1024 > 100){print $1,$3*$4/1024/1024} }'
参数选项 | 解释说明 |
---|---|
-a | 显示活跃和非活跃内存 |
-f | 显示从系统启动至今的fork进程数量 |
-m | 显示slab信息 |
-n | 只在开始时显示一次各字段名称 |
-s | 显示内存相关统计信息及多种系统活动数量@ |
-d | 显示磁盘相关统计信息 |
-p | 显示指定磁盘分区统计信息 |
-S | 使用指定单位显示。参数有k,K,m,M,分别代表1000,1024,1000000,1048576字节(byte)。默认单位为K(1024byte) |
-t | 统计信息带上时间戳 |
[root@nginx ~]# vmstat #如果省略“间隔时间”和“次数”的参数,则仅显示一次报告后就退出
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 915796 8792 22900 0 0 1188 54 203 474 2 6 90 1 0
[root@nginx ~]# vmstat 5 #表示每5秒钟更新一次输出信息,循环输出,按Ctrl + C组合键停止输出
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 915796 8792 22928 0 0 343 24 64 144 1 2 97 0 0
[root@nginx ~]# vmstat 5 6 #表示每5秒钟更新一次输出信息,统计6次后停止输出
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 915796 8792 22928 0 0 781 53 137 316 1 4 94 1 0
[root@nginx ~]# vmstat -a 2 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free inact active si so bi bo in cs us sy id wa st
0 0 0 915324 19284 18180 0 0 5 0 6 9 0 0 100 0 0
0 0 0 915284 19284 18180 0 0 0 0 10 9 0 0 100 0 0
0 0 0 915284 19284 18180 0 0 0 0 10 10 0 0 100 0 0
0 0 0 915284 19284 18212 0 0 0 0 9 10 0 0 100 0 0
0 0 0 915284 19284 18212 0 0 0 0 9 11 0 0 100 0 0
[root@nginx ~]# vmstat -s
1004412 total memory
89096 used memory
18196 active memory
19268 inactive memory
915316 free memory
8816 buffer memory
23244 swap cache
2031608 total swap
0 used swap
2031608 free swap
......
这些信息分别来自于/proc/meminfo,/proc/stat和/proc/vmstat
[root@nginx ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
ram0 0 0 0 0 0 0 0 0 0 0
ram1 0 0 0 0 0 0 0 0 0 0
ram2 0 0 0 0 0 0 0 0 0 0
ram3 0 0 0 0 0 0 0 0 0 0
ram4 0 0 0 0 0 0 0 0 0 0
ram5 0 0 0 0 0 0 0 0 0 0
ram6 0 0 0 0 0 0 0 0 0 0
ram7 0 0 0 0 0 0 0 0 0 0
ram8 0 0 0 0 0 0 0 0 0 0
ram9 0 0 0 0 0 0 0 0 0 0
ram10 0 0 0 0 0 0 0 0 0 0
ram11 0 0 0 0 0 0 0 0 0 0
ram12 0 0 0 0 0 0 0 0 0 0
ram13 0 0 0 0 0 0 0 0 0 0
ram14 0 0 0 0 0 0 0 0 0 0
ram15 0 0 0 0 0 0 0 0 0 0
loop0 0 0 0 0 0 0 0 0 0 0
loop1 0 0 0 0 0 0 0 0 0 0
loop2 0 0 0 0 0 0 0 0 0 0
loop3 0 0 0 0 0 0 0 0 0 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
loop4 0 0 0 0 0 0 0 0 0 0
loop5 0 0 0 0 0 0 0 0 0 0
loop6 0 0 0 0 0 0 0 0 0 0
loop7 0 0 0 0 0 0 0 0 0 0
sr0 33 0 264 47 0 0 0 0 0 0
sda 2144 1571 70016 659 411 452 6898 277 0 0
dm-0 2786 0 62426 1755 854 0 6832 583 0 0
dm-1 287 0 2296 35 0 0 0 0 0 0
这些信息主要来自于/proc/diskstats。其中的merged表示一次来自于合并的写/读请求,系统一般会把多个连接/邻近的读/写请求合并到一起来操作。
(5)查看/dev/sda1磁盘的读写统计信息
[root@nginx ~]# vmstat -p /dev/sda1
sda1 reads read sectors writes requested writes
431 3518 9 66
lscpu | grep -i numa #查询numa开关,1为关闭
dmesg | grep -i numa #同上
//检查超线程的开关
grep 'siblings' /proc/cpuinfo
grep 'cpu cores' /proc/cpuinfo
如果“siblings”和“cpu cores”一致,则说明不支持超线程,或者超线程未打开。
如果“siblings”是“cpu cores”的两倍,则说明支持超线程,并且超线程已打开。
chronyc -n sources -v
cat /etc/ntp.conf
storcli64 /c0 show #查看磁盘模式与raid
storcli64 /c0/v0 delete #清空raid组
storcli64 /c0 set jbod=on #设置jbod
storcli64 /c0/eall/sall show #显示控制器为0的所有物理磁盘信息
//设置raid模式 = ‘8:4-1’根据环境情况而定
storcli64 /c0 add vd type=raid1 size=all names=t1 drives=8:4-1
//过滤大文件
find . -type f -size +200M -print | xargs ls -lh
//权限
chmod 755 test #将文件设置为755权限
chown admin:admin test #将文件的属组属主设置为admin
e2label /dev/sda3 #查看磁盘标签
lsblk #查看磁盘大小,分区,挂载
df -hT #查看磁盘大小,分区,挂载,格式,使用量
参数选项 | 解释说明 |
---|---|
-c | 显示CPU的使用情况@ |
-d | 显示磁盘的使用情况@ |
-k | 每秒以kB为单位显示数据 |
-m | 每秒以MB为单位显示数据 |
-n | 显示NFS的使用情况 |
-t | 显示每次统计的执行时间 |
-p device | 指定要统计的磁盘设备名称,默认为所有的磁盘设备 |
-x | 显示扩展统计 |
[root@nginx ~]# iostat #如果省略“时间间隔”和“次数”参数,则仅显示一次报告后就退出
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.02 0.00 0.05 0.01 0.00 99.93
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.02 0.13 0.00 1976 0
sda 0.34 28.21 7.76 445866 122724
dm-0 1.30 27.73 7.76 438306 122664
dm-1 0.02 0.15 0.00 2296 0
[root@nginx ~]# iostat 2 3 #每隔2秒刷新显示一次,共显示3次
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.01 0.00 0.04 0.00 0.00 99.94
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.02 0.09 0.00 1976 0
sda 0.25 19.82 5.51 445866 123988
dm-0 0.92 19.48 5.51 438306 123928
dm-1 0.01 0.10 0.00 2296 0
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
dm-0 0.00 0.00 0.00 0 0
dm-1 0.00 0.00 0.00 0 0
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.50 0.00 0.00 99.50
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
dm-0 0.00 0.00 0.00 0 0
dm-1 0.00 0.00 0.00 0 0
[root@nginx ~]# iostat -d #选项-d只显示磁盘的统计信息
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.02 0.09 0.00 1976 0
sda 0.24 19.50 5.43 445866 124108
dm-0 0.91 19.17 5.42 438306 124048
dm-1 0.01 0.10 0.00 2296 0
[root@nginx ~]# iostat -d -k #选项-k以KB为单位显示数据
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.02 0.04 0.00 988 0
sda 0.24 9.72 2.70 222933 62054
dm-0 0.91 9.55 2.70 219153 62024
dm-1 0.01 0.05 0.00 1148 0
[root@nginx ~]# iostat -d -m #选项-m以MB为单位显示数据
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
scd0 0.02 0.00 0.00 0 0
sda 0.24 0.01 0.00 217 60
dm-0 0.90 0.01 0.00 214 60
dm-1 0.01 0.00 0.00 1 0
[root@nginx ~]# iostat -d -x -k #选项-x显示扩展信息
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.02 0.00 0.04 0.00 5.41 0.00 1.09 1.09 0.00
sda 0.08 0.61 0.18 0.05 9.49 2.65 102.62 0.00 0.79 0.42 0.01
dm-0 0.00 0.00 0.22 0.66 9.33 2.65 27.03 0.01 6.17 0.11 0.01
dm-1 0.00 0.00 0.01 0.00 0.05 0.00 8.00 0.00 0.24 0.13 0.00
[root@nginx ~]# iostat -c #使用-c选项只显示系统CPU的统计信息
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.01 0.00 0.04 0.00 0.00 99.95
参数选项 | 解释说明 |
---|---|
-o | 显示正在使用I/O的进程或者线程,默认是显示所有@ |
-d | 设置显示的间隔秒数 |
-p | 只显示指定PID的信息@ |
-u | 显示指定用户的信息 |
-P | 只显示进程,一般是显示所有的线程 |
-a | 显示从iotop启动后每个线程完成了的I/O总数 |
-k | 设置显示单位为KB |
-t | 在每一行前添加一个当前的时间 |
[root@nginx ~]# iotop
Total DISK READ: 0.00 B/s | Total 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 % init
2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd]
3 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
4 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0]
5 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
6 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/0]
7 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [events/0]
8 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [cgroup]
9 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [khelper]
10 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [netns]
11 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [async/mgr]
12 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [pm]
13 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [sync_supers]
14 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [bdi-default]
15 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kintegrityd/0]
16 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kblockd/0]
17 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kacpid]
18 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kacpi_notify]
19 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kacpi_hotplug]
20 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ata_aux]
21 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ata_sff/0]
22 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksuspend_usbd]
23 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [khubd]
24 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kseriod]
dmidecode |grep "Serial Number"
或
dmidecode -t 1
参数选项 | 解释说明 |
---|---|
-P | 指定CPU编号,例如:-P 0表示第一个CPU,-P 1表示第二个CPU,-P ALL表示所有CPU |
[root@nginx ~]# mpstat #如果省略“时间间隔”和“次数”参数,则仅显示一次报告后就退出
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
06:13:34 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
06:13:34 PM all 0.01 0.00 0.06 0.01 0.00 0.00 0.00 0.00 99.93
[root@nginx ~]# mpstat 5 6 #表示每5秒更新一次输出信息,统计6次后停止输出。
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
06:15:16 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
06:15:21 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
06:15:26 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
06:15:31 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
06:15:36 PM all 0.00 0.00 0.20 0.00 0.00 0.00 0.00 0.00 99.80
06:15:41 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
06:15:46 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.03 0.00 0.00 0.00 0.00 0.00 99.97
说明 :
第1列:06:13:34 PM,表示当前时间
第2列:CPU,all表示所有CPU,0表示第一个CPU…
后面9列的含义分别如下:
[root@nginx ~]# mpstat -P 0 #显示第一个CPU信息
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
06:29:46 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
06:29:46 PM 0 0.01 0.00 0.05 0.01 0.00 0.00 0.00 0.00 99.93
参数选项 | 解释说明 |
---|---|
-A | 显示系统所有资源设备(CPU,内存,磁盘)的运行状态 |
-u | 显示系统所有CPU在采样时间内的负载状态@ |
-P | 显示当前系统中指定CPU的使用情况 |
-d | 显示系统所有硬盘设备在采样时间内的使用状况@ |
-r | 显示在采样时间内系统内存的使用状况@ |
-b | 显示在采样时间内缓冲区的使用情况@ |
-v | 显示索引节点,文件和其他内核表的状态 |
-n | 显示网络运行状态@ |
-q | 显示运行队列的大小,它与系统当时的平均负载相同@ |
-R | 显示进程在采样时间内的活动情况 |
-y | 显示终端设备在采样时间内的活动情况 |
-w | 显示系统交换活动在采样时间内的状态 |
-o filename | 将命令结果以二进制格式存放在文件中,filename是文件名 |
[root@nginx ~]# sar -u 2 3
Linux 2.6.32-431.el6.x86_64 (test) 03/10/2018 _x86_64_ (1 CPU)
11:44:34 PM CPU %user %nice %system %iowait %steal %idle
11:44:36 PM all 0.00 0.00 1.00 0.00 0.00 99.00
11:44:38 PM all 0.00 0.00 0.50 0.00 0.00 99.50
11:44:40 PM all 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.50 0.00 0.00 99.50
[root@nginx ~]# sar -q 2 3 #使用-q选项显示运行队列的大小
Linux 2.6.32-431.el6.x86_64 (localhost) 03/14/2018 _x86_64_ (1 CPU)
04:59:18 PM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
04:59:20 PM 0 69 0.00 0.00 0.00
04:59:22 PM 0 69 0.00 0.00 0.00
04:59:24 PM 0 70 0.00 0.00 0.00
Average: 0 69 0.00 0.00 0.00
[root@nginx ~]# sar -r 2 3 #使用-r选项显示系统内存在采样时间内的使用状况
Linux 2.6.32-431.el6.x86_64 (localhost) 03/14/2018 _x86_64_ (1 CPU)
05:03:44 PM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit
05:03:46 PM 913012 91400 9.10 11136 23324 43344 1.43
05:03:48 PM 913012 91400 9.10 11136 23324 43344 1.43
05:03:50 PM 913012 91400 9.10 11136 23324 43344 1.43
Average: 913012 91400 9.10 11136 23324 43344 1.43
[root@nginx ~]# sar -b 2 3 #使用-b选项显示缓冲区在采样时间内的使用情况
Linux 2.6.32-431.el6.x86_64 (localhost) 03/14/2018 _x86_64_ (1 CPU)
05:15:01 PM tps rtps wtps bread/s bwrtn/s
05:15:03 PM 0.00 0.00 0.00 0.00 0.00
05:15:05 PM 0.00 0.00 0.00 0.00 0.00
05:15:07 PM 0.00 0.00 0.00 0.00 0.00
Average: 0.00 0.00 0.00 0.00 0.00
(1)查看网络接口信息
[root@nginx ~]# sar -n DEV 2 3 #使用-n DEV显示网络接口的信息
Linux 2.6.32-431.el6.x86_64 (localhost) 03/14/2018 _x86_64_ (1 CPU)
05:22:13 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
05:22:15 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:15 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:15 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:15 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
05:22:17 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:17 PM eth0 0.50 0.50 0.03 0.24 0.00 0.00 0.00
05:22:17 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:17 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
05:22:19 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:22:19 PM eth0 0.50 0.50 0.03 0.24 0.00 0.00 0.00
05:22:19 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 0.33 0.33 0.02 0.16 0.00 0.00 0.00
Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
(2) 查看网络错误的统计数据
[root@nginx ~]# sar -n EDEV 2 3 #使用-n EDEV显示网络错误的统计数据
Linux 2.6.32-431.el6.x86_64 (localhost) 03/14/2018 _x86_64_ (1 CPU)
05:32:03 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
05:32:05 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:05 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:05 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:05 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
05:32:07 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:07 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:07 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:07 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
05:32:09 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:09 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:32:09 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
(3)查看套接字信息
[root@nginx ~]# sar -n SOCK 2 3 #使用-n SOCK显示套接字信息
Linux 2.6.32-431.el6.x86_64 (test) 03/14/2018 _x86_64_ (1 CPU)
05:42:12 PM totsck tcpsck udpsck rawsck ip-frag tcp-tw
05:42:14 PM 281 2 0 0 0 0
05:42:16 PM 281 2 0 0 0 0
05:42:18 PM 281 2 0 0 0 0
Average: 281 2 0 0 0 0
[root@nginx ~]# sar -d 2 3 #使用-d选项显示系统所有硬盘设备在采样时间内的使用状况
Linux 2.6.32-431.el6.x86_64 (test) 03/14/2018 _x86_64_ (1 CPU)
05:47:59 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
05:48:01 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:01 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:01 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:01 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:01 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
05:48:03 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:03 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:03 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:03 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:03 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
05:48:05 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:05 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:05 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
05:48:05 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
Average: dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
docker logs [$container_id] #查看容器日志信息
docker exec -it [$container_id] bash #进入容器内
docker rm [$container_id] #删除容器
docker rmi [$container_id] #删除镜像
docker stop(start/restart) [$container_id] #停止(启动/重启)容器
docker save -o [$xxx.tar] [$镜像名称]:[$镜像tag] #打包镜像文件
docker images #查看所有镜像
docker ps (-a) #查看容器(-a可查看已经退出的容器)
docker pull(push) [$镜像名称]:[$镜像tag] #拉取/上传镜像
docker run -it [$container_id] #启动容器
docker cp [$container_id]:[$文件绝对路径] [文件存放路径] #复制文件
docker kill [$container_id] #杀死正在运行的容器
docker stats [$container_id] #显示一个容器的实时资源占用
docker pause [$container_id] #暂停容器内所有进程
docker unpause [$container_id] #恢复容器内被所有暂停的进程
docker logs -f -t --tail=100 [$container_id] #查看容器后百行日志
docker version #查看docker版本
docker info #查看docker系统信息
kubectl get nodes #查询节点
kubectl get pod -A #查询pod,-n可通过命名空间查看对应命名空间内的pod
kubectl get cs
kubectl get ns #查询命名空间
kubectl describe po {pod_name} #查看pod的基础信息
kubectl cluster-info #查看k8s集群的信息
kubectl version #查看版本
kubectl logs {pod_name} -n {namespace} #查看pod日志
kubectl run pod --image={image_name} -n {namespace} #运行pod
kubectl delete pod {pod_name} -n {namespace} #删除pod
screen -dmS test #创建名为test的切屏
screen -x test #进入切屏
ctrl +a+d #退出切屏
screen -ls #查看创建的切屏
//每输入一个字符,终端都会滚动显示历史命令。提示:(reverse-i-search),
Ctrl + r
//移动操作快捷键
ESC + f #向右移动一个单词,MAC下建议用ALT + →
ESC + b #向左移动一个单词,MAC下建议用ALT + ←
Ctrl + a #跳到行首
Ctrl + e #跳到行尾
//删除操作快捷键
Ctrl + d #向右删除一个字符
Ctrl + h #向左删除一个字符
Ctrl + u #删除当前位置字符至行首(输入密码错误的时候多用下这个)
Ctrl + k #删除当前位置字符至行尾
Ctrl + w #删除从光标到当前单词开头
vim编辑方式/vimrc自动加载文件头等操作记录
参数选项 | 解释说明 |
---|---|
-a | 显示所有终端下执行的进程 |
a | 显示与终端相关的所有进程,包含每个进程的完整路径 |
x | 显示与终端无关的所有进程 |
u | 显示进程的用户信息 |
-u | 显示指定用户相关的进程信息 |
-e | 显示所有进程 |
-f | 额外显示UID,PPID,C与STIME栏位 |
f | 显示进程树 |
-H | 显示进程树 |
-l | 以详细的格式来显示进程的状况 |
-o | 自定义输出指定的字段,以逗号分隔 |
-sort key | key表示为指定字段排序,默认升序,+key升序,-key降序 |
[root@test1 ~]# ps
PID TTY TIME CMD
1135 pts/0 00:00:00 bash
1152 pts/0 00:00:00 bash
1162 pts/0 00:00:00 bash
1173 pts/0 00:00:00 bash
1182 pts/0 00:00:00 ps
[root@test1 ~]# ps -ef # -e显示所有进程,-f格外显示UID,PPID,C与STIME栏位
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 15:00 ? 00:00:00 /sbin/init
root 2 0 0 15:00 ? 00:00:00 [kthreadd]
root 3 2 0 15:00 ? 00:00:00 [migration/0]
root 4 2 0 15:00 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 15:00 ? 00:00:00 [migration/0]
root 6 2 0 15:00 ? 00:00:00 [watchdog/0]
root 7 2 0 15:00 ? 00:00:00 [events/0]
root 8 2 0 15:00 ? 00:00:00 [cgroup]
root 9 2 0 15:00 ? 00:00:00 [khelper]
root 10 2 0 15:00 ? 00:00:00 [netns]
root 11 2 0 15:00 ? 00:00:00 [async/mgr]
root 12 2 0 15:00 ? 00:00:00 [pm]
root 13 2 0 15:00 ? 00:00:00 [sync_supers]
root 14 2 0 15:00 ? 00:00:00 [bdi-default]
root 15 2 0 15:00 ? 00:00:00 [kintegrityd/0]
root 16 2 0 15:00 ? 00:00:00 [kblockd/0]
root 17 2 0 15:00 ? 00:00:00 [kacpid]
[root@test1 ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19232 1488 ? Ss 15:00 0:00 /sbin/init
root 2 0.0 0.0 0 0 ? S 15:00 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 15:00 0:00 [migration/0]
root 4 0.0 0.0 0 0 ? S 15:00 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S 15:00 0:00 [migration/0]
root 6 0.0 0.0 0 0 ? S 15:00 0:00 [watchdog/0]
root 7 0.0 0.0 0 0 ? S 15:00 0:00 [events/0]
[root@test1 ~]# ps -u testuser
PID TTY TIME CMD
1315 pts/1 00:00:00 bash
1335 pts/1 00:00:00 vim
[root@test1 ~]# ps -u testuser -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1315 1314 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
0 T 500 1335 1315 0 80 0 - 35884 signal pts/1 00:00:00 vim
[root@test1 ~]# ps -eo pid,args,psr
PID COMMAND PSR
1 /usr/lib/systemd/systemd -- 1
2 [kthreadd] 1
3 [ksoftirqd/0] 0
5 [kworker/0:0H] 0
7 [migration/0] 0
8 [rcu_bh] 0
9 [rcu_sched] 0
10 [lru-add-drain] 0
11 [watchdog/0] 0
12 [watchdog/1] 1
#省略#
参数选项 | 解释说明 |
---|---|
-l | 列出全部的信号名称 |
-p | 指定kill命令只打印相关进程的进程号,而不发送任何信号 |
-s | 指定要发送的信号 |
[root@test1 ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[root@test1 ~]# kill -l kill #可以使用-l参数对信号名和数字信号互换
9
[root@test1 ~]# kill -l 9
KILL
信号 | 说明 |
---|---|
HUP(1) | 挂起,通常因终端掉线或用户退出而引发 |
INT(2) | 中断,通常是按下Ctrl+c组合键来发出这个信号 |
QUIT(3) | 退出,通常是按下CTRL+\组合键来发出这个信号 |
KILL(9) | 立即结束进程的运行 |
TERM(15) | 终止,通常在系统关机时发送 |
TSTP(20) | 暂停进程的运行,通常是按下Ctrl+z组合键来发出这个信号 |
kill 2203 #kill 命令默认使用的信号为15,这种格式最常用、
kill -s 15 2203 #这种格式使用-s参数明确指定发送值为15的信号,效果和kill 2203一样
kill -15 2203 #上面的-s 15可以简写为-15
kill -9 2203 #信号9会强行终止进程
参数选项 | 解释说明 |
---|---|
-I | 不区分大小写匹配 |
-g | 终止属于该进程组的进程 |
-i | 在终止进程之前询问是否确认 |
-l | 列出所有已知的信号名 |
-q | 如果没有进程终止则不提示 |
-r | 使用正则表达式匹配要终止的进程名称 |
-s | 用指定的信号代替默认信号 |
-u | 终止指定用户的进程 |
-v | 报告信号是否发送成功 |
-w | 等待所有被终止的进程死去。killall每秒都会检查一次被终止的进程是否仍然存在,其仅在都死光后才返回。注意,如果信号被忽略,或者没有起作用,或者进程停留在僵尸状态,那么killall可能会永久等待 |
[root@test1 ~]# killall crond
[root@test1 ~]# killall crond #用killall终止进程可执行多次
crond: no process killed #等看到这个结果说明进程死了
[root@test1 ~]#
[root@test1 ~]# /etc/init.d/crond start #启动服务
Starting crond: [ OK ]
[root@test1 ~]# killall -w crond #使用-w参数,会看到等待几秒后结束命令操作
[root@test1 ~]# killall -w crond
crond: no process killed
[root@test1 ~]# ps -u testuser -l #查看普通用户的所有进程详细信息
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
0 T 500 1235 1210 0 80 0 - 35884 signal pts/1 00:00:00 vim
[root@test1 ~]# killall -u testuser vim #杀掉指定用户的vim进程
[root@test1 ~]# ps -u testuser -l #成功
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
参数选项 | 解释说明 |
---|---|
-t终端 | 杀死指定终端的进程 |
-u用户 | 杀死指定用户的进程 |
[root@test1 ~]# /etc/init.d/crond status #查看定时任务程序运行状态
crond (pid 1274) is running...
[root@test1 ~]# pkill crond #终止定时任务进程
[root@test1 ~]# /etc/init.d/crond status
crond dead but subsys locked #进程被终止
[root@test1 ~]# w #第二列TTY就是用户运行的终端
15:57:09 up 1:05, 3 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
testuser tty1 - 15:55 12.00s 0.02s 0.01s vim ttt
root pts/0 192.168.200.1 14:51 0.00s 0.07s 0.00s w
root pts/1 192.168.200.1 15:19 3:28 0.01s 0.00s bash
[root@test1 ~]# ps -u testuser -l #查看用户的进程
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
4 S 500 1333 1322 0 80 0 - 27075 wait tty1 00:00:00 bash
0 S 500 1359 1333 0 80 0 - 35890 poll_s tty1 00:00:00 vim
[root@test1 ~]# pkill -t tty1 #杀掉终端正在运行的进程
[root@test1 ~]# ps -u testuser -l #vim进程没了
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
4 S 500 1333 1322 0 80 0 - 27075 n_tty_ tty1 00:00:00 bash
[root@test1 ~]# pkill -9 -t tty1 #强行杀掉tty1终端(踢掉用户)
[root@test1 ~]# ps -u testuser -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
[root@test1 ~]# w #tty1终端没了
15:58:17 up 1:06, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.200.1 14:51 0.00s 0.08s 0.00s w
root pts/1 192.168.200.1 15:19 4:36 0.01s 0.00s bash
[root@test1 ~]# w
16:08:24 up 1:16, 3 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
testuser tty1 - 16:01 4.00s 0.03s 0.02s -bash
root pts/0 192.168.200.1 14:51 0.00s 0.09s 0.00s w
root pts/1 192.168.200.1 15:19 14:43 0.01s 0.00s bash
[root@test1 ~]# ps -u testuser -l #查看用户的进程信息
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
4 S 500 1387 1366 0 80 0 - 27076 n_tty_ tty1 00:00:00 bash
0 T 500 1430 1387 0 80 0 - 35883 signal tty1 00:00:00 vim
[root@test1 ~]# pkill -u testuser #杀掉指定用户所有进程
[root@test1 ~]# ps -u testuser -l #成功
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 500 1175 1174 0 80 0 - 27076 wait pts/1 00:00:00 bash
0 S 500 1210 1175 0 80 0 - 27076 n_tty_ pts/1 00:00:00 bash
4 S 500 1387 1366 0 80 0 - 27076 n_tty_ tty1 00:00:00 bash
参数选项 | 解释说明 |
---|---|
-a | 将进程按照使用内存排序 |
-b | 以批处理的模式显示进程信息,输出结果可以传递给其他程序或写入到文件中。在这种模式下,top命令不会接受任何输入,一直运行直到达到-n选项设置的阈值,或者按Ctrl+C等组合键终止程序 |
-c | 显示进程的整个命令路径,而不是只显示命令名称 |
-d | 指定每两次屏幕信息刷新之间的时间间隔 |
-H | 指定这个可以显示每个线程的情况,否则就是进程的总的状态 |
-i | 不显示闲置或者僵死的进程信息 |
-n | top输出信息更新的次数,完成后将退出top命令 |
-p | 显示指定的进程信息 |
[root@test1 ~]# top #使用top命令通常不接任何参数
top - 16:40:31 up 1:48, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 77 total, 1 running, 76 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
Mem: 1004412k total, 152112k used, 852300k free, 11312k buffers
Swap: 2031608k total, 0k used, 2031608k free, 42304k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19232 1536 1256 S 0.0 0.2 0:00.76 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/0
补充:
1)计算真正可用的内存数为:第四行的free+第四行的buffers+第五行的cached。
2)在对内存进行监控时,要时刻关注top命令里第五行swap交换分区的used,如果这个数值还在不断地变化,则说明内核正在不断进行内存和swap的数据交换,这表示内存真的不够用了或者程序运行有内存溢出问题。
3)在top基本视图中,按键盘数字“1”,可监控每个逻辑CPU的状况,显示多核不同核CPU的信息。
4)直接用top无任何参数的方式查看会非常占用系统的资源。可以使用top -b -n1 > test的方式将数据重定向到文件里,再进行查看。
[root@test1 ~]# top -a #使用参数-a将进程按照使用内存排序
top - 18:07:36 up 42 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 164 total, 1 running, 163 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
Mem: 1004412k total, 153948k used, 850464k free, 10296k buffers
Swap: 2031608k total, 0k used, 2031608k free, 37868k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1236 root 20 0 98.0m 4036 3056 S 0.0 0.4 0:00.20 sshd
1209 postfix 20 0 81524 3424 2544 S 0.0 0.3 0:00.01 qmgr
1202 root 20 0 81272 3400 2496 S 0.0 0.3 0:00.03 master
1208 postfix 20 0 81352 3380 2504 S 0.0 0.3 0:00.03 pickup
[root@test1 ~]# top -b #使用参数-b可以看到命令执行结果不停地向下刷新
......
1229 root 20 0 4064 576 496 S 0.0 0.1 0:00.00 mingetty
1231 root 20 0 4064 576 496 S 0.0 0.1 0:00.00 mingetty
1233 root 20 0 4064 576 496 S 0.0 0.1 0:00.00 mingetty
1235 root 20 0 4064 576 496 S 0.0 0.1 0:00.00 mingetty
1236 root 20 0 98.0m 4036 3056 S 0.0 0.4 0:00.22 sshd
1243 root 18 -2 12344 2580 516 S 0.0 0.3 0:00.00 udevd
1244 root 18 -2 12344 2584 516 S 0.0 0.3 0:00.00 udevd
1248 root 20 0 105m 1876 1520 S 0.0 0.2 0:00.04 bash
1301 root 20 0 17384 668 452 S 0.0 0.1 0:00.00 anacron
1318 root 20 0 15032 1340 984 R 0.0 0.1 0:00.01 top
^C #退出使用快捷键Ctrl+C
[root@test1 ~]# top -c #使用参数-c显示进程的整个命令路径。
top - 18:19:38 up 54 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 164 total, 1 running, 163 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
Mem: 1004412k total, 154196k used, 850216k free, 10316k buffers
Swap: 2031608k total, 0k used, 2031608k free, 37904k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19232 1484 1220 S 0.0 0.1 0:00.98 /sbin/init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kthreadd]
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 [migration/0]
//设置执行top命令后的信息刷新时间
[root@test1 ~]# top -d 3 #使用参数-d指定更新周期为3秒,也就是说命令结果每隔3s刷新一次
//设置执行top命令后的信息刷新次数
[root@test1 ~]# top -n 2 #使用参数-n指定更新次数为2次,也就是说命令结果刷新两次后终止退出,-n参数可以和-b参数配合使用
//将top输出结果的全部信息输出到文件中
[root@test1 ~]# top -b -n1 > test #以批处理方式,就刷新1次数据重定向到文件里
[root@test1 ~]# cat test | wc -l
171
[root@test1 ~]# top -n1 > test #如果不是批处理方式,数据量少
[root@test1 ~]# cat test | wc -l
28
//显示指定的进程信息
[root@test1 ~]# top -p 1126 #使用-p选项显示指定进程号的信息
top - 18:31:18 up 1:06, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 1 total, 0 running, 1 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
Mem: 1004412k total, 154032k used, 850380k free, 10448k buffers
Swap: 2031608k total, 0k used, 2031608k free, 38060k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1126 root 20 0 66604 1184 468 S 0.0 0.1 0:00.00 sshd
//显示指定用户的信息
[root@test1 ~]# top -u testuser #使用-u参数显示指定用户的进程信息
top - 18:33:05 up 1:08, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 165 total, 1 running, 164 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
Mem: 1004412k total, 160388k used, 844024k free, 10796k buffers
Swap: 2031608k total, 0k used, 2031608k free, 41696k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1384 testuser 20 0 105m 1896 1528 S 0.0 0.2 0:00.01 bash
1403 testuser 20 0 140m 3968 2684 S 0.0 0.4 0:00.01 vim
[root@test ~]# cd test/
[root@test test]# nohup ping www.baidu.com #让当前执行的进程始终运行,关闭界面也不消失
nohup: ignoring input and appending output to `nohup.out'
^C[root@test test]# ls
nohup.out test
[root@test test]# cat nohup.out #命令的执行记录会被记录在当前目录下的nohup.out中
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=128 time=4.22 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=128 time=4.28 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=128 time=4.20 ms
64 bytes from 61.135.169.125: icmp_seq=4 ttl=128 time=4.21 ms
64 bytes from 61.135.169.125: icmp_seq=5 ttl=128 time=4.16 ms
64 bytes from 61.135.169.125: icmp_seq=6 ttl=128 time=4.11 ms
64 bytes from 61.135.169.125: icmp_seq=7 ttl=128 time=4.22 ms
64 bytes from 61.135.169.125: icmp_seq=8 ttl=128 time=4.18 ms
64 bytes from 61.135.169.125: icmp_seq=9 ttl=128 time=4.21 ms
--- www.a.shifen.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8508ms
rtt min/avg/max/mdev = 4.114/4.203/4.285/0.097 ms
参数选项 | 解释说明 |
---|---|
-c | 统计每一个系统调用所执行的算时间、次数和出错的次数等 |
-d | 输出strace关于标准错误的调试信息 |
-f | 跟踪目标进程,以及目标进程创建的所有子进程@ |
-ff | 如果提供-o filename,则将所有进程的跟踪结果输出到相应的filename.pid中,pid是各进程的进程号 |
-i | 输出系统调用的入口指针 |
-q | 禁止输出关于脱离的消息 |
-r | 输出每一个系统调用的相对时间 |
-t | 在输出中的每一行前加上时间信息。例如:16:45:28 |
-tt | 在输出中的每一行前加上时间信息,精确到微秒。例如11:18:59.759546@ |
-ttt | 在输出中的每一行前加上时间信息,精确到微秒,而且时间表示为UNIX时间戳。例如1486111461.650434 |
-T | 显示每次系统调用所花费的时间 |
-v | 对于某些相关调用,把完整的环境变量、文件stat结构等打印出来 |
-x | 以十六进制形式输出非标准字符串 |
-xx | 所有字符串以十六进制形式输出 |
-o filename | 将strace的输出写入文件filename |
-p pid | 指定要跟踪的进程pid,要同时跟踪多个pid,重复多次-p选项即可@ |
-s strsize | 指定输出的字符串的最大长度,默认为32.并没有将文件名视为字符串,默认全部输出 |
-u username | 以username的UID和GID执行所跟踪的命令 |
输出过滤器
参数选项 | 解释说明 |
---|---|
-e expr | 输出过滤器,通过表达式,可以过滤掉你不想要的输出@ |
-e trace=open | 表示只跟踪open调用而-e trace!=open表示跟踪除open外所有 |
-e trace=file | 只跟踪与文件操作有关的系统调用 |
-e trace=process | 只跟踪与进程有关的系统调用 |
-e trace=network | 只跟踪与网络有关的系统调用 |
-e trace=signal | 只跟踪与系统信号有关的系统调用 |
-e trace=desc | 只跟踪与文件描述符有关的系统调用 |
-e trace=ipc | 只跟踪与进程通信有关的系统调用 |
[root@nginx tmp]# strace -tt -f -o /tmp/test.txt /usr/local/nginx/sbin/nginx #f参数跟踪目标进程,以及目标进程创建的所有子进程,-tt参数在输出中的每一行前加上时间信息,-o将跟踪内容输出到文件里。
[root@nginx tmp]# cat test.txt
3824 05:37:14.300486 prctl(PR_SET_DUMPABLE, 1) = 0
3824 05:37:14.300498 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
3824 05:37:14.300518 epoll_create(512) = 8
3824 05:37:14.300535 eventfd2(0, 0) = 9
3824 05:37:14.300549 epoll_ctl(8, EPOLL_CTL_ADD, 9, {EPOLLIN|EPOLLET, {u32=7095968, u64=7095968}}) = 0
3824 05:37:14.300569 mmap(NULL, 233472, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f73714f7000
3824 05:37:14.300607 brk(0x1fa5000) = 0x1fa5000
3824 05:37:14.300693 epoll_ctl(8, EPOLL_CTL_ADD, 6, {EPOLLIN|0x2000, {u32=1901031440, u64=140133798998032}}) = 0
3824 05:37:14.300712 close(3) = 0
3824 05:37:14.300724 epoll_ctl(8, EPOLL_CTL_ADD, 7, {EPOLLIN|0x2000, {u32=1901031664, u64=140133798998256}}) = 0
3824 05:37:14.300742 epoll_wait(8, #epoll_wait表示等待连接访问,因此后面的输出都是和前一次访问有关。
先将test.txt文件的内容清空,然后模拟去访问nginx
[root@nginxt tmp]# cat test.txt
{{EPOLLIN, {u32=1901031440, u64=140133798998032}}}, 512, 4294967295) = 1
3824 05:57:59.271342 accept4(6, {sa_family=AF_INET, sin_port=htons(64469), sin_addr=inet_addr("192.168.0.254")}, [16], SOCK_NONBLOCK) = 3
3824 05:57:59.271383 epoll_ctl(8, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLET|0x2000, {u32=1901031888, u64=140133798998480}}) = 0
3824 05:57:59.271401 epoll_wait(8, {{EPOLLIN, {u32=1901031888, u64=140133798998480}}}, 512, 60000) = 1
3824 05:57:59.271423 recvfrom(3, "GET / HTTP/1.1\r\nHost: 192.168.0."..., 1024, 0, NULL, NULL) = 308 #recvfrom接收到get请求
3824 05:57:59.271483 stat("/usr/local/nginx/html/www/index.html", 0x7fff69246350) = -1 ENOENT (No such file or directory) #查看index.html文件不存在
3824 05:57:59.271506 stat("/usr/local/nginx/html/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
3824 05:57:59.271525 stat("/usr/local/nginx/html/www/index.htm", 0x7fff69246350) = -1 ENOENT (No such file or directory) #查看index.htm文件不存在
#下面向用户返回403错误,并写入错误日志
3824 05:57:59.271546 write(5, "2018/01/14 05:57:59 [error] 3824"..., 200) = 200
3824 05:57:59.271587 writev(3, [{"HTTP/1.1 403 Forbidden\r\nServer: "..., 155}, {"\r\n403 Forbidd"..., 116}, {"
nginx/1.10.2
[root@nginx tmp]# strace -tt -f -e trace=file -o /tmp/test.txt /usr/local/nginx/sbin/nginx
[root@nginx tmp]# cat test.txt
3860 06:28:42.306924 stat("/usr/local/nginx/html/www/index.html", 0x7fff3ce39670) = -1 ENOENT (No such file or directory)
3860 06:28:42.306973 stat("/usr/local/nginx/html/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
3860 06:28:42.306994 stat("/usr/local/nginx/html/www/index.htm", 0x7fff3ce39670) = -1 ENOENT (No such file or directory)
[root@nginx tmp]# /usr/local/nginx/sbin/nginx #启动nginx服务
[root@nginx tmp]# pgrep nginx -l
3873 nginx #nginx的master进程
3874 nginx #nginx的worker进程
[root@nginx tmp]# strace -tt -f -e trace=file -p 3874 #使用-p参数,只跟踪worker进程,结果更加精简
Process 3874 attached - interrupt to quit
06:49:38.629248 stat("/usr/local/nginx/html/www/index.html", 0x7fffbd4e7fc0) = -1 ENOENT (No such file or directory)
06:49:38.629329 stat("/usr/local/nginx/html/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
06:49:38.629353 stat("/usr/local/nginx/html/www/index.htm", 0x7fffbd4e7fc0) = -1 ENOENT (No such file or directory)
[root@nginx tmp]# strace -c /usr/local/nginx/sbin/nginx #使用-c参数为进程所有的系统调用做一个统计分析
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00 0.000047 47 1 clone
0.00 0.000000 0 29 read
0.00 0.000000 0 31 open
0.00 0.000000 0 33 close
0.00 0.000000 0 6 stat
0.00 0.000000 0 28 fstat
0.00 0.000000 0 1 lseek
0.00 0.000000 0 61 mmap
0.00 0.000000 0 36 mprotect
0.00 0.000000 0 7 munmap
0.00 0.000000 0 6 brk
0.00 0.000000 0 14 rt_sigaction
0.00 0.000000 0 1 rt_sigprocmask
0.00 0.000000 0 1 ioctl
0.00 0.000000 0 2 pread
0.00 0.000000 0 2 2 access
0.00 0.000000 0 5 socket
0.00 0.000000 0 4 4 connect
0.00 0.000000 0 1 bind
0.00 0.000000 0 2 listen
0.00 0.000000 0 1 setsockopt
0.00 0.000000 0 1 execve
0.00 0.000000 0 2 uname
0.00 0.000000 0 3 fcntl
0.00 0.000000 0 5 5 mkdir
0.00 0.000000 0 3 getrlimit
0.00 0.000000 0 1 geteuid
0.00 0.000000 0 1 statfs
0.00 0.000000 0 1 arch_prctl
0.00 0.000000 0 2 1 futex
0.00 0.000000 0 1 epoll_create
0.00 0.000000 0 1 set_tid_address
0.00 0.000000 0 1 set_robust_list
------ ----------- ----------- --------- --------- ----------------
100.00 0.000047 294 12 total
[root@nginx tmp]# strace -c -o /tmp/test.txt /usr/local/nginx/sbin/nginx #-o选项将strace的结果输出到文件中
[root@nginx tmp]# cat test.txt
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
-nan 0.000000 0 29 read
-nan 0.000000 0 31 open
-nan 0.000000 0 33 close
-nan 0.000000 0 6 stat
[root@nginx tmp]# /usr/local/nginx/sbin/nginx -s stop
[root@nginx tmp]# strace -T /usr/local/nginx/sbin/nginx #使用-T将每个系统调用所花费的时间打印出来,每个调用的时间花销在调用行最右边的尖括号里
execve("/usr/local/nginx/sbin/nginx", ["/usr/local/nginx/sbin/nginx"], [/* 24 vars */]) = 0 <0.000075>
brk(0) = 0x1055000 <0.000003>
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fbbf84f6000 <0.000004>
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000005>
open("/etc/ld.so.cache", O_RDONLY) = 3 <0.000004>
fstat(3, {st_mode=S_IFREG|0644, st_size=15441, ...}) = 0 <0.000003>
mmap(NULL, 15441, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fbbf84f2000 <0.000003>
close(3) = 0 <0.000003>
open("/lib64/libdl.so.2", O_RDONLY) = 3 <0.000005>
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\r\0\0\0\0\0\0"..., 832) = 832 <0.000003>
fstat(3, {st_mode=S_IFREG|0755, st_size=19536, ...}) = 0 <0.000003>
mmap(NULL, 2109696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fbbf80d4000 <0.000004>
mprotect(0x7fbbf80d6000, 2097152, PROT_NONE) = 0 <0.000004>、
ltrace能够跟踪进程的库函数调用,它会显现出调用了哪个库函数,而strace则是跟踪进程的每个系统调用
参数选项 | 解释说明 |
---|---|
-c | 统计库函数每次调用的时间,最后程序退出时打印摘要 |
-C | 解码低级别名称(内核级)为用户级名称 |
-d | 打印调试信息 |
-e expr | 输出过滤器,通过表达式,可以过滤掉你不想要的输出@ |
-e printf | 表示只查看printf函数调用 |
-f | 跟踪子进程 |
-o filename | 将ltrace的输出写入文件filename |
-p pid | 指定要跟踪的进程pid@ |
-r | 输出每一个调用的相对时间 |
-S | 显示系统调用 |
-t | 在输出中每一行前加上时间信息。例如 16:45:28 |
-tt | 在输出中每一行前加上时间信息。例如 11:18:59.759546 |
-ttt | 在输出中每一行前加上时间信息,精确到微妙,而且时间表示为UNIX时间戳。 |
-T | 显示每次调用所花费时间 |
-u username | 以username的UID和GID执行所跟踪的命令 |
(1)ltrace使用
[root@nginx tmp]# ltrace /usr/local/nginx/sbin/nginx #ltrace后面直接接上要检测的命令语句
memcpy(0x1d5b7b0, "www.test.com", 17) = 0x1d5b7b0
memcpy(0x1d5b7c1, "/usr/local/nginx/", 17) = 0x1d5b7c1
malloc(2048) = 0x1d55530
memset(0x1d55530, '\000', 68) = 0x1d55530
memset(0x1d55530, '\000', 70) = 0x1d55530
memset(0x1d55530, '\000', 72) = 0x1d55530
memset(0x1d55530, '\000', 74) = 0x1d55530
memset(0x1d55530, '\000', 76) = 0x1d55530
memset(0x1d55530, '\000', 78) = 0x1d55530
memset(0x1d55530, '\000', 80) = 0x1d55530
memset(0x1d55530, '\000', 82) = 0x1d55530
memset(0x1d55530, '\000', 84) = 0x1d55530
memset(0x1d55530, '\000', 86) = 0x1d55530
......
sigemptyset(0x7fffb75519f8) = 0
sigaction(17, 0x7fffb75519f0, NULL) = 0
sigemptyset(0x7fffb75519f8) = 0
sigaction(31, 0x7fffb75519f0, NULL) = 0
sigemptyset(0x7fffb75519f8) = 0
sigaction(13, 0x7fffb75519f0, NULL) = 0
fork() = 3962
[pid 3961] exit(0
[pid 3961] +++ exited (status 0) +++
(2)通过pid跟踪进程调用库函数
[root@nginx tmp]# pgrep -l nginx
3967 nginx
3968 nginx #nginx的worker进程的pid号
[root@nginx tmp]# ltrace -p 3968 使用-p指定进程号
__errno_location() = 0x7f0a392e1768
gettimeofday(0x7fff8f684cd0, NULL) = 0
memcpy(0x6b281f, "14", 2) = 0x6b281f
memcpy(0x6b2826, "2018", 4) = 0x6b2826
memcpy(0x6b282b, "12", 2) = 0x6b282b
memcpy(0x6b282e, "28", 2) = 0x6b282e
memcpy(0x6b2831, "30", 2) = 0x6b2831
localtime_r(0x7fff8f684c88, 0x7fff8f684d20, 0x48400a, 12339, 0x7fff8f684b52) = 0x7fff8f684d20
memcpy(0x6b22fc, "2018", 4) = 0x6b22fc
memcpy(0x6b2302, "1", 1) = 0x6b2302
memcpy(0x6b2304, "14", 2) = 0x6b2304
memcpy(0x6b2308, "7", 1) = 0x6b2308
memcpy(0x6b230a, "28", 2) = 0x6b230a
memcpy(0x6b230d, "30", 2) = 0x6b230d
memcpy(0x6b2f91, "14", 2) = 0x6b2f91
memcpy(0x6b2f98, "2018", 4) = 0x6b2f98
memcpy(0x6b2f9e, "7", 1) = 0x6b2f9e
memcpy(0x6b2fa0, "28", 2) = 0x6b2fa0
memcpy(0x6b2fa3, "30", 2) = 0x6b2fa3
memcpy(0x6b2fa8, "5", 1) = 0x6b2fa8
memcpy(0x6b2faa, "0", 1) = 0x6b2faa
memcpy(0x6b364e, "2018", 4) = 0x6b364e
memcpy(0x6b3654, "1", 1) = 0x6b3654
memcpy(0x6b3656, "14", 2) = 0x6b3656
memcpy(0x6b365a, "7", 1) = 0x6b365a
memcpy(0x6b365c, "28", 2) = 0x6b365c
memcpy(0x6b365f, "30", 2) = 0x6b365f
memcpy(0x6b3663, "5", 1) = 0x6b3663
memcpy(0x6b3666, "0", 1) = 0x6b3666
memcpy(0x6b3cb4, "14", 2) = 0x6b3cb4
memcpy(0x6b3cb8, "7", 1) = 0x6b3cb8
memcpy(0x6b3cba, "28", 2) = 0x6b3cba
memcpy(0x6b3cbd, "30", 2) = 0x6b3cbd
epoll_wait(8, 0x2639990, 512, 0xffffffff, 0x7fff8f684b52
参数选项 | 解释说明 |
---|---|
-A | 以ASCII码的方式显示每一个数据包(不会显示数据包中链路层的头部信息)。在抓取包含网页数据的数据包时,可方便查看数据 |
-c <数据包数目> | 接收到指定的数据包数目后退出命令@ |
-e | 每行的打印输出中将包含数据包的数据链路层头部信息 |
-i <网络接口> | 指定要监听数据包的网络接口@ |
-n | 不进行DNS解析,加快显示速度@ |
-nn | 不将协议和端口数字等转换成名字@ |
-q | 以快速输出的方式运行,此选项仅显示数据包的协议概要信息,输出信息较短@ |
-s <数据包大小> | 设置数据包抓取长度,如果不设置则默认为68字节,设置为0则自动选择合适的长度来抓取数据包 |
-t | 在每行输出信息中不显示时间戳标记 |
-tt | 在每行输出信息中显示无格式的时间戳标记 |
-ttt | 显示当前行与前一行的延迟 |
-tttt | 在每行打印的时间戳之前添加日期 |
-ttttt | 显示当前行与第一行的延迟 |
-v | 显示命令执行的详细信息 |
-vv | 显示比-v选项更加详细的信息 |
-vvv | 显示比-vv选项更加详细的输出 |
[root@nginx ~]# tcpdump #默认情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
05:12:45.464963 IP localhost.ssh > localhost.50832: Flags [P.], seq 898292388:898292596, ack 861396487, win 317, length 208
05:12:45.465055 IP localhost.50832 > localhost.ssh: Flags [.], ack 208, win 523, length 0
05:12:45.465215 IP localhost.57595 > localhost.domain: 4104+ PTR? 254.0.168.192.in-addr.arpa. (44)
05:12:45.467851 IP localhost.domain > localhost.57595: 4104 1/0/0 PTR localhost. (67)
05:12:45.467906 IP localhost.57067 > localhost.domain: 107+ PTR? 233.0.168.192.in-addr.arpa. (44)
05:12:45.469444 IP localhost.domain > localhost.57067: 107 1/0/0 PTR localhost. (67)
05:12:45.469504 IP localhost.34192 > localhost.domain: 2703+ PTR? 1.0.168.192.in-addr.arpa. (42)
05:12:45.621206 IP localhost.ssh > localhost.50832: Flags [P.], seq 3120:3280, ack 1, win 317, length 160
05:12:45.621258 IP localhost.50832 > localhost.ssh: Flags [.], ack 3280, win 524, length 0
05:12:45.636742 IP localhost.ssh > localhost.50832: Flags [P.], seq 3280:3536, ack 1, win 317, length 256
05:12:45.643843 IP localhost.50832 > localhost.ssh: Flags [P.], seq 1:65, ack 3536, win 523, length 64
05:12:45.643889 IP localhost.ssh > localhost.50832: Flags [P.], seq 3536:3696, ack 65, win 317, length 160
^C #tcpdump命令在运行期间可以使用组合Ctrl+C终止程序
29 packets captured #最后三行就是按CTRL+C后输出的监听到的数据包汇总信息
32 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -q #默认情况下,tcpdump命令的输出信息较多,为了显示精简的信息,可以使用-q选项
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
05:33:01.438200 IP localhost.ssh > localhost.50832: tcp 208
05:33:01.479036 IP localhost.50832 > localhost.ssh: tcp 0
05:33:01.494539 IP localhost.ssh > localhost.50832: tcp 176
05:33:01.510460 IP localhost.ssh > localhost.50832: tcp 112
05:33:01.510907 IP localhost.50832 > localhost.ssh: tcp 0
05:33:01.525789 IP localhost.ssh > localhost.50832: tcp 176
05:33:01.541450 IP localhost.ssh > localhost.50832: tcp 112
05:33:01.541548 IP localhost.50832 > localhost.ssh: tcp 0
05:33:01.557049 IP localhost.ssh > localhost.50832: tcp 176
05:33:01.574173 IP localhost.ssh > localhost.50832: tcp 112
05:33:01.574486 IP localhost.50832 > localhost.ssh: tcp 0
05:33:01.583765 IP localhost.50832 > localhost.ssh: tcp 64
05:33:01.583857 IP localhost.ssh > localhost.50832: tcp 176
^C
24 packets captured
26 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -c 5 #使用-c选项指定监听的数据包数量,这样就不需要使用Ctrl+C了
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
05:34:24.515192 IP localhost.ssh > localhost.50832: Flags [P.], seq 898300004:898300212, ack 861398503, win 317, length 208
05:34:24.515301 IP localhost.50832 > localhost.ssh: Flags [.], ack 208, win 519, length 0
05:34:24.515445 IP localhost.60389 > localhost.domain: 26412+ PTR? 254.0.168.192.in-addr.arpa. (44)
05:34:24.518180 IP localhost.domain > localhost.60389: 26412 1/0/0 PTR localhost. (67)
05:34:24.518247 IP localhost.38804 > localhost.domain: 7473+ PTR? 233.0.168.192.in-addr.arpa. (44)
5 packets captured
10 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -i eth0 #使用-i选项可以指定要监听的网卡
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
05:46:06.865643 IP localhost.ssh > localhost.50832: Flags [P.], seq 898335828:898336036, ack 861403175, win 317, length 208
05:46:06.865721 IP localhost.50832 > localhost.ssh: Flags [.], ack 208, win 524, length 0
05:46:06.865876 IP localhost.37090 > localhost.domain: 16313+ PTR? 254.0.168.192.in-addr.arpa. (44)
^C
49 packets captured
52 packets received by filter
0 packets dropped by kernel
命令结果说明:
[root@nginx ~]# tcpdump -n -c 5 host 192.168.0.254 #使用-n选项不进行DNS解析,加快显示速度。监听指定主机的关键字为host,后面直接接主机名或IP地址即可。本行命令的作用是监听所有192.168.0.254的主机收到的和发出的数据包
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
06:18:59.812585 IP 192.168.0.233.ssh > 192.168.0.254.50832: Flags [P.], seq 898389300:898389508, ack 861410071, win 317, length 208
06:18:59.812763 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 208, win 524, length 0
06:18:59.813478 IP 192.168.0.233.ssh > 192.168.0.254.50832: Flags [P.], seq 208:496, ack 1, win 317, length 288
06:18:59.814441 IP 192.168.0.233.ssh > 192.168.0.254.50832: Flags [P.], seq 496:672, ack 1, win 317, length 176
06:18:59.814534 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 672, win 522, length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -n -c 5 src host 192.168.0.254 #只监听从192.168.0.254发出的数据包,即源地址为192.168.0.254,关键字为src(source,源地址)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
06:19:45.439633 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 898393156, win 522, length 0
06:19:45.511489 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 161, win 521, length 0
06:19:45.589521 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 321, win 520, length 0
06:19:45.667712 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 481, win 520, length 0
06:19:45.733979 IP 192.168.0.254.50832 > 192.168.0.233.ssh: Flags [.], ack 641, win 519, length 0
5 packets captured
6 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -n -c 5 dst host 192.168.0.254 #只监听192.168.0.254收到的数据包,即目标地址为192.168.0.254,关键字为dst(destination,目的地)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18:21:33.783811 IP 192.168.0.233.ssh > 192.168.0.254.55962: Flags [P.], seq 1885784800:1885785008, ack 322191067, win 317, length 208
18:21:33.785709 IP 192.168.0.233.ssh > 192.168.0.254.55962: Flags [P.], seq 208:400, ack 1, win 317, length 192
18:21:33.786677 IP 192.168.0.233.ssh > 192.168.0.254.55962: Flags [P.], seq 400:576, ack 1, win 317, length 176
18:21:33.787676 IP 192.168.0.233.ssh > 192.168.0.254.55962: Flags [P.], seq 576:752, ack 1, win 317, length 176
18:21:33.788684 IP 192.168.0.233.ssh > 192.168.0.254.55962: Flags [P.], seq 752:928, ack 1, win 317, length 176
5 packets captured
5 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -nn -c 5 port 22 #-nn不进行DNS解析,不将端口转换成服务名字, port指定监听端口
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18:27:25.472624 IP 192.168.0.233.22 > 192.168.0.254.55962: Flags [P.], seq 1886385856:1886386064, ack 322195131, win 317, length 208
18:27:25.472764 IP 192.168.0.254.55962 > 192.168.0.233.22: Flags [.], ack 208, win 522, length 0
18:27:25.473731 IP 192.168.0.233.22 > 192.168.0.254.55962: Flags [P.], seq 208:496, ack 1, win 317, length 288
18:27:25.474746 IP 192.168.0.233.22 > 192.168.0.254.55962: Flags [P.], seq 496:672, ack 1, win 317, length 176
18:27:25.474836 IP 192.168.0.254.55962 > 192.168.0.233.22: Flags [.], ack 672, win 520, length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -n -c 5 arp #监听arp协议数据包,因此表达式直接写arp即可
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18:29:08.056959 ARP, Request who-has 192.168.0.111 tell 192.168.0.1, length 46
18:29:08.978765 ARP, Request who-has 192.168.0.111 tell 192.168.0.1, length 46
18:29:09.900334 ARP, Request who-has 192.168.0.111 tell 192.168.0.1, length 46
18:29:10.822093 ARP, Request who-has 192.168.0.111 tell 192.168.0.1, length 46
18:29:12.050836 ARP, Request who-has 192.168.0.111 tell 192.168.0.1, length 46
5 packets captured
5 packets received by filter
0 packets dropped by kernel
[root@nginx ~]# tcpdump -n -c 5 icmp #监听icmp数据包(想要查看下面的监控数据,可以使用其他机器ping本机即可)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18:30:55.576828 IP 192.168.0.254 > 192.168.0.233: ICMP echo request, id 1, seq 19956, length 40
18:30:55.576844 IP 192.168.0.233 > 192.168.0.254: ICMP echo reply, id 1, seq 19956, length 40
18:30:56.578427 IP 192.168.0.254 > 192.168.0.233: ICMP echo request, id 1, seq 19958, length 40
18:30:56.578445 IP 192.168.0.233 > 192.168.0.254: ICMP echo reply, id 1, seq 19958, length 40
18:30:57.582167 IP 192.168.0.254 > 192.168.0.233: ICMP echo request, id 1, seq 19960, length 40
5 packets captured
6 packets received by filter
0 packets dropped by kernel
常见的协议关键字有ip,arp,icmp,tcp,udp等类型
使用tcpdump对tcp数据进行抓包
[root@nginx www]# tcpdump tcp port 80 or dst 192.168.0.114 -i eth0 -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
#抓包分析:三次握手过程
22:38:18.564320 ARP, Reply 192.168.0.233 is-at 00:0c:29:a8:ca:50, length 28
#发送了一个ARP响应包给目标MAC地址,告知对方本机的MAC地址
22:38:18.564418 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [S], seq 3675775834, win 14600, options [mss 1460,sackOK,TS val 4294710555 ecr 0,nop,wscale 6], length 0
#IP为192.168.0.114(client)通过临时端口52367向本机192.168.0.233(server)的80监听端口发起连接,client的初始包序号为3675775834,滑动窗口大小为14600字节(即TCP接收缓冲区的大小,用于TCP拥塞控制),mss大小为1460(即可接收的最大包长度),[S]=[SYN](发起连接标志)
22:38:18.564434 IP 192.168.0.233.http > 192.168.0.114.52367: Flags [S.], seq 2909831439, ack 3675775835, win 14480, options [mss 1460,sackOK,TS val 15157720 ecr 4294710555,nop,wscale 6], length 0
#Server的响应连接,同时带上上一个包的ack信息(为client端的初始包序号+1,即3675775835,也就是server端下次等待接收这个包序号的包,用于TCP字节流的顺序控制。Server端的初始包序号为2909831439,mss也是1460)
22:38:18.564541 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [.], ack 1, win 229, options [nop,nop,TS val 4294710556 ecr 15157720], length 0
#Client端再次确认,tcp三次握手完成。“.”表示没有任何标识
数据传输过程:
22:38:18.564654 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [P.], seq 1:169, ack 1, win 229, options [nop,nop,TS val 4294710557 ecr 15157720], length 168
#Client端发请求包,包长度是168字节。[P]=[push](传送数据标志)
22:38:18.564658 IP 192.168.0.233.http > 192.168.0.114.52367: Flags [.], ack 169, win 243, options [nop,nop,TS val 15157720 ecr 4294710557], length 0 #Server端回应Client端表示收到请求,并确认已经收到了之前的168字节
22:38:18.564707 IP 192.168.0.233.http > 192.168.0.114.52367: Flags [P.], seq 1:237, ack 169, win 243, options [nop,nop,TS val 15157720 ecr 4294710557], length 236 #Server回包,包长度236字节
22:38:18.564755 IP 192.168.0.233.http > 192.168.0.114.52367: Flags [P.], seq 237:258, ack 169, win 243, options [nop,nop,TS val 15157720 ecr 4294710557], length 21
22:38:18.564773 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [.], ack 237, win 245, options [nop,nop,TS val 4294710557 ecr 15157720], length 0
22:38:18.564818 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [.], ack 258, win 245, options [nop,nop,TS val 4294710557 ecr 15157720], length 0
4次挥手过程:
22:38:18.564946 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [F.], seq 169, ack 258, win 245, options [nop,nop,TS val 4294710557 ecr 15157720], length 0
#Client端发送关闭连接请求,F=FIN(断开连接标志)
22:38:18.564956 IP 192.168.0.233.http > 192.168.0.114.52367: Flags [F.], seq 258, ack 170, win 243, options [nop,nop,TS val 15157720 ecr 4294710557], length 0
#Server端回应并确认了Client端的断开连接请求,并且也发送了FIN标志关闭。(只有当服务器传输未完成时,此处才会出现两次挥手,如果Clinet发起断开请求时,服务器已经传输数据完成,则此处服务端会直接回应关闭标志FIN)
22:38:18.565022 IP 192.168.0.114.52367 > 192.168.0.233.http: Flags [.], ack 259, win 245, options [nop,nop,TS val 4294710557 ecr 15157720], length 0
#Clinet端响应ack,关闭连接的四次挥手完成
参数选项 | 解释说明 |
---|---|
-sS | TCP同步扫描(TCP SYN)@ |
-sT | TCP连接扫描 |
-sn | 不进行端口扫描,只检查主机正在运行。该选项与老版本的-sP相同@ |
-sU | 扫描UDP端口 |
-sV | 探测服务版本信息 |
-Pn | 只进行扫描,不ping主机 |
-PS | 使用SYN包对目标主机进行扫描。默认是80端口,也可以指定端口,格式为-PS22或-PS22-25,80,113,1050,35000,记住PS和端口号之间不要有空格 |
-PU | 使用udp ping扫描端口 |
-O | 激活对TCP/IP指纹特征(fingerprinting)的扫描,获得远程主机的标志,也就是操作系统类型 |
-v | 显示扫描过程中的详细信息@ |
-S | 设置扫描的源IP地址 |
-g port | 设置扫描的源端口 |
-oN | 把扫描的结果重定向到文件中 |
-iL filename | 从文件中读取扫描的目标 |
-p <端口> | 指定要扫描的端口,可以是一个单独的端口,也可以用逗号分隔开多个端口,或者使用“-”表示端口范围@ |
-n | 不进行DNS解析,加快扫描速度@ |
-exclude | 排除指定主机 |
-excludefile | 排除指定文件中的主机 |
[root@nginx ~]# nmap 192.168.0.1 #直接接目标主机,默认会扫描1~1000端口
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:23 EST
Nmap scan report for localhost (192.168.0.1)
Host is up (0.014s latency). #目标主机正在运行
Not shown: 999 closed ports #999个端口关闭
PORT STATE SERVICE
80/tcp open http #开放的80端口http服务
MAC Address: CC:B2:55:DF:3C:83 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.37 seconds
[root@nginx ~]# nmap -p 1024-65535 192.168.0.1 #-p选项指定扫描范围
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:26 EST
Nmap scan report for localhost (192.168.0.1)
Host is up (0.039s latency).
Not shown: 64511 closed ports
PORT STATE SERVICE
1780/tcp open unknown
MAC Address: CC:B2:55:DF:3C:83 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 17.46 seconds
[root@nginx ~]# nmap 192.168.0.0/24 #使用网段的格式扫描局域网
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:29 EST
Nmap scan report for localhost (192.168.0.1)
Host is up (0.0072s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
80/tcp open http
MAC Address: CC:B2:55:DF:3C:83 (Unknown)
Nmap scan report for localhost (192.168.0.129)
Host is up (0.092s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
80/tcp open http
MAC Address: F0:FE:6B:69:5B:1E (Unknown)
Nmap scan report for localhost (192.168.0.133)
Host is up (0.33s latency).
All 1000 scanned ports on localhost (192.168.0.133) are closed
MAC Address: BC:3D:85:FE:3F:DA (Unknown)
Nmap scan report for localhost (192.168.0.233)
Host is up (0.0000010s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
22/tcp open ssh
Nmap scan report for localhost (192.168.0.254)
Host is up (0.00016s latency).
Not shown: 992 closed ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
443/tcp open https
445/tcp open microsoft-ds
902/tcp open iss-realsecure
912/tcp open apex-mesh
5678/tcp open rrac
10000/tcp open snet-sensor-mgmt
MAC Address: 30:B4:9E:74:1B:3B (Unknown)
Nmap done: 256 IP addresses (5 hosts up) scanned in 19.27 seconds
[root@nginx ~]# nmap -sn 192.168.0.0/24 #使用-sn选项不扫描端口
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:32 EST
Nmap scan report for localhost (192.168.0.1)
Host is up (0.0027s latency).
MAC Address: CC:B2:55:DF:3C:83 (Unknown)
Nmap scan report for localhost (192.168.0.233)
Host is up.
Nmap scan report for localhost (192.168.0.254)
Host is up (0.000071s latency).
MAC Address: 30:B4:9E:74:1B:3B (Unknown)
Nmap done: 256 IP addresses (3 hosts up) scanned in 2.56 seconds
[root@nginx ~]# nmap -sn 192.168.0.232-234 #使用这种地址范围进行扫描
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:34 EST
Nmap scan report for localhost (192.168.0.233)
Host is up.
Nmap done: 3 IP addresses (1 host up) scanned in 0.81 seconds
[root@nginx ~]# nmap -O -sV 192.168.0.1
Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-28 08:43 EST
Nmap scan report for localhost (192.168.0.1)
Host is up (0.0037s latency).
Not shown: 999 closed ports
PORT STATE SERVICE VERSION
80/tcp open http Linksys wireless-G WAP http config (Name D-Link Wireless N Router DIR-600M)
MAC Address: CC:B2:55:DF:3C:83 (Unknown)
Device type: general purpose
Running: Linux 2.4.X
OS details: Linux 2.4.18 - 2.4.35 (likely embedded)
Network Distance: 1 hop
Service Info: Device: WAP
OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 2.93 seconds