Linux查看端口占用情况

排查问题的时候,可能需要知道这个端口目前被哪个服务占用着,在linux中,一般会用到lsofnetstat这2个命令。比如检查80端口的占用情况

lsof

1
2
3
4
5
6
[root@VM_43_49_centos ~]# sudo lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 5358 root 6u IPv4 236554022 0t0 TCP *:http (LISTEN)
nginx 5358 root 7u IPv6 236554023 0t0 TCP *:http (LISTEN)
nginx 28325 nginx 6u IPv4 236554022 0t0 TCP *:http (LISTEN)
nginx 28325 nginx 7u IPv6 236554023 0t0 TCP *:http (LISTEN)

netstat

1
2
3
[root@VM_43_49_centos ~]# sudo netstat -tunlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5358/nginx: master
tcp6 0 0 :::80 :::* LISTEN 5358/nginx: master

注意在Mac上面,netstat的命令可能会出现下面的异常:

1
2
3
4
5
6
7
8
9
10
~ » netstat -tunlp | grep 80
netstat: option requires an argument -- p
Usage: netstat [-AaLlnW] [-f address_family | -p protocol]
netstat [-gilns] [-f address_family]
netstat -i | -I interface [-w wait] [-abdgRtS]
netstat -s [-s] [-f address_family | -p protocol] [-w wait]
netstat -i | -I interface -s [-f address_family | -p protocol]
netstat -m [-m]
netstat -r [-Aaln] [-f address_family]
netstat -rs [-s]

查询了一下stackoverflow,发现How to query ports are using by one process with knowing its name or pid on mac?

If you are only interested in inet ports then you can use:

1
netstat -anvf inet

Or TCP sockets:

1
netstat -anvp tcp

Or UDP sockets:

1
netstat -anvp udp

terminal快捷键

终端跳转解说图

bash-shortcut.jpg

常用快捷键

编辑

  • Ctrl + a – 跳到行首
  • Ctrl + e – 跳到行尾
  • Ctrl + k – 删除当前光标至行尾内容
  • Ctrl + u – 删除当前光标至行首内容
  • Ctrl + w – 删除当前光标至词首内容
  • Ctrl + y – 将剪切的内容粘贴在光标后
  • Ctrl + xx – 在行首和当前光标处(来回)移动
  • Alt + b – 跳到词首
  • Alt + f – 跳到词尾
  • Alt + d – 删除自光标处起的单词内容
  • Alt + c – 大写光标处的字符(注:该条内容与原文不同)
  • Alt + u – 大写自光标处起的单词内容
  • Alt + l – 小写自光标处起的单词内容
  • Alt + t – 将光标处单词与上一个词交换
  • Ctrl + f – 向前移动一个字符(相当于按向左箭头)
  • Ctrl + b – 向后移动一个字符(相当于按向右箭头)
  • Ctrl + d – 删除光标后一个字符(相当于按Delete)
  • Ctrl + h – 删除光标前一个字符(相当于按后退键)
  • Ctrl + t – 交换光标处的两个字符

搜索

  • Ctrl + r – 反向搜索历史命令
  • Ctrl + g – 退出历史搜索模式(相当于按Esc)
  • Ctrl + p – 上一个历史命令(相当于按向上箭头)
  • Ctrl + n – 下一个历史命令(相当于按向下箭头)
  • Alt + . – 使用上一个命令的最后一个单词

控制

  • Ctrl + l – 清屏
  • Ctrl + s – 终止输出到屏幕(对长时间运行并打印详细信息的命令)
  • Ctrl + q – 允许输出到屏幕(如果之前用过终止输出命令)
  • Ctrl + c – 终止命令
  • Ctrl + z – 中断命令

Bang(即感叹号)

  • !! – 执行上一条命令
  • !blah –执行最近运行过的以blah开头的命令
  • !blah:p – 打印!blah要执行的命令(并将其作为最后一条命令加入到命令历史中)
  • !$ – 上一条命令的最后一个单词 (等同于Alt + .)
  • !$:p – 打印!$指代的单词
  • !* – 上一条命令除最后一个词的部分
  • !:p – 打印!指代部分

参考资料

ApacheBench ab压测工具

ab简介

ApacheBench 是 Apache服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,根据ab命令可以创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,因此可以用来测试目标服务器的负载压力。

安装ab

1
sudo apt-get install apache2-utils

参数列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform //请求链接数
-c concurrency Number of multiple requests to make at a time //表示并发数
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

基本使用

GET 压测

1
ab -n 100 -c 10 http://www.baidu.com/
  • -n表示请求数
  • -c表示并发数

样例输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient).....done


Server Software: BWS/1.1
Server Hostname: www.baidu.com
Server Port: 80


Document Path: / #测试页面
Document Length: 112439 bytes #测试页面大小

Concurrency Level: 10 #并发数
Time taken for tests: 1.256 seconds #整个测试话费的时间
Complete requests: 100 #完成请求的总量
Failed requests: 96 #失败的请求次数
(Connect: 0, Receive: 0, Length: 96, Exceptions: 0)
Write errors: 0
Total transferred: 11348660 bytes #传输数据总大小
HTML transferred: 11253726 bytes #传输页面总大小
Requests per second: 79.62 [#/sec] (mean) #平均每秒请求数
Time per request: 125.593 [ms] (mean) #平均每次并发10个请求的处理时间
Time per request: 12.559 [ms] (mean, across all concurrent requests) #平均每个请求处理时间,所有并发的请求加一起
Transfer rate: 8824.29 [Kbytes/sec] received #平均每秒网络流量

Connection Times (ms)
min mean[+/-sd] median max
Connect: 4 20 7.7 18 38
Processing: 18 90 50.5 82 356
Waiting: 4 22 7.9 22 41
Total: 22 111 50.7 101 384
#花费在连接Connect,处理Processing,等待Waiting的时间的最小min,平均值mean,标准差[+/-sd],中值median,最大表max的一个表。

Percentage of the requests served within a certain time (ms)
50% 101 #50%请求的响应时间在101ms内
66% 103 #66%请求的响应时间在103ms内
75% 104 #...以此类推
80% 105
90% 111
95% 267
98% 311
99% 384
100% 384 (longest request)

带自定义header请求:

1
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

POST请求

1
ab -n 1 -c 1 -p 'post.txt' -T 'application/x-www-form-urlencoded'   http://192.168.188.6:8080/distributeLock2
  • -p 用来做post数据的文件,这里此文件保存在ab同级目录下
  • -T 设置content-type值
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×