在开启虚拟机时,需要宿主与虚拟机网络连通并可以上互联网,需要配置网络桥接模式,本文记录配置方法。
桥接模式下的所有虚拟机都是和主机是在同一网段的,可以把它们看成是"平等"的关系,桥接模式下虚拟机和物理的总数不能超过网段容纳的最大主机数量
ifconfig
nm-connection-editor
。这款GUI(图形用户界面)的工具允许你傻瓜式地配置一个网桥,启动nm-connection-editor。nm-connection-editor
+
,添加一个网桥,选择桥接
bridge0
增加
按钮,选择以太网
,建立从机
从机
配置界面,以太网选项卡中,设备选择当前的网卡:常规
选项卡中勾选前两个,之后保存从机
配置选项:IPv4 设置
标签,为网桥配置DHCP或者是静态IP地址桥接
窗口里。因为已经从属与网桥,以前配置好的有线连接
就不再需要了,原来的有线连接
可以删除了。bridge0 从机 1
ifconfig
查看网络配置:bridge0
,并使用了我们设置的IPenp1s0
已经没有 IP 了至此,网桥已经建立,宿主机静态IP已经设置好了,接下来配置虚拟机
virt -install
创建虚拟机时,网络配置参数:--network bridge=bridge0
/etc/netplan/*.yaml
,配置虚拟网卡静态 IP,可以参考Ubuntu 20.04 设置静态IPnetwork:
ethernets:
enp1s0:
match:
macaddress: 52:54:00:fd:fb:17
dhcp4: false
dhcp6: false
wakeonlan: true
addresses:
- 192.168.1.111/24
gateway4: 192.168.1.255
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
sudo netplan apply
有时 netplan 文件夹下没有文件,可以采用如下方法
- `/etc/network/interfaces`(配置IP和网关)
- `/etc/resolv.conf`(配置DNS服务器)修改
# 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 enp1s0
allow-hotplug enp1s0
# iface enp1s0 inet dhcp
iface enp1s0 inet static
address 192.168.1.111
netmask 255.255.255.0
gateway 192.168.1.1
broadcast 192.168.1.255
# This is an autoconfigured IPv6 interface
iface enp1s0 inet6 auto
配置 | 含义 |
---|---|
auto enp1s0 | 开机自动连接网络 |
iface enp1s0 inet static | 设置静态IP |
iface enp1s0 inet dhcp | dhcp 自动获取IP |
address 192.168.1.111 | 静态 IPv4地址 |
netmask 255.255.255.0 | 子网掩码 |
gateway 192.168.1.1 | 网关 |
broadcast 192.168.1.255 | 广播地址(也可以不写) |
/etc/resolv.conf
文件,设置 dnsnameserver 192.168.1.1
nameserver 114.114.114.114
service networking restart
$ ifconfig
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a1e:8fc7:3bc0:5054:ff:fefd:fb17 prefixlen 64 scopeid 0x0<global>
inet6 fe80::5054:ff:fefd:fb17 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:fd:fb:17 txqueuelen 1000 (Ethernet)
RX packets 39141 bytes 2699714 (2.5 MiB)
RX errors 0 dropped 24226 overruns 0 frame 0
TX packets 503 bytes 77888 (76.0 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 2126 bytes 818164 (798.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2126 bytes 818164 (798.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0