网络配置与管理

最后更新: 2026-01-10 作者: Linux Team
页面目录
目录

网络基础

网络配置文件

# Debian/Ubuntu
/etc/network/interfaces          # 网络接口配置
/etc/netplan/*.yaml              # Netplan配置
/etc/resolv.conf                 # DNS配置
/etc/hosts                       # 主机名解析
/etc/hostname                    # 主机名

# CentOS/RHEL
/etc/sysconfig/network           # 基础网络配置
/etc/sysconfig/network-scripts/ifcfg-*   # 网卡配置
/etc/resolv.conf                # DNS配置

# 通用
/etc/services                    # 端口服务映射
/proc/sys/net/ipv4/ip_forward    # IP转发

网络接口命名

传统命名:eth0, eth1, wlan0
新式命名:enp0s3, ens33, wlpn0s0

命名规则:
en ── 以太网 (ethernet)
wl ── 无线 (wlan)
ww ── 广域网 (wwan)

后面:
p ── 总线接口 (pci bus)
s ── 插槽 (slot)
n ── 物理位置编号

ip命令

ip 是现代Linux推荐的网络配置工具。

地址管理

# 查看IP信息
ip addr                          # 显示所有地址
ip addr show                     # 同上
ip addr show eth0                # 显示指定接口

# 简写
ip a

# 添加地址
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip addr add 192.168.1.100/24 dev eth0 label eth0:1

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

接口管理

# 查看接口
ip link show                     # 显示所有接口
ip link show up                  # 只显示活跃接口

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

# 修改MAC地址
sudo ip link set eth0 down
sudo ip link set eth0 address aa:bb:cc:dd:ee:ff
sudo ip link set eth0 up

# 设置MTU
sudo ip link set eth0 mtu 9000

路由管理

# 查看路由表
ip route                         # 显示路由
ip route show

# 添加路由
sudo ip route add default via 192.168.1.1 dev eth0
sudo ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0
sudo ip route add 172.16.0.0/16 via 192.168.1.1 dev eth0

# 删除路由
sudo ip route del default
sudo ip route del 10.0.0.0/8

# 路由策略
ip rule                          # 路由策略表
sudo ip rule add from 192.168.1.0/24 table local

邻居管理(ARP)

# 查看ARP表
ip neigh show
ip neighbor

# 添加ARP条目
sudo ip neigh add 192.168.1.1 lladdr aa:bb:cc:dd:ee:ff dev eth0

# 删除ARP条目
sudo ip neigh del 192.168.1.1 dev eth0

ifconfig命令

# 查看网络信息
ifconfig
ifconfig eth0

# 启用/禁用
sudo ifconfig eth0 up
sudo ifconfig eth0 down

# 设置IP
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
sudo ifconfig eth0 broadcast 192.168.1.255

# 设置MAC
sudo ifconfig eth0 hw ether aa:bb:cc:dd:ee:ff

# 设置MTU
sudo ifconfig eth0 mtu 9000

网络配置示例

静态IP配置

Ubuntu (Netplan)

# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  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
        search:
          - example.com

# 应用配置
sudo netplan apply
sudo netplan try              # 测试配置

Ubuntu (interfaces)

# /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
    dns-search example.com

CentOS/RHEL

# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
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

DHCP配置

# Netplan DHCP
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: yes
      dhcp6: yes
# CentOS DHCP
# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth0
DEVICE=eth0
ONBOOT=yes

多IP配置

# 临时添加第二IP
sudo ip addr add 192.168.1.101/24 dev eth0 label eth0:0

# 永久配置 - Ubuntu
# /etc/network/interfaces
auto eth0:0
iface eth0:0 inet static
    address 192.168.1.101
    netmask 255.255.255.0

DNS配置

resolv.conf

# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
search example.com

# 搜索域
search local domain corp

hosts文件

# /etc/hosts
127.0.0.1   localhost
127.0.1.1   hostname
192.168.1.100   server1.example.com server1
192.168.1.101   server2.example.com server2

# IPv6
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet

DNS查询

# dig
dig example.com
dig @8.8.8.8 example.com      # 指定DNS服务器
dig +short example.com         # 简短输出
dig +trace example.com         # 追踪DNS解析

# nslookup
nslookup example.com
nslookup example.com 8.8.8.8

# host
host example.com
host -a example.com
host 93.184.216.34            # 反向查询

网络诊断工具

ping - 连通性测试

# 基本用法
ping example.com
ping 192.168.1.1

# 常用选项
ping -c 4 example.com         # 发送4个包
ping -i 2 example.com          # 2秒间隔
ping -w 10 example.com          # 运行10秒
ping -W 5 example.com          # 等待5秒超时
ping -s 1000 example.com       # 1000字节数据
ping -f example.com            # flood模式(需root)
ping -6 example.com            # IPv6

# 示例
ping -c 3 -i 2 example.com

traceroute - 路由追踪

# 追踪路由
traceroute example.com
traceroute -n example.com     # 不解析域名
traceroute -m 15 example.com   # 最大15跳
traceroute -w 3 example.com    # 等待超时3秒

# 工具变体
traceroute6 example.com        # IPv6
tracepath example.com          # 无需root
mtr example.com               # 实时追踪(推荐)

netstat - 网络统计

# 常用选项
netstat -a                     # 所有连接
netstat -at                     # TCP连接
netstat -au                     # UDP连接
netstat -l                      # 监听端口
netstat -lt                     # TCP监听
netstat -lu                     # UDP监听
netstat -p                      # 显示进程
netstat -n                      # 数字显示
netstat -r                      # 路由表
netstat -i                      # 接口统计
netstat -s                      # 协议统计

# 组合使用
netstat -tulnp                  # 监听端口及进程
netstat -an | grep ESTABLISHED # 活动连接
netstat -an | grep :80          # 80端口连接
netstat -anp | grep TIME_WAIT  # TIME_WAIT状态

ss - Socket统计

# 基本用法
ss -a                     # 所有
ss -l                     # 监听
ss -t                     # TCP
ss -u                     # UDP
ss -p                     # 进程
ss -n                     # 数字

# 状态过滤
ss -t state established
ss -t state time-wait
ss -t state listening

# 组合
ss -tulnp                 # 监听端口
ss -tunap                 # 所有带进程
ss -o state fin-wait-1   # 特定状态

# 常用状态
ss -t state time-wait | wc -l

nc/netcat - 网络瑞士军刀

# 端口扫描
nc -zv example.com 80 443 22
nc -zv 192.168.1.1 1-1000

# 连接测试
nc -zv example.com 80
echo "GET / HTTP/1.0\r\n\r\n" | nc example.com 80

# 传输文件
nc -l 1234 > file.txt              # 接收端
nc -N 192.168.1.1 1234 < file.txt  # 发送端

# 简单聊天
nc -l 1234                         # 端1
nc 192.168.1.1 1234                # 端2

# 端口转发
nc -l 8080 | nc example.com 80

# 其他
nc -zv -w 3 example.com 80         # 超时3秒

curl - HTTP客户端

# 基本请求
curl https://example.com
curl -X GET https://example.com
curl -X POST https://example.com -d "data=xxx"

# 头部
curl -I https://example.com       # HEAD请求
curl -v https://example.com       # 详细输出
curl -H "User-Agent: Mozilla" https://example.com

# 保存
curl -o output.html https://example.com
curl -O https://example.com/file.tar.gz

# 表单
curl -F "file=@upload.txt" https://example.com/upload
curl -X POST -d "name=value" https://example.com/api

# Cookie
curl -b cookies.txt https://example.com
curl -c cookies.txt https://example.com

# 代理
curl -x http://proxy:8080 https://example.com

wget - 下载工具

# 基本下载
wget https://example.com/file.tar.gz
wget -O output.tar.gz https://example.com/file.tar.gz

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

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

# 递归下载
wget -r -l 2 https://example.com/

# 其他选项
wget -q                    # 静默
wget --user=user --password=pass https://example.com
wget -e https_proxy=http://proxy:8080 https://example.com

网络诊断实战

常见故障排查

# 1. 检查物理连接
ip link show
ethtool eth0
mii-tool eth0

# 2. 检查IP配置
ip addr show
ip route show

# 3. 测试本地网关
ping 192.168.1.1

# 4. 测试DNS
ping example.com
nslookup example.com

# 5. 测试外网
ping 8.8.8.8
ping google.com

# 6. 检查端口
netstat -tulnp
ss -tulnp
lsof -i :80

# 7. 检查防火墙
sudo iptables -L -n
sudo ufw status
sudo firewall-cmd --list-all

网络性能测试

# 带宽测试
sudo apt install iperf3
# 服务端
iperf3 -s
# 客户端
iperf3 -c server_ip

# 快速测试
wget -O /dev/null http://speedtest.telia.telerik.se/1MB.bin

网卡绑定

# 查看 bonding 模块
modinfo bonding

# 配置 bond0 - /etc/network/interfaces
auto bond0
iface bond0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    slaves eth0 eth1
    bond-mode 802.3ad
    bond-miimon 100
    bond-lacp-rate 1

课后练习

实践任务
  1. 使用 ipifconfig 查看网络配置
  2. 配置静态IP地址
  3. 使用 dignslookup 进行DNS查询
  4. 使用 tracerouteping 排查网络问题
  5. 使用 netstat/ss 分析网络连接
  6. 配置hosts文件实现自定义域名解析

下一篇预告:我们将学习SSH远程管理,掌握安全远程连接技能。