Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to bridge two VirtualBox virtual networks with bridge in Linux

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how bridge in Linux bridges two VirtualBox virtual networks. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

First of all, there is not enough disk space, and the allocated computer has only 12G of space left, cannot connect to the hard disk, and cannot upload backup for the time being, which is equivalent to an offline machine. I want to fully compile a 4.19 version of the Linux kernel. So day and night, I began the operation of human flesh compression and release, not happy face. In the end, I solved the problem, but the disk space problem was not solved elegantly, at least it didn't make it look like I was showing off.

Compared with the complaints about disk space, the following questions are interesting.

Using virtual machines to build a network test environment will always encounter a variety of strange problems, which is often caused by the defects of various virtual machine platforms. (maybe I understand narrowly, but in my opinion, the mechanism that hinders work is a defect.)

After all, the virtual network is not real. With the development of network technology for more than 40 years, it is almost impossible for a software to pass comprehensive testing. It is also a normal phenomenon that the design is not well considered.

For example, use a VirtualBox virtual machine to build a test environment that meets the following requirements:

I need a Linux host to bridge the two networks. (I need to set some rules in the middle and I don't want to walk)

It's simple, isn't it? Have you tried it?

The idea is very straightforward and simple. Lan Segment,VirtualBox like VMWare has a virtual network called Internal Network, which, to put it bluntly, helps you touch an internal private network.

Host B is used as a Bridge, and the network card is configured as follows:

As two hosts for Bridge bridging two networks, the Linux system is configured as follows:

Brctl addbr br0;brctl addif br0 enp0s9;brctl addif br0 enp0s10;ifconfig br0 up

The enp0s9 of host H1 is set to Internal mode, and access to intnet:

The Linux system configuration of H1 is as follows:

Ifconfig enp0s9 40.40.40.201/24

The enp0s9 of host H2 is set to Internel mode, and access to intnet2:

The Linux system configuration of H2 is as follows:

Ifconfig enp0s9 40.40.40.100/24

Do you think H1 and H2 can ping each other's address?

If you try, you'll know. No!

As of November 8, 2019, 6.0.14 is the latest:

Yes, no!

I'll explain Why first, and then demonstrate How.

So, Why?

Unlike VMWare's LAN Segment, which has a virtual learning switch (early on, broadcast Hub), VirtualBox's Internal Network's internal switch is not learning, but registered!

The so-called registered switch is that only the registered Mac address will be forwarded for you! There are only two forwarding strategies for this type of switch:

For broadcast frames, all ports send a copy. For unicast frames, it is sent only to the port that registers the destination Mac address.

Obviously, the guess is that there is a table inside:

Register port Mac address P1Mac1P2Mac2...

So how does the switch register the Mac address? In other words, where can I see which Mac is registered on a network card of a virtual machine?

Please see "Virtual Machine-> Settings-> Network":

Each time a network card is activated, the Advanced below is the registered Mac address of the network card.

The reason why the above experiment does not work is now clear. When H1 ping H2 is broadcast to H2 via Linux Bridge,ARP request, in the ARP broadcast received by H2, the source MAC is naturally the MAC address of H1. When H2 replies to ARP Reply, the destination MAC is the MAC address of H1, while the MAC address of H1 is not registered in the virtual switch of intnet2, so the switch of intnet2 will not forward this frame. So naturally it doesn't make sense!

All right, next, I'll get through the second pulse of his governor and let it through! It's time to demonstrate How.

It only takes three steps:

Register the MAC address of H1 on intnet with the intnet2 network card enp0s10 of Linux Bridge.

Register the MAC address of H2 on intnet2 with the intnet network card enp0s9 of Linux Bridge.

After the Linux Bridge host gets up, use the ifconfig command to change the MAC of its enp0s9,enp0s10 network card to something else.

Let me show you how to do it separately.

We can see that the interface of Nic configuration does not seem to have the option to specify a MAC address, only a button to randomly generate MAC:

What shall I do?

Use the command line!

We use the VBoxManage command to specify the MAC address of a specific network card, as follows:

ZhaoyadeMacBook-Pro:~ $VBoxManage modifyvm "ubuntu new"-- macaddress4 0800279ff0e6zhaoyadeMacBooklu Provu ~ $VBoxManage modifyvm "ubuntu new"-- macaddress3 080027bb3d67

The above 0800279ff0e6 is the MAC address of H1 enp0s9, while 080027bb3d67 is the MAC address of H2 enp0s9, as can be seen from their network configuration interface:

OK, now start the Linux Bridge machine to complete the third step. This step is needed to prevent address conflicts from poisoning the forwarding table of Bridge:

0800279ff0e6 can be learned from the intnet of H1.

0800279ff0e6 is configured on its own enp0s10.

You're confused, aren't you? Therefore, the MAC of the two network cards of Linux Bridge's enp0s9,enp0s10 must be changed to something else, and the Internal Network switch of VirtualBox does not know this modification action (only the new network card is activated or the new MAC is re-registered will change the forwarding table, that is, click that small button or execute the VBoxManage command to change the MAC address), so it does not affect the forwarding table.

All right, let's do this:

Ifconfig enp0s9 hw ether 08:00:27:bb:3d:68ifconfig enp0s10 hw ether 08:00:27:9f:f0:e7

OK, after all the steps are done, let's test:

Brctl addbr br0;brctl addif br0 enp0s9;brctl addif br0 enp0s10;ifconfig br0 up

Ping again?

Doesn't that make sense?

In fact, the bridging of HostOnly mode is similar to that of Intenal Network, and there is such a problem, and it is the same solution.

I don't know whether the problem of being unable to bridge the two networks is intentional by VirtualBox or is it really an implementation flaw, but I really encountered this requirement in my actual work, so in my personal opinion, it is a flaw! This is not as good as VMWare's LAN Segment.

Or does VirtualBox have this problem only on Mac OS? All right on Windows? I don't know. I've been away from the Windows platform for almost two years.

The problem has been solved for almost a day.

At first, I thought it was my brctl configuration problem, such an obvious operation, why didn't it work? I was puzzled. I used HostOnly to do the experiment. Later, by grabbing the packet, I found that the return packet of H1 was directly returned to the vboxnet1 Nic of the Mac OS host, while vboxnet1 did not forward the packet to Linux Bridge. First, it was suspected that it was a BUG of VirtualBox. After rebooting the system to no avail, I roughly guessed what it meant. As for the word "registered switch", I made it up, anyway, and I obviously followed the problem and solved it.

I haven't encountered this kind of problem for a long time, but I still like it and am good at solving this kind of difficult and complicated disease. Personally, I am not very good at writing tutorials on conventional knowledge, but I have my own medicine for this strange problem that people don't usually encounter.

Before finding the answer, I also Google for a long time, looking for something corresponding to LAN Segment on VirtualBox, and looking for the difference between Internal Network and LAN Segment. I even want to find a Virtual Switch on Mac OS to use to create a virtual and real Ethernet. If you really can't find it, you'll have to DIY.

This is the end of the article on "how to bridge two VirtualBox virtual networks in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 262

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report