开发过程中经常会使用到linux虚拟机,一般用NAT模式共享主机网络即可,但是我想在机房用其他电脑远程访问电脑上的虚拟机就不能使用NAT模式了。这时候我们就需要使用桥接模式。设置vmware中的linux与主机属于同一网段,不仅可以访问公司任意网段,也可以在公司网段下访问到vmware 中的linux虚拟机。
公司电脑一般都配置了两张网,一张用于访问公司内网(假设172.17 是内网网段),一张访问外网(假设172.16是外网网段)。我们需要桥接这两张网卡就可以像独立主机一样同时访问公司的两个网段。
怎么设置呢?
第一步:先在vmware中配置桥接模式:打开 【编辑】 >> 【虚拟网络编辑器】。
添加两个网络,这里分别是VMnet0 桥接模式 桥接到外网网卡,VMnet1桥接模式 桥接到内网网卡。然后确定保存。退出VMWare软件再次打开生效。
第二步: 找到linux虚拟机,打开【编辑虚拟机设置】
默认有一个网络适配器,需要再添加网络适配器,然后分别选择第一步中设置的网络名称。
第三步:开启linux,进行设置网络,我这里以debian 9 为例。
我这里默认网卡名为ens33 和ens37,我想更改网卡名为eth0 和eth1。
1. 修改 /etc/default/grub 文件
GRUB_CMDLINE_LINUX=""
改为
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
以root 权限更新grub修改:
grub-mkconfig -o /boot/grub/grub.cfg
2. 编辑 /etc/udev/rules.d/70-persistent-net.rules 没有就创建一个
# interface with MAC address "00:0c:29:c7:be:f8" will be assigned "eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fc:e7:f8", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# interface with MAC address "00:0c:29:c7:be:99" will be assigned "eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fc:e7:99", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
3. 修改 /etc/network/interfaces, eth0 为访问外网,eth1访问内网。
# 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
auto eth0
allow-hotplus eth0
iface eth0 inet dhcp
auto eth1
iface eth1 inet static
address 172.17.176.80
netmask 255.255.0.0
gateway 172.26.0.1
下面是修改后的网卡信息,eth0对应外网,因为公司访问外网需要去网管那登记mac地址、不登记无法上外网,所以我这里修改和主机的mac地址一致。
根据需求下面进行修改mac地址:
1. 因为这里需要修改成已经登记过的mac地址才能上外网,索性修改成和电脑网卡mac地址一致(电脑网卡mac地址已经在入职的时候就登记过了),查看一下把本机mac地址。
2. 修改VMware的中桥接网卡对应的mac地址;
3. 进入debian中修改 /etc/udev/rules.d/70-persistent-net.rules 中eth0的mac地址和上面的一样
# interface with MAC address "4c:ed:fb:69:38:c3" will be assigned "eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fc:e7:f8", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# interface with MAC address "00:0c:29:c7:be:99" will be assigned "eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:fc:e7:99", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
4.重启生效。
这时候vmware中的虚拟机就像一台独立的主机,可以和公司的任意一台主机进行互访,也可以不用登记mac地址使用公司的外网。