如果尚未對網關執行 ping 操作,則無法對網關或網際網路執行 ping 操作

如果尚未對網關執行 ping 操作,則無法對網關或網際網路執行 ping 操作

我對網路很陌生,我試著尋找我的問題的答案,但沒有找到。

我有 4 個虛擬機,我們稱之為 1、2、3、4。第一個(網關)具有僅主機和橋接網路卡,其他僅具有僅主機。

我設定了一些iptables 規則,將虛擬機的2,3,4 封包重新導向到網關,以便它們存取互聯網,它有效,但我既無法ping 網關,也無法8.8.8.8,例如,只要我沒有ping虛擬機器第一個(網關),在我這樣做之後,我可以 ping 網關、谷歌等等,在此之前 ic 只能 ping 本機。我希望能夠存取互聯網並 ping GW,而無需使用 GW 對其進行 ping 操作。

以下是我制定的 iptables 規則:

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -i enp0s8 -o enp0s3 -j ACCEPT
iptables -A FORWARD -i enp0s3 -o enp0s8 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADE

第一個VM的介面如下:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo enp0s8
iface lo inet loopback
auto lo enp0s3
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp
auto enp0s8
iface enp0s8 inet static
        address 192.168.1.1
        netmask 255.255.255.0

還有我的其他虛擬機,其他虛擬機也有相同的,只是 IP 不同:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static
        address 192.168.1.3
        netmask 255.255.255.0
        gateway 192.168.1.1

謝謝!

相關內容