This is an old revision of the document!
How to Create a Bridge Connection Between PC and VM
Prerequisites
Verify that the necessary packages are installed. For Ubuntu, you will need the bridge-utils package.
USER
#-$ sudo apt-get install bridge-utils
Step 1
- Identify the physical network interface of the PC using the following command and note down the name of the physical interface (e.g., eth0, enp1s0, etc.):
USER
#-$ ip link show
Step 2
Configure the bridge on the PC
- Open the network configuration file using a text editor at “/etc/netplan/” (Ubuntu)
- Create a new configuration file (e.g., bridge.yaml) or modify an existing one and paste the following configuration:
USER
network: version: 2 renderer: networkd ethernets: pc_interface_name: #replace pc_interface_name with the network interface you noted before dhcp4: no dhcp6: no bridges: br0: interfaces: [pc_interface_name] addresses: [192.168.1.128/24] //You can choose a different address if you want gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8]
!!Note!!
This only works with wired network. You have to replace networkd with NetworkManager to have your internet access through wifi back
- Apply the network configuration:
USER
#-$ sudo netplan apply
- This will create the bridge interface (br0) and associate it with the physical interface you can check that using:
USER
#-$ ip -br a s
- If not you can restart your network:
USER
#-$ sudo systemctl restart network-online.target
- Or:
USER
#-$ sudo systemctl restart systemd-networkd
Step 3
Configure the VM to use the bridge:
- Make sure the vm is shut off before applying any changes , check using the following command:
USER
#-$ virsh list –all
- Modify the XML configuration file of the VM to use the bridge interface (br0) instead of a virtual network you can do that usig the following commands or manually which i'll be mentioning it afterwards:
- Determine the name of the VM's network interface: Run the following command on the host machine to list the available network interfaces of the VM:
USER
#-$ virsh domiflist
- Connect the VM to the bridge interface:
USER
#-$ virsh attach-interface –domain vm_name –type bridge –source br0 –model virtio –config –live
Manually
- Locate the XML configuration file of the VM (e.g., /etc/libvirt/qemu/<vm_name>.xml)and open it with a text editor of your choice
- Locate the tag <interface> and add the follwing after the closing tag of interface </interface>:
USER
<interface type='bridge'> <mac address='52:54:00:09:39:b6'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x08' slot='0x00' function='0x0'/> </interface>
- Save the changes and exit
Step 4
Assign the ip address
- Launch the VM using:
USER
#-$ virsh start VM_name
- On the VM Terminal check the network interfaces names using :
USER
#-$ ip -br a s
- You will notice that there is an additional interface named (e.g: enp9s0) if it's DOWN use this command to set it UP:
USER
#-$ ip link set dev enp9s0 up
- Replace enp9s0 according to your interface name
- Add an ip address of your choice:
USER
#-$ ip address add 192.168.1.182/24 dev enp9s0
- Replace enp9s0 according to your interface name
- Check if it's running using :
USER
#-$ ip address show dev enp9s0
- Verify connectivity by accessing the VM from the PC:
USER
#-$ ping 192.168.1.182 # change the address acording to your configuration
- Or:
USER
#-$ ssh vm_username@192.168.1.182
- Change the address according to your configuration