网络高级 #

一、网络命名空间 #

1.1 基本概念 #

网络命名空间(Network Namespace)是 Linux 提供的网络隔离机制,每个命名空间有独立的网络栈。

1.2 命名空间管理 #

bash
# 创建命名空间
sudo ip netns add ns1

# 列出所有命名空间
sudo ip netns list

# 删除命名空间
sudo ip netns delete ns1

# 在命名空间中执行命令
sudo ip netns exec ns1 ip addr

# 进入命名空间
sudo ip netns exec ns1 bash

# 查看命名空间中的进程
sudo ip netns pids ns1

1.3 虚拟网络设备 #

bash
# 创建 veth pair
sudo ip link add veth0 type veth peer name veth1

# 将 veth1 移到命名空间
sudo ip link set veth1 netns ns1

# 配置 IP 地址
sudo ip addr add 192.168.1.1/24 dev veth0
sudo ip netns exec ns1 ip addr add 192.168.1.2/24 dev veth1

# 启用接口
sudo ip link set veth0 up
sudo ip netns exec ns1 ip link set veth1 up

# 测试连通性
sudo ip netns exec ns1 ping 192.168.1.1

二、网桥配置 #

2.1 网桥基础 #

网桥(Bridge)是二层网络设备,用于连接多个网络接口。

bash
# 创建网桥
sudo ip link add br0 type bridge

# 查看网桥
sudo ip link show br0

# 删除网桥
sudo ip link delete br0

# 启用网桥
sudo ip link set br0 up

2.2 网桥管理 #

bash
# 将接口添加到网桥
sudo ip link set eth0 master br0

# 从网桥移除接口
sudo ip link set eth0 nomaster

# 查看网桥信息
sudo bridge link

# 查看网桥 MAC 地址表
sudo bridge fdb show

# 安装 bridge-utils
sudo apt install bridge-utils

# 使用 brctl
sudo brctl addbr br0
sudo brctl addif br0 eth0
sudo brctl show

2.3 网桥配置文件 #

bash
# Ubuntu/Debian - /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
  bridges:
    br0:
      interfaces: [eth0]
      dhcp4: yes

# CentOS/RHEL - /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes

三、VLAN 配置 #

3.1 VLAN 基础 #

VLAN(虚拟局域网)用于网络隔离和分段。

bash
# 创建 VLAN 接口
sudo ip link add link eth0 name eth0.100 type vlan id 100

# 配置 IP 地址
sudo ip addr add 192.168.100.1/24 dev eth0.100

# 启用接口
sudo ip link set eth0.100 up

# 删除 VLAN
sudo ip link delete eth0.100

3.2 VLAN 配置文件 #

bash
# Ubuntu/Debian
network:
  version: 2
  vlans:
    vlan100:
      id: 100
      link: eth0
      addresses:
        - 192.168.100.1/24

# CentOS/RHEL
# /etc/sysconfig/network-scripts/ifcfg-eth0.100
DEVICE=eth0.100
VLAN=yes
BOOTPROTO=static
IPADDR=192.168.100.1
NETMASK=255.255.255.0
ONBOOT=yes

3.3 802.1Q 配置 #

bash
# 安装 vlan 工具
sudo apt install vlan

# 加载 8021q 模块
sudo modprobe 8021q

# 查看模块
lsmod | grep 8021q

# 开机自动加载
echo "8021q" | sudo tee -a /etc/modules

四、路由高级配置 #

4.1 路由表管理 #

bash
# 查看路由表
ip route show
ip route show table main
ip route show table local

# 查看所有路由表
ip route show table all

# 添加路由表
# 编辑 /etc/iproute2/rt_tables
echo "200 custom" | sudo tee -a /etc/iproute2/rt_tables

# 在自定义表中添加路由
sudo ip route add 10.0.0.0/24 via 192.168.1.1 table custom

# 查看自定义表
ip route show table custom

4.2 策略路由 #

bash
# 查看策略
ip rule show

# 基于源地址的策略
sudo ip rule add from 192.168.1.100 table custom

# 基于目标地址的策略
sudo ip rule add to 10.0.0.0/24 table custom

# 基于标记的策略
sudo ip rule add fwmark 1 table custom

# 删除策略
sudo ip rule del from 192.168.1.100

4.3 路由示例 #

bash
# 多网关配置
# 假设有两个 ISP
sudo ip route add default via 192.168.1.1 metric 100
sudo ip route add default via 192.168.2.1 metric 200

# 负载均衡
sudo ip route add default scope global nexthop via 192.168.1.1 weight 1 nexthop via 192.168.2.1 weight 1

# 源地址路由
sudo ip route add 192.168.10.0/24 dev eth0 src 192.168.10.1

五、网络绑定 #

5.1 Bonding 模式 #

模式 名称 说明
0 balance-rr 轮询负载均衡
1 active-backup 主备模式
2 balance-xor XOR 负载均衡
3 broadcast 广播模式
4 802.3ad LACP 动态聚合
5 balance-tlb 发送负载均衡
6 balance-alb 自适应负载均衡

5.2 配置 Bonding #

bash
# 加载 bonding 模块
sudo modprobe bonding

# 创建 bond 接口
sudo ip link add bond0 type bond mode 802.3ad

# 添加接口到 bond
sudo ip link set eth0 master bond0
sudo ip link set eth1 master bond0

# 配置 IP
sudo ip addr add 192.168.1.100/24 dev bond0
sudo ip link set bond0 up

5.3 Bonding 配置文件 #

bash
# Ubuntu/Debian
network:
  version: 2
  ethernets:
    eth0: {}
    eth1: {}
  bonds:
    bond0:
      interfaces: [eth0, eth1]
      mode: 802.3ad
      addresses:
        - 192.168.1.100/24

# CentOS/RHEL
# /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
ONBOOT=yes
BONDING_OPTS="mode=802.3ad miimon=100"

六、VPN 配置 #

6.1 OpenVPN #

bash
# 安装 OpenVPN
sudo apt install openvpn

# 生成密钥
openvpn --genkey secret /etc/openvpn/static.key

# 服务器配置
# /etc/openvpn/server.conf
dev tun
ifconfig 10.8.0.1 10.8.0.2
secret static.key
keepalive 10 60
persist-key
persist-tun

# 客户端配置
# /etc/openvpn/client.conf
remote server.example.com
dev tun
ifconfig 10.8.0.2 10.8.0.1
secret static.key

# 启动服务
sudo systemctl start openvpn@server
sudo systemctl start openvpn@client

6.2 WireGuard #

bash
# 安装 WireGuard
sudo apt install wireguard

# 生成密钥
wg genkey | tee privatekey | wg pubkey > publickey

# 服务器配置
# /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
ListenPort = 51820
PrivateKey = <server_private_key>

[Peer]
PublicKey = <client_public_key>
AllowedIPs = 10.0.0.2/32

# 客户端配置
[Interface]
Address = 10.0.0.2/24
PrivateKey = <client_private_key>

[Peer]
PublicKey = <server_public_key>
Endpoint = server.example.com:51820
AllowedIPs = 0.0.0.0/0

# 启动服务
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0

七、网络故障排查 #

7.1 连通性测试 #

bash
# ping 测试
ping -c 5 google.com
ping -I eth0 google.com

# 路由跟踪
traceroute google.com
mtr google.com

# DNS 解析
nslookup google.com
dig google.com
host google.com

# 端口测试
nc -zv google.com 80
telnet google.com 80

7.2 抓包分析 #

bash
# tcpdump 抓包
sudo tcpdump -i eth0
sudo tcpdump -i eth0 port 80
sudo tcpdump -i eth0 host 192.168.1.100
sudo tcpdump -i eth0 -w capture.pcap

# Wireshark 分析
sudo apt install wireshark
wireshark capture.pcap

# tshark 命令行
sudo apt install tshark
tshark -i eth0
tshark -i eth0 -f "port 80"

7.3 网络统计 #

bash
# 接口统计
ip -s link show eth0

# 网络连接
ss -tunap
netstat -tunap

# 路由表
ip route show
route -n

# ARP 表
ip neigh show
arp -a

八、网络性能优化 #

8.1 TCP 参数调优 #

bash
# 查看当前参数
sysctl net.ipv4.tcp_congestion_control
sysctl net.core.somaxconn

# 编辑配置文件
sudo vim /etc/sysctl.conf

# 常用参数
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_congestion_control = bbr

# 应用配置
sudo sysctl -p

8.2 网卡优化 #

bash
# 查看网卡参数
ethtool eth0

# 查看速度和双工
ethtool eth0 | grep -i speed

# 设置速度和双工
sudo ethtool -s eth0 speed 1000 duplex full autoneg off

# 查看中断
cat /proc/interrupts | grep eth0

# 设置多队列
sudo ethtool -L eth0 combined 4

# 查看 RSS
sudo ethtool -x eth0

8.3 网络性能测试 #

bash
# 安装 iperf3
sudo apt install iperf3

# 服务端
iperf3 -s

# 客户端
iperf3 -c server_ip

# 多线程测试
iperf3 -c server_ip -P 10

# UDP 测试
iperf3 -c server_ip -u -b 1G

# 安装 netperf
sudo apt install netperf

# 服务端
netserver

# 客户端
netperf -H server_ip

九、实践练习 #

9.1 练习一:命名空间 #

bash
# 1. 创建命名空间
sudo ip netns add test_ns

# 2. 创建 veth pair
sudo ip link add veth0 type veth peer name veth1

# 3. 移动 veth1 到命名空间
sudo ip link set veth1 netns test_ns

# 4. 配置 IP
sudo ip addr add 192.168.100.1/24 dev veth0
sudo ip netns exec test_ns ip addr add 192.168.100.2/24 dev veth1

# 5. 启用并测试
sudo ip link set veth0 up
sudo ip netns exec test_ns ip link set veth1 up
sudo ip netns exec test_ns ping 192.168.100.1

9.2 练习二:网桥配置 #

bash
# 1. 创建网桥
sudo ip link add br0 type bridge

# 2. 启用网桥
sudo ip link set br0 up

# 3. 配置 IP
sudo ip addr add 192.168.200.1/24 dev br0

# 4. 查看网桥
sudo ip link show br0

9.3 练习三:VPN 配置 #

bash
# 1. 安装 WireGuard
sudo apt install wireguard

# 2. 生成密钥
wg genkey | tee privatekey | wg pubkey > publickey

# 3. 创建配置
sudo vim /etc/wireguard/wg0.conf

# 4. 启动服务
sudo wg-quick up wg0

# 5. 查看状态
sudo wg show

十、小结 #

本章学习了 Linux 高级网络配置,包括命名空间、网桥、VLAN、VPN 等技术。

关键要点:

  1. 网络命名空间提供网络隔离
  2. 网桥用于连接多个网络接口
  3. VLAN 实现网络分段
  4. 策略路由实现灵活的路由控制
  5. VPN 提供安全的远程连接

下一章预告: 系统安全 - 学习 Linux 系统安全配置和管理。

最后更新:2026-03-27