本文档详细描述如何使用 Multipass 搭建一个三台虚拟机的小型局域网环境,模拟现实互联网中的 NAT、内网穿透、网络分段访问控制等场景。
Client (10.0.0.3) ← → Gateway (10.0.0.1) ← → Server (10.0.0.2:8000)
|
Internet (NAT)
- 虚拟化工具: Multipass
- 宿主机系统: Ubuntu 或 macOS
- VM 操作系统: Ubuntu 24.04 LTS
- 网络: 10.0.0.0/24 内网段
首先创建三台虚拟机:
# 创建网关虚拟机
multipass launch --name gateway --cpus 2 --memory 4G --disk 10G
# 创建服务器虚拟机
multipass launch --name server --cpus 2 --memory 4G --disk 10G
# 创建客户端虚拟机
multipass launch --name client --cpus 2 --memory 4G --disk 10G
验证虚拟机创建成功:
multipass list
应该看到类似输出:
Name State IPv4 Image
client Running 192.168.69.18 Ubuntu 24.04 LTS
gateway Running 192.168.69.16 Ubuntu 24.04 LTS
server Running 192.168.69.17 Ubuntu 24.04 LTS
Gateway 虚拟机需要配置桥接网络接口:
# 创建桥接接口
multipass exec gateway -- sudo ip link add name br0 type bridge
# 配置桥接接口IP地址
multipass exec gateway -- sudo ip addr add 10.0.0.1/24 dev br0
# 启用桥接接口
multipass exec gateway -- sudo ip link set br0 up
创建持久化网络配置:
# 创建 netplan 配置文件
multipass exec gateway -- bash -c 'echo "network:" | sudo tee /etc/netplan/60-bridge.yaml && echo " version: 2" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " bridges:" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " br0:" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " addresses:" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " - 10.0.0.1/24" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " dhcp4: false" | sudo tee -a /etc/netplan/60-bridge.yaml && echo " dhcp6: false" | sudo tee -a /etc/netplan/60-bridge.yaml'
# 修正文件权限
multipass exec gateway -- sudo chmod 600 /etc/netplan/60-bridge.yaml
# 应用网络配置
multipass exec gateway -- sudo netplan apply
为主接口添加 Gateway IP:
multipass exec gateway -- sudo ip addr add 10.0.0.1/24 dev enp0s1
为 Server 虚拟机添加静态 IP:
multipass exec server -- sudo ip addr add 10.0.0.2/24 dev enp0s1
为 Client 虚拟机添加静态 IP:
multipass exec client -- sudo ip addr add 10.0.0.3/24 dev enp0s1
在 Gateway 虚拟机上启用 IP 转发和 NAT:
# 启用 IP 转发
multipass exec gateway -- sudo sysctl -w net.ipv4.ip_forward=1
# 使 IP 转发持久化
multipass exec gateway -- echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
# 配置 NAT masquerading
multipass exec gateway -- sudo iptables -t nat -A POSTROUTING -o enp0s1 -j MASQUERADE
# 允许转发
multipass exec gateway -- sudo iptables -A FORWARD -i enp0s1 -o enp0s1 -j ACCEPT
# 配置端口转发(从 Gateway 到 Server)
multipass exec gateway -- sudo iptables -t nat -A PREROUTING -i enp0s1 -p tcp --dport 8000 -j DNAT --to-destination 10.0.0.2:8000
在 Server 虚拟机上启动 Python HTTP 服务:
# 启动 HTTP 服务,绑定到内网 IP
multipass exec server -- nohup python3 -m http.server 8000 --bind 10.0.0.2 > /dev/null 2>&1 &
验证服务启动:
multipass exec server -- ss -lntp | grep 8000
应该看到:
LISTEN 0 5 10.0.0.2:8000 0.0.0.0:* users:(("python3",pid=1798,fd=3))
在 Client 虚拟机上配置防火墙规则,阻止直接访问 Server:
# 阻止直接访问 Server
multipass exec client -- sudo iptables -A OUTPUT -d 10.0.0.2 -j DROP
# 允许访问 Gateway
multipass exec client -- sudo iptables -I OUTPUT -d 10.0.0.1 -j ACCEPT
# 从 Client 直接访问 Server(应该超时失败)
multipass exec client -- curl --connect-timeout 5 http://10.0.0.2:8000
预期结果:连接超时
# 从 Client 通过 Gateway 访问 Server(应该成功)
multipass exec client -- curl --connect-timeout 5 http://10.0.0.1:8000
预期结果:返回 HTML 页面内容
禁用 Gateway 转发功能:
multipass exec gateway -- sudo sysctl -w net.ipv4.ip_forward=0
再次测试访问(应该失败):
multipass exec client -- curl --connect-timeout 5 http://10.0.0.1:8000
恢复转发功能:
multipass exec gateway -- sudo sysctl -w net.ipv4.ip_forward=1
虚拟机名 | 主要IP | 内网IP | 角色 | 服务 |
---|---|---|---|---|
gateway | 192.168.69.16 | 10.0.0.1 | 网关/路由器 | NAT转发, 端口转发 |
server | 192.168.69.17 | 10.0.0.2 | 服务器 | HTTP服务 (端口8000) |
client | 192.168.69.18 | 10.0.0.3 | 客户端 | 仅通过网关访问服务器 |
- 网络隔离: Client 通过 iptables 规则无法直接访问 Server
- 强制路由: Client 只能通过 Gateway 访问 Server
- 端口转发: Gateway 使用 DNAT 将请求转发到 Server
- 访问控制: 可通过禁用 Gateway 的 IP 转发来控制整个网络的连通性
-
无法 ping 通
- 检查 IP 地址配置:
ip addr show
- 检查路由表:
ip route show
- 检查 IP 地址配置:
-
HTTP 访问失败
- 检查 HTTP 服务状态:
ss -lntp | grep 8000
- 检查 iptables 规则:
sudo iptables -L -n -v
- 检查 HTTP 服务状态:
-
转发不工作
- 检查 IP 转发状态:
sysctl net.ipv4.ip_forward
- 检查 NAT 规则:
sudo iptables -t nat -L -n -v
- 检查 IP 转发状态:
如需重新开始,可以删除所有虚拟机:
multipass delete gateway server client
multipass purge
这个实验环境可以帮助理解:
- NAT 网络地址转换原理
- 网络分段和访问控制
- Linux 网络配置和路由
- iptables 防火墙规则
- 网络故障排除方法
通过这个实验,可以深入理解现代网络架构中常见的安全和路由机制。