
API接口重試的8種方法
tcp6 0 0 :::8080 :::* LISTEN 2871/java
net-tools
包sudo lsof -i :80
-i
:指定網(wǎng)絡(luò)連接(格式:端口
/@主機(jī)
)-P
:禁止端口號(hào)轉(zhuǎn)服務(wù)名稱(chēng)(顯示原始數(shù)字)-n
:禁用主機(jī)名解析COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1234 root 6u IPv4 0xabcd 0t0 TCP *:http (LISTEN)
lsof -u apache -i
lsof -i 6
ss -tulnp | grep '80'
-t
:TCP協(xié)議-u
:UDP協(xié)議-l
:監(jiān)聽(tīng)狀態(tài)-n
:數(shù)字格式-p
:顯示進(jìn)程信息netstat
快30%ss -o state time-wait
)sudo fuser 80/tcp
80/tcp: 1234 5678
-k
參數(shù)可直接終止進(jìn)程(慎用?。?/li>
ls -l /proc/<PID>/fd | grep socket:
/proc/net/tcp
文件解析端口號(hào)(16進(jìn)制)1F90
(8080)= echo $((0x1F90))
nmap -sT -O 127.0.0.1
-sS
:SYN半開(kāi)掃描(需要root權(quán)限)-p 1-65535
:全端口范圍檢測(cè)--open
:僅顯示開(kāi)放端口sudo ss -tlnp | grep ':80\b'
\b
確保精確匹配80端口docker ps --format "{{.Ports}}" | awk -F'->' '{print $1}'
sudo lsof -i -P -n | grep -v 'docker-proxy'
sudo netstat -atnp | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c
watch -n 1 'ss -o state time-wait | wc -l'
tcp_max_tw_buckets
kubectl get pods -o json | jq '.items[].spec.containers[].ports[]'
nsenter -t <PID> -n ss -ltn
#!/bin/bash
PORT=8080
while true; do
if ss -tln | grep -q ":$PORT"; then
echo "$(date): Port $PORT is occupied" >> port_monitor.log
break
fi
sleep 10
done
perf record -e syscalls:sys_enter_accept -a
perf script | flamegraph.pl > port_accept.svg
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_accept { printf("%s %d\n", comm, args->fd); }'
Q1: 為什么netstat
顯示127.0.0.1:8080和:::8080兩種監(jiān)聽(tīng)?
A: 分別表示IPv4和IPv6協(xié)議棧的監(jiān)聽(tīng),可通過(guò)sysctl net.ipv6.bindv6only=1
調(diào)整綁定策略。
Q2: TIME_WAIT狀態(tài)是否影響服務(wù)重啟?
A: 是,可通過(guò)sysctl net.ipv4.tcp_tw_reuse=1
加速端口復(fù)用。
Q3: 如何永久保存端口監(jiān)控配置?
A: 使用systemd創(chuàng)建定時(shí)器單元或配置crontab任務(wù)。
工具 | 速度 | 信息詳細(xì)度 | 易用性 | 適用場(chǎng)景 |
netstat | 中 | 高 | 優(yōu) | 兼容舊系統(tǒng) |
ss | 快 | 高 | 良 | 現(xiàn)代Linux性能分析 |
lsof | 慢 | 極高 | 中 | 進(jìn)程級(jí)深度排查 |
nmap | 慢 | 中 | 中 | 安全審計(jì)與全端口掃描 |
掌握這些工具的組合使用,可覆蓋從基礎(chǔ)運(yùn)維到性能調(diào)優(yōu)的全場(chǎng)景需求。建議在日常工作中優(yōu)先使用ss
+lsof
組合,在復(fù)雜網(wǎng)絡(luò)問(wèn)題中引入eBPF等高級(jí)技術(shù),構(gòu)建多層次的端口監(jiān)控體系。
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)