如何通过第三台 Linux 计算机连接两台 Linux 计算机?

如何通过第三台 Linux 计算机连接两台 Linux 计算机?

我通过 Oracle VirtualBox 设置了 3 个虚拟机:Ubuntu、Fedora 和 CentOS。Ubuntu 机器的设置如下:

kuyu@ub16:~$ ifconfig
enp0s3    Link encap:Ethernet  HWaddr 08:00:27:ed:19:9f  
          inet addr:172.25.1.4  Bcast:172.25.1.255  Mask:255.255.255.0
          inet6 addr: fe80::7d42:e5b6:7818:df37/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23245 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18488 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:21373422 (21.3 MB)  TX bytes:3230755 (3.2 MB)

enp0s8    Link encap:Ethernet  HWaddr 08:00:27:a4:79:fe  
          inet6 addr: fe80::5d4e:ef97:3c34:f2b6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1582 errors:0 dropped:0 overruns:0 frame:0
          TX packets:70 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:119897 (119.8 KB)  TX bytes:9194 (9.1 KB)

enp0s9    Link encap:Ethernet  HWaddr 08:00:27:3a:28:14  
          inet6 addr: fe80::118f:71e1:f2b2:f582/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1586 errors:0 dropped:0 overruns:0 frame:0
          TX packets:69 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:120766 (120.7 KB)  TX bytes:8852 (8.8 KB)

enp0s10   Link encap:Ethernet  HWaddr 08:00:27:3a:23:39  
          inet addr:10.0.3.4  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::1013:fee6:cdd6:4ba4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16377 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12869 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:15323241 (15.3 MB)  TX bytes:2256638 (2.2 MB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2304 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2304 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:292108 (292.1 KB)  TX bytes:292108 (292.1 KB)

kuyu@ub16:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.0.3.1        0.0.0.0         UG    100    0        0 enp0s10
default         172.25.1.1      0.0.0.0         UG    101    0        0 enp0s3
10.0.3.0        *               255.255.255.0   U     100    0        0 enp0s10
link-local      *               255.255.0.0     U     1000   0        0 enp0s10
172.25.1.0      *               255.255.255.0   U     100    0        0 enp0s3

Fedora 机器设置如下:

[kuyu@localhost ~]$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
    inet6 fe80::a06b:59f9:ecd2:5f4f  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:cc:b4:fe  txqueuelen 1000  (Ethernet)
    RX packets 15606  bytes 10210208 (9.7 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 10377  bytes 1378129 (1.3 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.0.3.5  netmask 255.255.255.0  broadcast 10.0.3.255
    inet6 fe80::5081:a65:f591:5587  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:10:4b:c6  txqueuelen 1000  (Ethernet)
    RX packets 185  bytes 27128 (26.4 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 314  bytes 44983 (43.9 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.2.5  netmask 255.255.255.0  broadcast 192.168.2.255
    inet6 fe80::ea6c:3aa1:5fc9:bdff  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:e1:62:f4  txqueuelen 1000  (Ethernet)
    RX packets 62  bytes 12502 (12.2 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 234  bytes 34491 (33.6 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 36  bytes 3112 (3.0 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 36  bytes 3112 (3.0 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
    ether 00:00:00:00:00:00  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[kuyu@localhost ~]$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 enp0s3
default         gateway         0.0.0.0         UG    101    0        0 enp0s9
default         gateway         0.0.0.0         UG    102    0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
10.0.3.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.2.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s9
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

CentOS 机器设置如下:

[kuyu@localhost ~]$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.25.1.5  netmask 255.255.255.0  broadcast 172.25.1.255
    inet6 fe80::1f36:5e13:45ae:b7d1  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:f4:fd:91  txqueuelen 1000  (Ethernet)
    RX packets 15070  bytes 21673274 (20.6 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 7401  bytes 471787 (460.7 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet6 fe80::bd87:67c1:8b39:f511  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:30:f3:fa  txqueuelen 1000  (Ethernet)
    RX packets 1442  bytes 107149 (104.6 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 56  bytes 6524 (6.3 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet6 fe80::e6a5:62c4:3dfa:b841  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:b9:cf:71  txqueuelen 1000  (Ethernet)
    RX packets 1403  bytes 102963 (100.5 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 17  bytes 2954 (2.8 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.2.4  netmask 255.255.255.0  broadcast 192.168.2.255
    inet6 fe80::3c3e:1a03:e554:514d  prefixlen 64  scopeid 0x20<link>
    ether 08:00:27:bd:1f:4a  txqueuelen 1000  (Ethernet)
    RX packets 131  bytes 23156 (22.6 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 339  bytes 57279 (55.9 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1  (Local Loopback)
    RX packets 134  bytes 11636 (11.3 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 134  bytes 11636 (11.3 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
    ether 52:54:00:bb:2b:56  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[kuyu@localhost ~]$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 enp0s3
default         gateway         0.0.0.0         UG    101    0        0 enp0s10
172.25.1.0      0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.2.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s10
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

Fedora 机器中启用了路由:

[kuyu@localhost ~]$ sudo sysctl -p /etc/sysctl.conf
net.ipv4.ip_forward = 1

Ubuntu 机器能够 ping Fedora 机器:

kuyu@ub16:~$ ping 10.0.3.5
PING 10.0.3.5 (10.0.3.5) 56(84) bytes of data.
64 bytes from 10.0.3.5: icmp_seq=1 ttl=64 time=2.39 ms
64 bytes from 10.0.3.5: icmp_seq=2 ttl=64 time=1.53 ms

同样,CentOS 机器可以 ping Fedora 机器:

[kuyu@localhost ~]$ ping 192.168.2.5
PING 192.168.2.5 (192.168.2.5) 56(84) bytes of data.
64 bytes from 192.168.2.5: icmp_seq=1 ttl=64 time=1.63 ms
64 bytes from 192.168.2.5: icmp_seq=2 ttl=64 time=1.14 ms
64 bytes from 192.168.2.5: icmp_seq=3 ttl=64 time=1.42 ms

但是,Ubuntu 计算机无法通过 Fedora 计算机 ping 通 CentOS 计算机:

kuyu@ub16:~$ ping 192.168.2.4
PING 192.168.2.4 (192.168.2.4) 56(84) bytes of data.
^C
--- 192.168.2.4 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4050ms

同样,CentOS 机器无法通过 Fedora 机器 ping 通 Ubuntu 机器:

[kuyu@localhost ~]$ ping 10.0.3.4
PING 10.0.3.4 (10.0.3.4) 56(84) bytes of data.
^C
--- 10.0.3.4 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3004ms

我的问题是如何修复机器配置,以便 Ubuntu 机器可以通过 Fedora 机器 ping 通 CentOS 机器(反之亦然)?

答案1

您的 Fedora 机器可能/很可能具有默认防火墙,阻止了数据包的转发。

尝试运行

/sbin/iptables -I 转发 -j 接受

允许通过该箱转发。

如果这不起作用,我会打破 tcpdump(然后在盒子之间启动 ping 并查看它卡在哪里)

相关内容