当前位置:首页>Linux>Linux 网络接口与路由高级配置实战

Linux 网络接口与路由高级配置实战

  • 2026-10-11 05:38:33
Linux 网络接口与路由高级配置实战

Linux 网络接口与路由高级配置实战

一、网络接口管理基础回顾与现代化工具

传统工具 ifconfig、route 已逐步被 ip(iproute2)取代。ip 命令功能更强大,支持命名空间、更多统计信息。

基础命令速查:

# 查看所有接口详细信息
ip -s -c addr show    # -c 彩色输出

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

# 修改 MTU
ip link set eth0 mtu 9000

# 查看链路状态(速度、双工等)
ethtool eth0

多网卡环境常见场景:

  • 内网 + 外网分离
  • 管理口 + 业务口
  • 高可用 Bond + VLAN

永久配置方式对比:

  1. NetworkManager(桌面/简单服务器):nmcli
  2. systemd-networkd(推荐服务器):.network 文件
  3. 传统 ifcfg(RHEL 系):/etc/sysconfig/network-scripts/
  4. Netplan(Ubuntu):/etc/netplan/

示例:nmcli 配置静态 IP:

nmcli con add type ethernet ifname eth0 con-name eth0 ipv4.method manual ipv4.addresses 192.168.10.50/24 ipv4.gateway 192.168.10.1 ipv4.dns 8.8.8.8
nmcli con up eth0

systemd-networkd 示例(/etc/systemd/network/10-eth0.network):

[Match]
Name=eth0

[Network]
Address=192.168.10.50/24
Gateway=192.168.10.1
DNS=8.8.8.8

重载:systemctl restart systemd-networkd

二、Link Aggregation(Bonding/Teaming)高可用配置

生产服务器通常采用 Bond 实现链路冗余与负载均衡。

Bonding 模式(常见):

  • Mode 1 (Active-Backup):主备切换
  • Mode 4 (802.3ad LACP):动态负载均衡(推荐)
  • Mode 0 (Round-Robin)、Mode 6 (Balance-ALB) 等

配置步骤(以 Mode 4 为例):

  1. 加载模块:

    modprobe bonding
  2. 创建 Bond 接口:

    ip link add bond0 type bond
    ip link set bond0 type bond mode 802.3ad miimon 100
  3. 绑定从接口:

    ip link set eth0 down
    ip link set eth0 master bond0
    ip link set eth1 down
    ip link set eth1 master bond0
    ip link set bond0 up

永久配置(ifcfg 方式):

  • ifcfg-bond0:

    DEVICE=bond0
    TYPE=Bond
    BONDING_MASTER=yes
    BONDING_OPTS="mode=4 miimon=100"
    BOOTPROTO=none
    IPADDR=10.0.0.50
    PREFIX=24
    GATEWAY=10.0.0.1
  • ifcfg-eth0 / eth1:MASTER=bond0、SLAVE=yes

验证:

cat /proc/net/bonding/bond0
ethtool bond0

故障案例:某机房交换机 LACP 未配置,导致 Bond 接口 DOWN。排查:交换机侧开启 LACP,Linux 侧确认 lacp_rate fast。

Teaming(RHEL 新特性):更现代,支持 runner 模式,配置类似。

三、VLAN 配置与子接口管理

VLAN(802.1Q)实现逻辑隔离,提升安全与广播控制。

创建 VLAN 子接口:

ip link add link eth0 name eth0.10 type vlan id 10
ip addr add 192.168.10.50/24 dev eth0.10
ip link set eth0.10 up

永久配置: Netplan 示例:

network:
version:2
vlans:
vlan10:
id:10
link:eth0
addresses:[192.168.10.50/24]

802.1ad QinQ:双层 VLAN,用于运营商场景。

运维注意:

  • MTU:VLAN 通常需减 4 字节(1496)。
  • 交换机端口设 Trunk 模式。
  • 防火墙规则需匹配子接口。

四、路由表与 ip route 高级操作

Linux 支持多个路由表(默认 main、local、default 等)。

查看路由:

ip route show table main
ip route show table all

添加路由:

# 默认路由
ip route add default via 192.168.1.1 dev eth0

# 静态路由
ip route add 10.0.0.0/8 via 192.168.1.254

# 主机路由
ip route add 8.8.8.8 via 192.168.1.1

多路由表 + 策略路由(Policy Routing): 适用于多线路(电信 + 联通)分流场景。

步骤:

  1. 定义路由表(/etc/iproute2/rt_tables):

    100 telecom
    200 unicom
  2. 添加路由:

    ip route add default via 1.1.1.1 table telecom
    ip route add default via 2.2.2.2 table unicom
  3. 策略规则(基于源 IP 或标记):

    ip rule add from 192.168.10.0/24 table telecom
    ip rule add fwmark 1 table unicom   # 配合 iptables mark

iptables 标记示例:

iptables -t mangle -A PREROUTING -s 192.168.20.0/24 -j MARK --set-mark 1

动态路由:Quagga/FRR 实现 BGP/OSPF(大型数据中心常用)。

# 安装 FRR
apt install frr   # 或 yum
vtysh
configure terminal
router bgp 65001
neighbor 192.168.1.2 remote-as 65002

五、IP 隧道与高级网络虚拟化

  • GRE/IPIP 隧道:

    ip tunnel add gre1 mode gre remote 203.0.113.1 local 198.51.100.1
    ip addr add 10.0.1.1/30 dev gre1
  • VXLAN:容器/云常用(K8s Calico/Flannel 底层):

    ip link add vxlan0 type vxlan id 100 dstport 4789 remote 192.168.1.100
  • Network Namespace:隔离测试/多租户:

    ip netns add testns
    ip netns exec testns ip link set lo up

六、常见故障排查与性能优化

接口问题:

  • Link DOWN:dmesg | grep eth、ethtool 检查协商。
  • 速度不符:强制 ethtool -s eth0 speed 1000 duplex full autoneg off。
  • 丢包:ip -s link 查看 RX/TX errors,检查网线/交换机。

路由问题:

  • 路由冲突:ip route get 8.8.8.8 查看实际路径。
  • 黑洞路由:误删默认路由导致失联(控制台救援)。
  • 多路径:ip route show cache 查看缓存。

性能优化:

  • 关闭 IPv6(若不使用):sysctl net.ipv6.conf.all.disable_ipv6=1
  • 调整 ARP 缓存:net.ipv4.neigh.default.gc_thresh1
  • Offload:ethtool -K eth0 gro on gso on tso on
  • Interrupt Coalescing:降低 CPU 中断。

监控命令:

watch -n 1 "ip -s link"
ss -m   # 显示内存使用
cat /proc/net/dev

生产案例: 某双线服务器流量不均衡,排查发现策略路由 rule 优先级错误(数字越小优先级越高)。解决:调整 ip rule 顺序并 flush cache:ip route flush cache。

七、最佳实践与自动化

  1. 标准化配置:使用 Ansible 模板统一部署 network configs。
  2. 备份:ip addr > backup-$(date +%F).txt
  3. 监控:Prometheus node_exporter + Grafana 监控接口流量/错误。
  4. 零信任:结合 nftables 限制接口流量。
  5. 云混合:了解 ENI(弹性网卡)、Secondary IP 等与本地差异。
  6. 测试:Always 在非生产环境验证路由变更。

迁移到 nftables 建议:iptables 兼容层存在,但未来趋势是 nftables。

nft add table ip filter
nft add chain ip filter input { type filter hook input priority 0 \; policy accept \; }

最新文章

随机文章