在 Linux 系统中,网络接口配置是系统管理的基础任务之一,无论是服务器部署、网络服务配置还是日常使用,都需要正确配置网络接口。
一、网络接口基础概念
1.1 网络接口命名规则
在不同 Linux 发行版中,网络接口名称可能有所不同:
- 传统命名方式:eth0, eth1, eth2 等
- 基于固件 / 拓扑的命名:enp0s3, ens33 等
- 基于设备位置的命名:enx000ec6012345
可以通过以下命令查看系统中的网络接口:
ip link show
或使用更简单的命令:
ifconfig -a
1.2 网络接口类型
常见的网络接口类型包括:
- 以太网接口:eth0, enp0s3 等
- 无线接口:wlan0, wlp2s0 等
- 虚拟接口:lo (回环接口), docker0 (Docker 虚拟接口) 等
- PPP 接口:ppp0 (用于拨号连接)
- 桥接接口:br0 (用于虚拟机网络桥接)
二、临时配置网络接口
2.1 使用 ifconfig 命令
ifconfig
是较旧的网络配置工具,但仍然被广泛使用:
# 启用网络接口
ifconfig eth0 up
# 禁用网络接口
ifconfig eth0 down
# 配置 IP 地址和子网掩码
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
# 添加默认网关
route add default gw 192.168.1.1
2.2 使用 ip 命令
ip
是更现代的网络配置工具,功能更强大:
# 启用网络接口
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 add default via 192.168.1.1
# 查看路由表
ip route show
2.3 配置 DNS
临时配置 DNS 可以修改 /etc/resolv.conf
文件:
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
三、持久化配置网络接口
3.1 Debian/Ubuntu 系统
在 Debian/Ubuntu 系统中,网络配置文件位于 /etc/network/interfaces
:
# 回环接口
auto lo
iface lo inet loopback
# 以太网接口 - DHCP 配置
auto eth0
iface eth0 inet dhcp
# 以太网接口 - 静态 IP 配置
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
配置完成后,重启网络服务:
sudo systemctl restart networking
或使用 ifdown
和 ifup
命令:
sudo ifdown eth0 && sudo ifup eth0
3.2 Red Hat/CentOS 系统
在 Red Hat/CentOS 系统中,网络配置文件位于 /etc/sysconfig/network-scripts/ifcfg-*
:
# DHCP 配置
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
NAME="System eth0"
# 静态 IP 配置
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
配置完成后,重启网络服务:
sudo systemctl restart network
3.3 使用 NetworkManager
大多数现代 Linux 系统都使用 NetworkManager 来管理网络接口:
# 查看网络连接
nmcli connection show
# 创建静态 IP 连接
sudo nmcli connection add type ethernet con-name eth0 ifname eth0 ip4 192.168.1.100/24 gw4 192.168.1.1
# 设置 DNS
sudo nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
# 应用配置
sudo nmcli connection up eth0
四、高级网络配置
4.1 VLAN 配置
VLAN (虚拟局域网) 配置需要安装 vlan
包:
# Debian/Ubuntu
sudo apt-get install vlan
# Red Hat/CentOS
sudo yum install vlan
配置 VLAN 接口:
# 创建 VLAN 接口
ip link add link eth0 name eth0.10 type vlan id 10
# 配置 VLAN 接口 IP 地址
ip addr add 192.168.10.100/24 dev eth0.10
# 启用 VLAN 接口
ip link set eth0.10 up
持久化配置(以 CentOS 为例):
# /etc/sysconfig/network-scripts/ifcfg-eth0.10
DEVICE=eth0.10
BOOTPROTO=static
ONBOOT=yes
VLAN=yes
IPADDR=192.168.10.100
NETMASK=255.255.255.0
4.2 网络桥接配置
网络桥接常用于虚拟机网络:
# 创建桥接接口
ip link add br0 type bridge
# 将物理接口加入桥接
ip link set eth0 master br0
# 配置桥接接口 IP 地址
ip addr add 192.168.1.100/24 dev br0
# 启用桥接接口
ip link set br0 up
持久化配置(以 Ubuntu 为例):
# /etc/network/interfaces
auto br0
iface br0 inet static
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
4.3 绑定 / 聚合网络接口
网络绑定(Bonding)可以提高网络带宽或提供冗余:
# 加载 bonding 模块
modprobe bonding mode=active-backup miimon=100
# 创建绑定接口
ip link add bond0 type bond mode active-backup miimon 100
# 将物理接口加入绑定
ip link set eth0 master bond0
ip link set eth1 master bond0
# 配置绑定接口 IP 地址
ip addr add 192.168.1.100/24 dev bond0
# 启用绑定接口
ip link set bond0 up
持久化配置(以 CentOS 为例):
# /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
TYPE=Bond
BONDING_MASTER=yes
BONDING_OPTS="mode=active-backup miimon=100"
IPADDR=192.168.1.100
NETMASK=255.255.255.0
# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
# /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
五、网络故障排除
5.1 基本网络测试命令
# 检查网络接口状态
ip link show
# 检查 IP 地址配置
ip addr show
# 检查路由表
ip route show
# 测试网络连通性
ping 192.168.1.1
# 测试 DNS 解析
nslookup google.com
# 跟踪路由
traceroute google.com
# 检查端口监听
ss -tulpn
5.2 常见网络问题及解决方法
-
无法连接到网络
- 检查物理连接是否正常
- 检查网络接口是否启用
- 检查 IP 地址配置是否正确
- 检查网关是否可达
-
无法解析域名
- 检查
/etc/resolv.conf
文件是否正确配置 - 检查 DNS 服务器是否可达
- 尝试使用其他 DNS 服务器
- 检查
-
网络连接不稳定
- 检查网络设备(交换机、路由器)是否正常
- 检查网络线缆是否损坏
- 检查网络接口是否有丢包现象
- 使用
mtr
命令诊断网络问题
六、防火墙配置
6.1 iptables 基础配置
# 允许 SSH 连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许 HTTP 和 HTTPS 连接
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许 ICMP (ping)
iptables -A INPUT -p icmp -j ACCEPT
# 允许已建立的和相关的连接
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# 默认拒绝所有输入流量
iptables -P INPUT DROP
# 保存 iptables 规则
# Debian/Ubuntu
iptables-save > /etc/iptables/rules.v4
# Red Hat/CentOS
service iptables save
6.2 firewalld 基础配置
# 允许 SSH 服务
firewall-cmd --permanent --add-service=ssh
# 允许 HTTP 和 HTTPS 服务
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 开放自定义端口
firewall-cmd --permanent --add-port=8080/tcp
# 重新加载防火墙规则
firewall-cmd --reload
# 查看防火墙状态
firewall-cmd --state
# 查看已开放的服务和端口
firewall-cmd --list-all
七、IPv6 配置
7.1 启用 IPv6
编辑 /etc/sysctl.conf
文件,添加或修改以下行:
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
应用配置:
sysctl -p
7.2 配置 IPv6 地址
临时配置:
ip -6 addr add 2001:db8:1::100/64 dev eth0
持久化配置(以 CentOS 为例):
# /etc/sysconfig/network-scripts/ifcfg-eth0
IPV6INIT=yes
IPV6ADDR=2001:db8:1::100/64
IPV6_DEFAULTGW=2001:db8:1::1
八、网络性能优化
8.1 调整网络参数
编辑 /etc/sysctl.conf
文件,添加或修改以下参数:
# 增加 TCP 连接队列大小
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 16384
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# 增加系统文件描述符限制
fs.file-max = 2097152
# 调整 TCP 参数
net.ipv4.tcp_mem = 16777216 16777216 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
应用配置:
sysctl -p
8.2 网卡性能优化
调整网卡参数:
# 增加网卡接收队列长度
ethtool -G eth0 rx 4096 tx 4096
# 启用网卡多队列
ethtool -L eth0 combined 8
# 调整网卡中断亲和力
echo 1 > /proc/irq/$(cat /proc/interrupts | grep eth0 | awk '{print $1}' | tr -d :)smp_affinity_list
九、网络监控工具
9.1 常用网络监控命令
# 查看网络接口流量
ifconfig eth0
# 实时监控网络流量
iftop
# 监控网络带宽使用情况
nethogs
# 查看网络连接状态
ss -tulpn
# 监控网络性能
vmstat 1
mpstat 1
iostat 1
9.2 高级网络监控工具
- Prometheus + Grafana:开源的监控系统和时间序列数据库
- Zabbix:企业级网络监控解决方案
- Nagios:网络和系统监控工具
- ELK Stack:日志收集、分析和可视化平台