关于 network-tools
面向管理员和开发者的 Linux 网络诊断技能,涵盖网络配置、连通性、端口、HTTP、抓包和性能排查,并介绍常用命令行工具。
主要功能
- 01查看 IP 地址和路由
- 02测试连通性与 DNS
- 03检查端口和连接
- 04发送 HTTP 请求与下载
- 05抓取和监控流量
- 06排查网络性能
适用场景
- 01查找占用某端口的进程
- 02排查主机或服务无法访问
- 03追踪路由并调查超时
- 04检查流量和带宽使用
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ chaterm/ terminal-skills --skill network-tools安装后在 Claude Code 中输入/network-tools确认已加载。
详细介绍
网络工具与诊断
概述
Linux 网络诊断、端口扫描、流量分析及其他工具使用技巧。
网络配置
查看配置
# IP address
ip addr
ip a
ifconfig # Legacy command
# Routing table
ip route
route -n
netstat -rn
# DNS configuration
cat /etc/resolv.conf
systemd-resolve --status
配置网络
# Temporary IP configuration
ip addr add 192.168.1.100/24 dev eth0
ip addr del 192.168.1.100/24 dev eth0
# Enable/Disable interface
ip link set eth0 up
ip link set eth0 down
# Add route
ip route add 10.0.0.0/8 via 192.168.1.1
ip route del 10.0.0.0/8
连通性测试
ping
ping hostname
ping -c 4 hostname # Send 4 packets
ping -i 0.2 hostname # 0.2 second interval
ping -s 1000 hostname # Specify packet size
traceroute
traceroute hostname
traceroute -n hostname # Don't resolve hostnames
traceroute -T hostname # Use TCP
mtr hostname # Real-time trace
DNS 查询
nslookup hostname
dig hostname
dig +short hostname
dig @8.8.8.8 hostname # Specify DNS server
host hostname
端口与连接
ss 命令(推荐)
# Listening ports
ss -tlnp # TCP listening
ss -ulnp # UDP listening
ss -tlnp | grep :80
# All connections
ss -tanp # TCP connections
ss -s # Statistics
# Filter
ss -t state established
ss -t dst 192.168.1.1
ss -t sport = :80
netstat 命令
netstat -tlnp # TCP listening
netstat -ulnp # UDP listening
netstat -anp # All connections
netstat -s # Statistics
lsof 网络
lsof -i # All network connections
lsof -i :80 # Specific port
lsof -i tcp # TCP connections
lsof -i @192.168.1.1 # Specific host
HTTP 工具
curl
# Basic request
curl http://example.com
curl -I http://example.com # Headers only
curl -v http://example.com # Verbose output
# POST request
curl -X POST -d "data=value" http://example.com
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com
# Download
curl -O http://example.com/file.zip
curl -o output.zip http://example.com/file.zip
# Authentication
curl -u user:pass http://example.com
curl -H "Authorization: Bearer token" http://example.com
wget
wget http://example.com/file.zip
wget -c http://example.com/file.zip # Resume download
wget -r http://example.com # Recursive download
wget --mirror http://example.com # Mirror site
数据包捕获
tcpdump
# Basic capture
tcpdump -i eth0
tcpdump -i any
# Filter
tcpdump -i eth0 port 80
tcpdump -i eth0 host 192.168.1.1
tcpdump -i eth0 'tcp port 80 and host 192.168.1.1'
# Save/Read
tcpdump -i eth0 -w capture.pcap
tcpdump -r capture.pcap
# Display content
tcpdump -i eth0 -A port 80 # ASCII
tcpdump -i eth0 -X port 80 # Hexadecimal
流量监控
# Real-time traffic
iftop
iftop -i eth0
# By process
nethogs
nethogs eth0
# Bandwidth test
iperf3 -s # Server
iperf3 -c server_ip # Client
常见场景
场景 1:排查端口占用
# Check port usage
ss -tlnp | grep :8080
lsof -i :8080
# Find process and handle
kill -9 PID
# Or
fuser -k 8080/tcp
场景 2:测试服务连通性
# TCP port test
nc -zv hostname 80
telnet hostname 80
# HTTP service test
curl -I http://hostname
curl -w "HTTP Code: %{http_code}\nTime: %{time_total}s\n" -o /dev/null -s http://hostname
场景 3:网络性能诊断
# Latency test
ping -c 100 hostname | tail -1
# Route analysis
mtr --report hostname
# Bandwidth test
iperf3 -c server -t 30
故障排查
| 问题 | 解决方案 |
|---|---|
| 网络不可达 | ping、traceroute,检查路由 |
| DNS 解析失败 | dig、nslookup,检查 resolv.conf |
| 端口不可达 | ss -tlnp,检查防火墙 |
| 连接超时 | 使用 mtr、tcpdump 捕获数据包 |
| 带宽不足 | 使用 iftop、iperf3 测试 |
评论
支持 Markdown