%20%E5%92%8C%20IPv6.png)
我在 Hetzner 租了一台專用伺服器並在上面安裝了 Ubuntu 伺服器 18.04 LTS。我有兩個名為 xxxx 和 yyyy 的公共 IPv4 位址以及一個用於將我的專用網路連接到互聯網的 IPv6 /64 區塊。我對 IPv4 使用 NAT。
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- x.x.x.x/32
- y.y.y.y/32
- a.a.a.a::2/64
routes:
- on-link: true
to: 0.0.0.0/0
via: z.z.z.z
gateway6: fe80::1
nameservers:
addresses:
- 1.1.1.1
- 1.0.0.1
- 2606:4700:4700::1111
- 2606:4700:4700::1001
bridges:
xenbr0:
interfaces: []
addresses:
- 192.168.0.1/24
- a.a.a.a::3/64
parameters:
forward-delay: 0
stp: false
xenbr1:
interfaces: []
addresses:
- 192.168.1.1/24
- a.a.a.a::4/64
parameters:
forward-delay: 0
stp: false
XEN的安裝與設定:
sudo apt-get install xen-hypervisor-amd64 xen-tools
sudo reboot
sudo vim /etc/default/grub
GRUB_CMDLINE_XEN_DEFAULT="dom0_mem=min:1024M,max:1024M dom0_max_vcpus=2 dom0_vcpus_pin"
sudo vim /etc/xen/xl.conf
autoballoon=0
sudo update-grub
sudo reboot
應該可以使用 IP 轉送和 NAT 來存取和從 Internet 存取虛擬機器。
sudo vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p /etc/sysctl.conf
sudo apt-get install iptables-persistent
NAT部分:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
將 HTTP 和 HTTPS 請求轉送到我們的 VM2 伺服器:
sudo iptables -A PREROUTING -t nat -p tcp -i eth0 -d x.x.x.x --dport 80 -j DNAT --to 192.168.0.11:80
sudo iptables -A PREROUTING -t nat -p tcp -i eth0 -d x.x.x.x --dport 443 -j DNAT --to 192.168.0.11:443
我們不希望 192.168.0.x <-> 192.168.0.y 之間有任何流量,因此我們使用一些簡單的規則丟棄所有資料包。這會阻止網橋之間的通訊:
sudo iptables -P FORWARD DROP
sudo iptables -A FORWARD -i eth0 -o xenbr0 -j ACCEPT
sudo iptables -A FORWARD -i xenbr0 -o eth0 -s 192.168.0.0/24 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o xenbr1 -j ACCEPT
sudo iptables -A FORWARD -i xenbr1 -o eth0 -s 192.168.1.0/24 -j ACCEPT
IPv4 部分工作正常,但 IPv6 部分不行。當我已手動為我的 VM 指派了 IPv6 位址。這就像網橋阻止 IPv6 流量離開網絡,但無法找出原因。
答案1
我已經設法弄清楚了。關鍵是使用帶有 /128 網路遮罩的 ipv6 位址,該位址將充當虛擬機器的網關。讓我們從修改 Netplan 設定開始:
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- x.x.x.x/32
- y.y.y.y/32
- a.a.a.a::00/128
- a.a.a.a::10/128
routes:
- on-link: true
to: 0.0.0.0/0
via: z.z.z.z
gateway6: fe80::1
nameservers:
addresses:
- 1.1.1.1
- 1.0.0.1
- 2606:4700:4700::1111
- 2606:4700:4700::1001
bridges:
xenbr0:
interfaces: []
addresses:
- 192.168.0.1/24
- a.a.a.a::00/125
parameters:
forward-delay: 0
stp: false
xenbr1:
interfaces: []
addresses:
- 192.168.1.1/24
- a.a.a.a::10/125
parameters:
forward-delay: 0
stp: false
請注意分配給 eth0、xenbr0 和 xenbr1 介面的 ipv6 位址。 Eth0 獲得兩個單一 ipv6 (/128) 位址,而 xenbr0 和 xenbr1 獲得可用於 VM 的八個位址區塊 (/125)。 VM1 網路配置如下所示:
# The loopback network interface
auto lo
iface lo inet loopback
iface lo0 inet6 loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.10
netmask 255.255.255.0
broadcast 192.168.0.255
network 192.168.0.0
gateway 192.168.0.1
iface eth0 inet6 static
address a:a:a:a::01
netmask 125
gateway a:a:a:a::00
VM3 配置使用另一個橋:
# The loopback network interface
auto lo
iface lo inet loopback
iface lo0 inet6 loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
iface eth0 inet6 static
address a:a:a:a::11
netmask 125
gateway a:a:a:a::10
最後但並非最不重要的一點是不要忘記啟用 ipv6 的 ip 轉送。
sudo vim /etc/sysctl.conf
net.ipv6.conf.all.forwarding=1
sudo sysctl -p /etc/sysctl.conf