我的目标是配置机器,以便我可以拥有多个可以与互联网通信的 nspawn 容器,主机必须能够与互联网通信,并且从主机我必须能够通过 ip 连接容器 -容器将运行 Web 服务器。
问题问题是,看起来配置正常,但我无法从 Internet 对计算机执行 ping 操作,并且当我登录到计算机时,它无法与 Internet 通信。
问题:我如何配置它以与互联网通信?
这是一个运行在 Oracle 云上的虚拟机,上面写着:
公共 IPv4 地址:192.9.139.126 私有 IPv4 地址:10.0.0.214
我正在尝试从 netplan 配置更改为使用 systemd 网络文件的配置。
这个网络计划配置工作正常:
ubuntu@instance-20240106-1821:~$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
ethernets:
ens3:
dhcp4: true
match:
macaddress: 02:00:17:00:2a:9c
set-name: ens3
ubuntu@instance-20240106-1821:~$
但现在我已经禁用了上述功能并通过 systemd 网络配置实现,我无法与互联网进行通信。
这是我的网络配置:
ubuntu@instance-20240106-1821:~$ cat /etc/systemd/network/br0.network
[Match]
Name=br0
[Network]
DHCP=no
Address=10.0.0.214/24
Gateway=10.0.0.1
ubuntu@instance-20240106-1821:~$ cat /etc/systemd/network/br0.netdev
[NetDev]
Name=br0
Kind=bridge
[Bridge]
MACAddress=02:00:17:00:2a:9c
ubuntu@instance-20240106-1821:~$ cat /etc/systemd/network/ens3.network
[Match]
Name=ens3
[Network]
Bridge=br0
ubuntu@instance-20240106-1821:~$ sudo networkctl 状态 ens3
● 2: ens3
Link File: /usr/lib/systemd/network/99-default.link
Network File: /etc/systemd/network/ens3.network
Type: ether
State: enslaved (configured)
Online state: online
Alternative Names: enp0s3
Path: pci-0000:00:03.0
Driver: virtio_net
Vendor: Red Hat, Inc.
Model: Virtio network device
HW Address: 02:00:17:00:2a:9c
MTU: 9000 (min: 68, max: 65535)
QDisc: pfifo_fast
Master: br0
IPv6 Address Generation Mode: none
Queue Length (Tx/Rx): 1/1
Auto negotiation: no
Speed: n/a
Activation Policy: up
Required For Online: yes
Jan 06 08:59:08 instance-20240106-1821 systemd-networkd[526]: ens3: Link UP
Jan 06 08:59:08 instance-20240106-1821 systemd-networkd[526]: ens3: Gained carrier
Jan 06 08:59:08 instance-20240106-1821 systemd-networkd[526]: ens3: Gained IPv6LL
ubuntu@instance-20240106-1821:~$ sudo networkctl 状态 br0
● 3: br0
Link File: /usr/lib/systemd/network/99-default.link
Network File: /etc/systemd/network/br0.network
Type: bridge
State: routable (configured)
Online state: online
Driver: bridge
HW Address: 5e:37:61:dc:ab:83
MTU: 9000 (min: 68, max: 65535)
QDisc: noqueue
IPv6 Address Generation Mode: eui64
Forward Delay: 15s
Hello Time: 2s
Max Age: 20s
Ageing Time: 5min
Priority: 32768
STP: no
Multicast IGMP Version: 2
Cost: 2000
Port State: disabled
Queue Length (Tx/Rx): 1/1
Auto negotiation: no
Speed: n/a
Address: 10.0.0.214
fe80::5c37:61ff:fedc:ab83
Gateway: 10.0.0.1
Activation Policy: up
Required For Online: yes
DHCP6 Client DUID: DUID-EN/Vendor:0000ab110e53ac1395c0e1110000
Jan 06 08:59:08 instance-20240106-1821 systemd-networkd[526]: br0: netdev ready
Jan 06 08:59:08 instance-20240106-1821 systemd-networkd[526]: br0: Link UP
Jan 06 08:59:09 instance-20240106-1821 systemd-networkd[526]: br0: Gained carrier
Jan 06 08:59:11 instance-20240106-1821 systemd-networkd[526]: br0: Gained IPv6LL
ubuntu@instance-20240106-1821:~$
ip 一个
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc pfifo_fast master br0 state UP group default qlen 1000
link/ether 02:00:17:00:2a:9c brd ff:ff:ff:ff:ff:ff
altname enp0s3
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc noqueue state UP group default qlen 1000
link/ether 5e:37:61:dc:ab:83 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.214/24 brd 10.0.0.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::5c37:61ff:fedc:ab83/64 scope link
valid_lft forever preferred_lft forever
ubuntu@instance-20240106-1821:~$
答案1
[NetDev]
Name=br0
Kind=bridge
[Bridge]
MACAddress=02:00:17:00:2a:9c
MACAddress=
仅记录该[NetDev]
部分,而不记录文件[Bridge]
的部分*.netdev
。或者,您可以[Link]
在文件中添加一个部分br0.network
并将其放入其中。
目前它似乎被忽略了,并且您的网桥正在获取生成的 MAC 地址:
● 3: br0
Link File: /usr/lib/systemd/network/99-default.link
Network File: /etc/systemd/network/br0.network
Type: bridge
State: routable (configured)
Online state: online
Driver: bridge
HW Address: 5e:37:61:dc:ab:83 <---!!!
所以你应该将你的br0.netdev
文件更改为:
[NetDev]
Name=br0
Kind=bridge
MACAddress=02:00:17:00:2a:9c
[Bridge]
或者,您可以从文件MacAddress=02:00:17:00:2a:9c
中完全删除该行br0.netdev
,并将其放入br0.network
文件中的以下[Link]
部分:
[Match]
Name=br0
[Link]
MACAddress=02:00:17:00:2a:9c
[Network]
DHCP=no
Address=10.0.0.214/24
Gateway=10.0.0.1
答案2
最后我认为问题是它必须是 DHCP=yes
将 br0.network 更改为:
[Match]
Name=br0
[Network]
DHCP=yes
#Address=10.0.0.214/24
#Gateway=10.0.0.1
DNS=8.8.8.8
[Link]
MACAddress=02:00:17:00:4a:bc
我有:
ip 一个
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc pfifo_fast master br0 state UP group default qlen 1000
link/ether 02:00:17:00:4a:bc brd ff:ff:ff:ff:ff:ff
altname enp0s3
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc noqueue state UP group default qlen 1000
link/ether 02:00:17:00:4a:bc brd ff:ff:ff:ff:ff:ff
inet 10.0.0.209/24 metric 1024 brd 10.0.0.255 scope global dynamic br0
valid_lft 86351sec preferred_lft 86351sec
inet6 fe80::17ff:fe00:4abc/64 scope link
valid_lft forever preferred_lft forever
嘿,快点:
ubuntu@instance-20240106-2021:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=61 time=1.72 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=61 time=1.75 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=61 time=1.74 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=61 time=1.78 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=61 time=1.75 ms
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 1.718/1.746/1.775/0.018 ms
ubuntu@instance-20240106-2021:~$