插件窝 干货文章 Linux系统中的网络拓扑结构及配置指南

Linux系统中的网络拓扑结构及配置指南

网络 接口 拓扑 配置 151    来源:    2025-04-18

Linux系统网络拓扑结构及配置指南

一、Linux网络拓扑基础

1. 常见网络拓扑类型

  • 星型拓扑:所有设备连接到一个中心节点(如交换机)
  • 总线型拓扑:所有设备共享一条通信线路(已较少使用)
  • 环型拓扑:设备形成闭合环(如令牌环网络)
  • 网状拓扑:设备间有多条连接路径(高可用性网络)
  • 混合拓扑:结合以上多种类型的复杂网络

2. Linux网络接口类型

  • 物理接口:eth0, enpXsY (以太网接口)
  • 虚拟接口:lo (回环接口), tun/tap (VPN接口)
  • 桥接接口:br0 (用于虚拟化环境)
  • 绑定接口:bond0 (网卡绑定/聚合)

二、网络配置工具

1. 传统配置工具

# ifconfig (已逐渐被淘汰)
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

# route
route add default gw 192.168.1.1

# 网络服务控制
service network restart   # RHEL/CentOS 6
/etc/init.d/networking restart  # Debian/Ubuntu

2. iproute2工具集(推荐)

# 查看所有接口
ip link show

# 配置IP地址
ip addr add 192.168.1.100/24 dev eth0

# 设置默认网关
ip route add default via 192.168.1.1

# 查看路由表
ip route show

3. NetworkManager(桌面环境常用)

# 基本命令
nmcli device status
nmcli connection show

# 配置静态IP
nmcli con add con-name "static-eth0" ifname eth0 type ethernet ip4 192.168.1.100/24 gw4 192.168.1.1

三、网络配置文件

1. Debian/Ubuntu系统

/etc/network/interfaces:

auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

2. RHEL/CentOS系统

/etc/sysconfig/network-scripts/ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

3. DNS配置

/etc/resolv.conf:

nameserver 8.8.8.8
nameserver 8.8.4.4

四、高级网络配置

1. 网卡绑定(链路聚合)

# 安装必要工具(RHEL/CentOS)
yum install -y teamd

# 创建绑定接口
nmcli con add type team con-name team0 ifname team0 config '{"runner": {"name": "activebackup"}}'

# 添加从属接口
nmcli con add type team-slave con-name team0-port1 ifname eth0 master team0
nmcli con add type team-slave con-name team0-port2 ifname eth1 master team0

2. 网络桥接

# 创建桥接接口
brctl addbr br0
brctl addif br0 eth0

# 使用iproute2创建桥接
ip link add name br0 type bridge
ip link set eth0 master br0

3. VLAN配置

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

# 配置VLAN IP
ip addr add 192.168.100.1/24 dev eth0.100

五、网络诊断工具

1. 基本诊断命令

# 测试连通性
ping 8.8.8.8

# 跟踪路由路径
traceroute google.com
tracepath google.com

# 网络扫描
nmap -sP 192.168.1.0/24

# DNS查询
dig google.com
nslookup google.com

2. 高级诊断工具

# 网络流量分析
tcpdump -i eth0 -n

# 带宽测试
iperf3 -s  # 服务器端
iperf3 -c server_ip  # 客户端

# 网络统计
ss -tulnp  # 查看监听端口
netstat -s  # 网络统计信息

六、防火墙配置

1. iptables基础

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

# 默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 保存规则(RHEL/CentOS)
service iptables save

2. firewalld(推荐)

# 基本命令
firewall-cmd --state
firewall-cmd --list-all

# 开放端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

七、常见问题排查

  1. 网络接口不工作

    • 检查物理连接
    • ip link show 查看接口状态
    • ethtool eth0 检查网卡参数
  2. 无法访问外部网络

    • 检查默认网关 ip route show
    • 测试DNS解析 dig google.com
    • 检查防火墙规则
  3. 网络速度慢

    • 使用 ethtool 检查双工模式和速度
    • 使用 iperf3 测试带宽
    • 检查是否有丢包 ip -s link show eth0
  4. 服务无法访问

    • 检查服务是否监听 ss -tulnp | grep service_port
    • 检查防火墙规则
    • 检查SELinux状态 getenforce

通过掌握这些网络拓扑结构和配置方法,您将能够有效地在Linux系统中设置和管理各种网络环境。