Skip to content

Instantly share code, notes, and snippets.

@JamesHopbourn
Created August 2, 2025 01:13
Show Gist options
  • Save JamesHopbourn/1d5f4e87d8c01c7e63ceb57e2d92c109 to your computer and use it in GitHub Desktop.
Save JamesHopbourn/1d5f4e87d8c01c7e63ceb57e2d92c109 to your computer and use it in GitHub Desktop.
小型互联网实验环境搭建详细步骤 - 三节点虚拟网络实验

小型互联网实验环境搭建详细步骤

前提条件

  • 安装了 Multipass 的主机系统(macOS/Linux/Windows)
  • 确保主机有足够资源:至少 6GB 内存,20GB 磁盘空间
  • 具备 sudo 权限

步骤一:创建虚拟机

1.1 创建三台虚拟机

# 创建网关虚拟机
multipass launch --name vm-gateway --cpus 2 --memory 4G --disk 10G

# 创建服务器虚拟机
multipass launch --name vm-server --cpus 2 --memory 4G --disk 10G

# 创建客户端虚拟机
multipass launch --name vm-client --cpus 2 --memory 4G --disk 10G

1.2 验证虚拟机创建

multipass list

预期输出:

Name                    State             IPv4             Image
vm-client               Running           192.168.69.21    Ubuntu 24.04 LTS
vm-gateway              Running           192.168.69.19    Ubuntu 24.04 LTS
vm-server               Running           192.168.69.20    Ubuntu 24.04 LTS

步骤二:配置服务器(vm-server)

2.1 启动 Python HTTP 服务

# 在 vm-server 上启动 HTTP 服务(后台运行)
multipass exec vm-server -- bash -c "nohup python3 -m http.server 8000 > /dev/null 2>&1 &"

2.2 验证服务启动

# 检查进程是否运行
multipass exec vm-server -- ps aux | grep python

# 本地测试服务
multipass exec vm-server -- curl -s http://localhost:8000 | head -5

预期输出包含 HTML 目录列表页面。

2.3 配置防火墙规则(阻止客户端直接访问)

# 阻止客户端直接访问服务器 8000 端口
multipass exec vm-server -- sudo iptables -A INPUT -s 192.168.69.21 -p tcp --dport 8000 -j DROP

# 允许网关访问服务器 8000 端口
multipass exec vm-server -- sudo iptables -A INPUT -s 192.168.69.19 -p tcp --dport 8000 -j ACCEPT

步骤三:配置网关(vm-gateway)

3.1 启用 IP 转发

# 启用 IP 转发功能
multipass exec vm-gateway -- sudo sysctl -w net.ipv4.ip_forward=1

3.2 配置 NAT 转发规则

# 设置 DNAT 规则:将访问网关 8000 端口的请求转发到服务器
multipass exec vm-gateway -- sudo iptables -t nat -A PREROUTING -p tcp --dport 8000 -j DNAT --to-destination 192.168.69.20:8000

# 设置 MASQUERADE 规则:对转发到服务器的请求进行源地址转换
multipass exec vm-gateway -- sudo iptables -t nat -A POSTROUTING -p tcp -d 192.168.69.20 --dport 8000 -j MASQUERADE

# 允许转发到服务器 8000 端口
multipass exec vm-gateway -- sudo iptables -A FORWARD -p tcp -d 192.168.69.20 --dport 8000 -j ACCEPT

3.3 验证规则配置

# 查看 NAT 表规则
multipass exec vm-gateway -- sudo iptables -t nat -L -n

# 查看 FORWARD 链规则
multipass exec vm-gateway -- sudo iptables -L FORWARD -n

步骤四:测试验证

4.1 测试直接访问(应该失败)

# 客户端直接访问服务器(应该超时失败)
multipass exec vm-client -- curl -m 5 http://192.168.69.20:8000

预期结果:连接超时错误

curl: (28) Connection timed out after 5028 milliseconds

4.2 测试通过网关访问(应该成功)

# 客户端通过网关访问服务器(应该成功)
multipass exec vm-client -- curl http://192.168.69.19:8000

预期结果:返回 HTML 目录列表页面

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Directory listing for /</title>
</head>
<body>
<h1>Directory listing for /</h1>
...

步骤五:持久化配置(可选)

5.1 保存 iptables 规则

在各虚拟机上保存 iptables 规则:

# 在 vm-server 上保存规则
multipass exec vm-server -- sudo sh -c "iptables-save > /etc/iptables/rules.v4"

# 在 vm-gateway 上保存规则
multipass exec vm-gateway -- sudo sh -c "iptables-save > /etc/iptables/rules.v4"

5.2 设置开机自启动

# 在 vm-gateway 上设置 IP 转发开机自启
multipass exec vm-gateway -- sudo sh -c "echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf"

# 在 vm-server 上创建服务启动脚本
multipass exec vm-server -- sudo tee /etc/systemd/system/http-server.service << 'EOF'
[Unit]
Description=Python HTTP Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/usr/bin/python3 -m http.server 8000
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 启用服务
multipass exec vm-server -- sudo systemctl enable http-server.service
multipass exec vm-server -- sudo systemctl start http-server.service

故障排除

常见问题

  1. 虚拟机无法启动

    # 检查 Multipass 状态
    multipass version
    multipass list
  2. 网络连接问题

    # 检查网络接口
    multipass exec vm-server -- ip addr show
    
    # 检查路由表
    multipass exec vm-gateway -- ip route show
  3. iptables 规则不生效

    # 清空所有规则重新配置
    multipass exec vm-gateway -- sudo iptables -F
    multipass exec vm-gateway -- sudo iptables -t nat -F
  4. 服务无法访问

    # 检查服务进程
    multipass exec vm-server -- netstat -tlnp | grep 8000
    
    # 检查防火墙状态
    multipass exec vm-server -- sudo ufw status

清理环境

完全清理实验环境:

# 停止并删除所有虚拟机
multipass stop vm-gateway vm-server vm-client
multipass delete vm-gateway vm-server vm-client
multipass purge

总结

通过以上步骤,成功实现了:

  • ✅ 三节点虚拟网络环境
  • ✅ 客户端无法直接访问服务器
  • ✅ 客户端必须通过网关访问服务器
  • ✅ 网络隔离和中转访问控制

实验环境模拟了真实的网络隔离场景,适用于学习网络安全、代理服务和网络架构设计。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment