network-tools

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Network Tools and Diagnostics

网络工具与诊断

Overview

概述

Linux network diagnostics, port scanning, traffic analysis and other tool usage skills.
Linux网络诊断、端口扫描、流量分析等工具的使用技巧。

Network Configuration

网络配置

View Configuration

查看配置

bash
undefined
bash
undefined

IP address

IP地址

ip addr ip a ifconfig # Legacy command
ip addr ip a ifconfig # 旧版命令

Routing table

路由表

ip route route -n netstat -rn
ip route route -n netstat -rn

DNS configuration

DNS配置

cat /etc/resolv.conf systemd-resolve --status
undefined
cat /etc/resolv.conf systemd-resolve --status
undefined

Configure Network

配置网络

bash
undefined
bash
undefined

Temporary IP configuration

临时IP配置

ip addr add 192.168.1.100/24 dev eth0 ip addr del 192.168.1.100/24 dev eth0
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
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
undefined
ip route add 10.0.0.0/8 via 192.168.1.1 ip route del 10.0.0.0/8
undefined

Connectivity Testing

连通性测试

ping

ping

bash
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
bash
ping hostname
ping -c 4 hostname                  # 发送4个数据包
ping -i 0.2 hostname                # 间隔0.2秒
ping -s 1000 hostname               # 指定数据包大小

traceroute

traceroute

bash
traceroute hostname
traceroute -n hostname              # Don't resolve hostnames
traceroute -T hostname              # Use TCP
mtr hostname                        # Real-time trace
bash
traceroute hostname
traceroute -n hostname              # 不解析主机名
traceroute -T hostname              # 使用TCP协议
mtr hostname                        # 实时追踪

DNS Query

DNS查询

bash
nslookup hostname
dig hostname
dig +short hostname
dig @8.8.8.8 hostname               # Specify DNS server
host hostname
bash
nslookup hostname
dig hostname
dig +short hostname
dig @8.8.8.8 hostname               # 指定DNS服务器
host hostname

Ports and Connections

端口与连接

ss Command (Recommended)

ss命令(推荐)

bash
undefined
bash
undefined

Listening ports

监听端口

ss -tlnp # TCP listening ss -ulnp # UDP listening ss -tlnp | grep :80
ss -tlnp # TCP监听端口 ss -ulnp # UDP监听端口 ss -tlnp | grep :80

All connections

所有连接

ss -tanp # TCP connections ss -s # Statistics
ss -tanp # TCP连接 ss -s # 统计信息

Filter

过滤

ss -t state established ss -t dst 192.168.1.1 ss -t sport = :80
undefined
ss -t state established ss -t dst 192.168.1.1 ss -t sport = :80
undefined

netstat Command

netstat命令

bash
netstat -tlnp                       # TCP listening
netstat -ulnp                       # UDP listening
netstat -anp                        # All connections
netstat -s                          # Statistics
bash
netstat -tlnp                       # TCP监听端口
netstat -ulnp                       # UDP监听端口
netstat -anp                        # 所有连接
netstat -s                          # 统计信息

lsof Network

lsof网络相关

bash
lsof -i                             # All network connections
lsof -i :80                         # Specific port
lsof -i tcp                         # TCP connections
lsof -i @192.168.1.1                # Specific host
bash
lsof -i                             # 所有网络连接
lsof -i :80                         # 指定端口
lsof -i tcp                         # TCP连接
lsof -i @192.168.1.1                # 指定主机

HTTP Tools

HTTP工具

curl

curl

bash
undefined
bash
undefined

Basic request

基础请求

curl http://example.com curl -I http://example.com # Headers only curl -v http://example.com # Verbose output
curl http://example.com curl -I http://example.com # 仅获取响应头 curl -v http://example.com # 详细输出

POST request

POST请求

curl -X POST -d "data=value" http://example.com curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com
curl -X POST -d "data=value" http://example.com curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com

Download

下载文件

Authentication

身份验证

curl -u user:pass http://example.com curl -H "Authorization: Bearer token" http://example.com
undefined
curl -u user:pass http://example.com curl -H "Authorization: Bearer token" http://example.com
undefined

wget

wget

bash
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
bash
wget http://example.com/file.zip
wget -c http://example.com/file.zip # 断点续传
wget -r http://example.com          # 递归下载
wget --mirror http://example.com    # 镜像站点

Packet Capture

数据包捕获

tcpdump

tcpdump

bash
undefined
bash
undefined

Basic capture

基础捕获

tcpdump -i eth0 tcpdump -i any
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'
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
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
undefined
tcpdump -i eth0 -A port 80 # ASCII格式 tcpdump -i eth0 -X port 80 # 十六进制格式
undefined

Traffic Monitoring

流量监控

bash
undefined
bash
undefined

Real-time traffic

实时流量监控

iftop iftop -i eth0
iftop iftop -i eth0

By process

按进程统计流量

nethogs nethogs eth0
nethogs nethogs eth0

Bandwidth test

带宽测试

iperf3 -s # Server iperf3 -c server_ip # Client
undefined
iperf3 -s # 服务器端 iperf3 -c server_ip # 客户端
undefined

Common Scenarios

常见场景

Scenario 1: Troubleshoot Port Usage

场景1:排查端口占用

bash
undefined
bash
undefined

Check port usage

检查端口占用情况

ss -tlnp | grep :8080 lsof -i :8080
ss -tlnp | grep :8080 lsof -i :8080

Find process and handle

找到进程并处理

kill -9 PID
kill -9 PID

Or

或者

fuser -k 8080/tcp
undefined
fuser -k 8080/tcp
undefined

Scenario 2: Test Service Connectivity

场景2:测试服务连通性

bash
undefined
bash
undefined

TCP port test

TCP端口测试

nc -zv hostname 80 telnet hostname 80
nc -zv hostname 80 telnet hostname 80

HTTP service test

HTTP服务测试

curl -I http://hostname curl -w "HTTP Code: %{http_code}\nTime: %{time_total}s\n" -o /dev/null -s http://hostname
undefined
curl -I http://hostname curl -w "HTTP状态码: %{http_code}\n耗时: %{time_total}s\n" -o /dev/null -s http://hostname
undefined

Scenario 3: Network Performance Diagnosis

场景3:网络性能诊断

bash
undefined
bash
undefined

Latency test

延迟测试

ping -c 100 hostname | tail -1
ping -c 100 hostname | tail -1

Route analysis

路由分析

mtr --report hostname
mtr --report hostname

Bandwidth test

带宽测试

iperf3 -c server -t 30
undefined
iperf3 -c server -t 30
undefined

Troubleshooting

故障排查

ProblemSolution
Network unreachable
ping
,
traceroute
, check routing
DNS resolution failed
dig
,
nslookup
, check resolv.conf
Port unreachable
ss -tlnp
, check firewall
Connection timeout
mtr
,
tcpdump
packet capture
Insufficient bandwidth
iftop
,
iperf3
test
问题解决方案
网络不可达使用
ping
traceroute
,检查路由配置
DNS解析失败使用
dig
nslookup
,检查resolv.conf配置
端口不可访问使用
ss -tlnp
,检查防火墙设置
连接超时使用
mtr
tcpdump
捕获数据包
带宽不足使用
iftop
iperf3
进行测试