Ubuntu22.04 VirtualBox
通常三种虚拟机网络连入模式
桥接网卡
虚拟网卡连入宿主机所在的路由器,公用一个网关地址,自动分配路由器ip到虚拟网卡上,相当于和物理机一样的地址,在同一网段,其他物理机和这个虚拟机一样,可以互相访问,也可以连接外网。
Nat网络
Nat网络转发,默认和宿主机共享ip,外界看不到虚拟机的真实ip,虚拟机可以联网,但是外部网络不能访问虚拟机,除非配置端口转发。虚拟机之间可以互相访问。
仅主机(Host-Only)网络
更加封闭的网络设置,只有宿主机和虚拟机能之间能访问,默认不能联网。
VirtualBox 通过2种网络连入配合设置网络可以被宿主机访问,可以访问外部网络,操作步骤:
-
启动 VirtualBox
-
确保工具 -> 属性 -> 仅主机 (Host-Only)网络 有相关的网络
-
确保工具 -> 属性 -> NAT网络 有相关的网络
设置网络配置-网卡1
设置网络配置-网卡2
-
查看网卡配置
$ ip addr
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:91:eb:1f brd ff:ff:ff:ff:ff:ff
...
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:3e:45:a9 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:3b:66:8e:f9 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
$ cat /etc/netplan/00-installer-config.yaml
network:
ethernets:
enp0s3:
dhcp4: true
version: 2
- 编辑 /etc/netplan/00-installer-config.yaml
network:
ethernets:
enp0s3:
dhcp4: true
enp0s8:
dhcp4: false
addresses: [192.168.56.11/24]
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
- 114.114.114.114
- 192.168.56.1
version: 2
- IP更新生效
$ sudo netplan apply
$ ip addr
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:91:eb:1f brd ff:ff:ff:ff:ff:ff
inet 10.0.2.4/24 metric 100 brd 10.0.2.255 scope global dynamic enp0s3
valid_lft 499sec preferred_lft 499sec
inet6 fe80::a00:27ff:fe91:eb1f/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:3e:45:a9 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.11/24 brd 192.168.56.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe3e:45a9/64 scope link
valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:3b:66:8e:f9 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
$ ping www.baidu.com
PING www.a.shifen.com (14.119.104.254): 56 data bytes
64 bytes from 14.119.104.254: icmp_seq=0 ttl=54 time=7.979 ms
64 bytes from 14.119.104.254: icmp_seq=1 ttl=54 time=8.269 ms
64 bytes from 14.119.104.254: icmp_seq=2 ttl=54 time=8.764 ms
64 bytes from 14.119.104.254: icmp_seq=3 ttl=54 time=9.807 ms
64 bytes from 14.119.104.254: icmp_seq=4 ttl=54 time=9.206 ms
...