跳到主要内容

网络命令

Linux 提供了丰富的网络命令,用于网络配置、诊断和故障排除。

网络配置

ip 命令

ip 命令是现代 Linux 的网络配置工具,替代了旧的 ifconfig

# 查看所有网络接口
ip addr
ip a

# 查看指定接口
ip addr show eth0

# 查看网络接口统计
ip -s link

# 启用/禁用接口
ip link set eth0 up
ip link set eth0 down

# 添加 IP 地址
ip addr add 192.168.1.100/24 dev eth0

# 删除 IP 地址
ip addr del 192.168.1.100/24 dev eth0

# 查看路由表
ip route
ip r

# 添加默认路由
ip route add default via 192.168.1.1

# 添加静态路由
ip route add 10.0.0.0/8 via 192.168.1.1

ifconfig 命令

ifconfig 是传统的网络配置工具:

# 查看所有接口
ifconfig

# 查看指定接口
ifconfig eth0

# 启用接口
ifconfig eth0 up

# 禁用接口
ifconfig eth0 down

# 配置 IP 地址
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

网络接口配置文件

不同发行版的配置文件位置不同:

Ubuntu/Debian/etc/netplan/*.yaml

network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4

CentOS/RHEL/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes

网络诊断

ping - 测试连通性

# 基本用法
ping google.com

# 指定次数
ping -c 4 google.com

# 指定间隔
ping -i 2 google.com

# 设置超时
ping -w 5 google.com

# 指定包大小
ping -s 1000 google.com

traceroute - 追踪路由

# 追踪到目标的路由
traceroute google.com

# 使用 ICMP 协议
traceroute -I google.com

# 指定最大跳数
traceroute -m 30 google.com

tracepath - 路径发现

# 类似 traceroute,不需要 root 权限
tracepath google.com

mtr - 网络诊断工具

mtr 结合了 ping 和 traceroute 功能:

# 基本用法
mtr google.com

# 报告模式
mtr -r google.com

# 指定次数
mtr -c 10 google.com

netstat - 网络统计

# 查看所有连接
netstat -a

# 查看 TCP 连接
netstat -t

# 查看 UDP 连接
netstat -u

# 查看监听端口
netstat -l

# 查看端口号(数字形式)
netstat -n

# 查看进程信息
netstat -p

# 常用组合
netstat -tlnp # 查看监听的 TCP 端口和进程
netstat -tuln # 查看所有监听端口
netstat -an # 查看所有连接

ss - socket 统计

ssnetstat 的现代替代品:

# 查看所有 socket
ss -a

# 查看 TCP socket
ss -t

# 查看 UDP socket
ss -u

# 查看监听端口
ss -l

# 查看进程信息
ss -p

# 常用组合
ss -tlnp # 查看监听的 TCP 端口
ss -tuln # 查看所有监听端口
ss -s # 显示统计摘要

lsof - 查看打开的文件

# 查看端口占用
lsof -i :80

# 查看指定用户打开的文件
lsof -u username

# 查看指定进程打开的文件
lsof -p PID

# 查看所有网络连接
lsof -i

DNS 工具

dig - DNS 查询

# 基本查询
dig google.com

# 查询指定记录类型
dig google.com A
dig google.com MX
dig google.com NS
dig google.com TXT

# 使用指定 DNS 服务器
dig @8.8.8.8 google.com

# 简短输出
dig +short google.com

# 显示 DNS 解析过程
dig +trace google.com

nslookup - DNS 查询

# 基本查询
nslookup google.com

# 使用指定 DNS 服务器
nslookup google.com 8.8.8.8

# 交互模式
nslookup
> set type=MX
> google.com

host - DNS 查询

# 基本查询
host google.com

# 查询指定记录
host -t MX google.com
host -t NS google.com

网络测试

curl - 数据传输

# 获取网页
curl https://example.com

# 保存到文件
curl -o file.html https://example.com

# 显示响应头
curl -I https://example.com

# 发送 POST 请求
curl -X POST -d "key=value" https://example.com/api

# 发送 JSON 数据
curl -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api

# 带认证
curl -u user:pass https://example.com

# 下载文件
curl -O https://example.com/file.zip

# 跟随重定向
curl -L https://example.com

# 设置超时
curl --connect-timeout 10 https://example.com

wget - 文件下载

# 下载文件
wget https://example.com/file.zip

# 指定文件名
wget -O myfile.zip https://example.com/file.zip

# 后台下载
wget -b https://example.com/file.zip

# 断点续传
wget -c https://example.com/file.zip

# 递归下载网站
wget -r https://example.com

# 限制下载速度
wget --limit-rate=1m https://example.com/file.zip

nc (netcat) - 网络工具

# 测试端口连通性
nc -zv example.com 80

# 扫描端口范围
nc -zv example.com 20-100

# 监听端口
nc -l 8080

# 传输文件
# 接收端
nc -l 8080 > received_file
# 发送端
nc destination_ip 8080 < file_to_send

# 简单聊天服务器
# 服务端
nc -l 8080
# 客户端
nc server_ip 8080

telnet - 远程连接

# 测试端口
telnet example.com 80

# 手动发送 HTTP 请求
telnet example.com 80
GET / HTTP/1.1
Host: example.com

nmap - 端口扫描

# 扫描常用端口
nmap example.com

# 扫描所有端口
nmap -p- example.com

# 扫描指定端口
nmap -p 80,443,22 example.com

# 扫描网段
nmap 192.168.1.0/24

# 操作系统检测
nmap -O example.com

# 服务版本检测
nmap -sV example.com

防火墙

ufw (Ubuntu)

# 查看状态
ufw status

# 启用防火墙
ufw enable

# 禁用防火墙
ufw disable

# 允许端口
ufw allow 80
ufw allow 443
ufw allow 22

# 拒绝端口
ufw deny 8080

# 允许特定 IP
ufw allow from 192.168.1.100

# 允许特定 IP 访问特定端口
ufw allow from 192.168.1.100 to any port 22

# 删除规则
ufw delete allow 80

# 重置规则
ufw reset

firewalld (CentOS/RHEL)

# 查看状态
systemctl status firewalld

# 启动/停止
systemctl start firewalld
systemctl stop firewalld

# 查看所有规则
firewall-cmd --list-all

# 开放端口
firewall-cmd --add-port=80/tcp
firewall-cmd --add-port=80/tcp --permanent # 永久生效

# 移除端口
firewall-cmd --remove-port=80/tcp

# 开放服务
firewall-cmd --add-service=http
firewall-cmd --add-service=https --permanent

# 重载配置
firewall-cmd --reload

iptables

# 查看规则
iptables -L -n

# 允许 SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许 HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 拒绝其他入站
iptables -A INPUT -j DROP

# 保存规则
iptables-save > /etc/iptables/rules.v4

SSH 远程连接

基本连接

# 连接远程主机
ssh user@hostname

# 指定端口
ssh -p 2222 user@hostname

# 使用密钥
ssh -i ~/.ssh/id_rsa user@hostname

SSH 密钥管理

# 生成密钥对
ssh-keygen -t rsa -b 4096
ssh-keygen -t ed25519 # 推荐使用 ed25519

# 复制公钥到远程主机
ssh-copy-id user@hostname

# 手动复制公钥
cat ~/.ssh/id_rsa.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

SSH 配置文件

~/.ssh/config:

Host myserver
HostName 192.168.1.100
User username
Port 22
IdentityFile ~/.ssh/id_rsa

Host alias
HostName example.com
User admin

SCP 文件传输

# 上传文件
scp local_file user@host:/remote/path

# 下载文件
scp user@host:/remote/file local_path

# 上传目录
scp -r local_dir user@host:/remote/path

# 指定端口
scp -P 2222 file user@host:/path

rsync 同步

# 同步目录
rsync -avz /local/dir/ user@host:/remote/dir/

# 显示进度
rsync -avz --progress /local/dir/ user@host:/remote/dir/

# 排除文件
rsync -avz --exclude '*.log' /local/dir/ user@host:/remote/dir/

# 删除目标多余文件
rsync -avz --delete /local/dir/ user@host:/remote/dir/

# 断点续传
rsync -avz --partial --progress /local/dir/ user@host:/remote/dir/

网络故障排除流程

  1. 检查物理连接

    ip link show
  2. 检查 IP 配置

    ip addr show
  3. 检查路由

    ip route show
  4. 测试本地回环

    ping 127.0.0.1
  5. 测试网关

    ping 192.168.1.1
  6. 测试外网 IP

    ping 8.8.8.8
  7. 测试 DNS

    ping google.com
    dig google.com
  8. 检查防火墙

    ufw status
    iptables -L -n

小结

本章我们学习了:

  1. 网络配置:ip、ifconfig
  2. 网络诊断:ping、traceroute、mtr、netstat、ss
  3. DNS 工具:dig、nslookup、host
  4. 网络测试:curl、wget、nc、nmap
  5. 防火墙:ufw、firewalld、iptables
  6. SSH 远程:ssh、scp、rsync

练习

  1. 查看本机的 IP 地址和网络接口状态
  2. 使用 ping 和 traceroute 诊断网络连通性
  3. 查看系统开放的端口和对应的进程
  4. 配置 SSH 密钥登录远程服务器