VirtualBox Ubuntu20.04 使用双网卡实现 ssh连接 和 外网访问

一、前言

1.1 配置双网卡的目的

  • 虚拟机可以访问外网
  • 虚拟机和虚拟机之间可以互相访问

二、配置

2.1 virtualBox主机网卡设置

管理 -> 主机网络管理器


2.2 虚拟机开启两个网卡

设置 -> 网络


2.3 查看网络状态

rockfane@ubuntu:~$ 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:e5:a3:29 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.100/24 brd 192.168.10.255 scope global dynamic enp0s3
       valid_lft 557sec preferred_lft 557sec
    inet6 fe80::a00:27ff:fee5:a329/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 08:00:27:82:5d:3e brd ff:ff:ff:ff:ff:ff

只有一张网卡在工作

2.4 网络配置

Ubuntu 18.04 LTS 之后的版本都改用 Netplan 来管理网络配置

Netplan默认配置文件在/etc/netplan目录下

2.4.1 查看目录下的yaml配置文件

rockfane@ubuntu:~$ ls /etc/netplan/*.yaml
/etc/netplan/00-installer-config.yaml

2.4.2 使用vim修改配置文件

rockfane@ubuntu:~$ sudo vim /etc/netplan/00-installer-config.yaml
[sudo] password for rockfane:

2.4.3 编辑配置文件

仅主机网卡只配置静态ip保证与物理机互联

NAT网卡直接从DHCP服务器获取地址

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s3:
      dhcp4: no
      addresses: [192.168.10.2/24]
    enp0s8:
      dhcp4: yes
  version: 2

2.4.4 重启网络

rockfane@ubuntu:~$ sudo netplan apply
[sudo] password for rockfane:

2.5 再次查看网络状态

rockfane@ubuntu:~$ 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:e5:a3:29 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.2/24 brd 192.168.10.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee5:a329/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:82:5d:3e brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
       valid_lft 82987sec preferred_lft 82987sec
    inet6 fe80::a00:27ff:fe82:5d3e/64 scope link
       valid_lft forever preferred_lft forever

两张网卡都在工作

三、验证

3.1 测试虚拟机和物理机通信

物理机ip: 192.168.1.84

rockfane@ubuntu:~$ ping 192.168.1.84
PING 192.168.1.84 (192.168.1.84) 56(84) bytes of data.
64 bytes from 192.168.1.84: icmp_seq=1 ttl=127 time=1.58 ms
64 bytes from 192.168.1.84: icmp_seq=2 ttl=127 time=0.748 ms
64 bytes from 192.168.1.84: icmp_seq=3 ttl=127 time=0.759 ms
64 bytes from 192.168.1.84: icmp_seq=4 ttl=127 time=0.699 ms

3.2 测试虚拟机外网连接

rockfane@ubuntu:~$ ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=55 time=6.97 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=55 time=8.51 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=55 time=6.31 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=55 time=6.56 ms

3.3 测试物理机ssh连接虚拟机

虚拟机ip: 192.168.10.2

PS C:\Users\RockFane> ssh rockfane@192.168.10.2
rockfane@192.168.10.2's password:
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-100-generic x86_64)
posted @ 2022-02-28 13:21  RockFane  阅读(1582)  评论(0编辑  收藏  举报