Skip to content

Instantly share code, notes, and snippets.

@avoidik
Last active September 18, 2022 09:33
Show Gist options
  • Save avoidik/4a30329501b3360dd15f8da4df12a552 to your computer and use it in GitHub Desktop.
Save avoidik/4a30329501b3360dd15f8da4df12a552 to your computer and use it in GitHub Desktop.
Host-only network in Qemu

Host-only network in Qemu

Windows

Install the TAP driver on Windows host

https://build.openvpn.net/downloads/releases/latest/tap-windows-latest-stable.exe

WIN + R > ncpa.cpl > OK

Rename newly created TAP interface to Tap and set its static ip-address, just ip and netmask without gateway, for example 192.168.168.1 and 255.255.255.0

Assign new network device to a Qemu guest

-device virtio-net,netdev=tap0,mac=52:54:00:12:34:57 -netdev tap,id=tap0,ifname=Tap,script=no,downscript=no

Inside the guest VM set static ip-address

ip addr add 192.168.168.2/24 dev enp0s4

Or by using netplan

version: 2
ethernets:
    enp0s4:
        dhcp4: false
        match:
            macaddress: '52:54:00:12:34:57'
        addresses: [192.168.168.2/24]
        set-name: enp0s4

Now you could try to connect from the host to the guest over SSH, for instance

ssh -i ~/.ssh/id_rsa_qemu -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l ubuntu 192.168.168.2

Linux

Create new bridge, tap interface and link them together

sudo ip link add br0 type bridge
sudo ip link set br0 up
sudo ip tuntap add dev tap0 mode tap
sudo ip link set tap0 up promisc on
sudo ip link set tap0 master br0
sudo ip addr add 192.168.168.1/24 broadcast 192.168.168.255 dev br0

Assign new network device to a Qemu guest

-device e1000,netdev=tap0,mac=52:54:00:12:34:57 -netdev tap,id=tap0,ifname=tap0,script=no,downscript=no

Inside the guest VM set static ip-address

ip addr add 192.168.168.2/24 dev enp0s4

Or by using netplan

version: 2
ethernets:
    enp0s4:
        dhcp4: false
        match:
            macaddress: '52:54:00:12:34:57'
        addresses: [192.168.168.2/24]
        set-name: enp0s4

Or run DHCP server on the bridge interface on the host

sudo dnsmasq --interface br0 -p 0 --bind-interfaces --dhcp-range=192.168.168.10,192.168.168.250

Or using dnsmasq configuration file

strict-order
except-interface=lo
bind-dynamic
interface=br0
dhcp-option=3
no-resolv
ra-param=*,0,0
dhcp-range=192.168.168.10,192.168.168.250,255.255.255.0
dhcp-no-override
dhcp-authoritative
dhcp-lease-max=225

If you want to clean up

sudo ip link set tap0 nomaster
sudo ip tuntap del tap0
sudo ip link set down dev br0
sudo ip link del br0

Now you could try to connect from the host to the guest over SSH, for instance

ssh -i ~/.ssh/id_rsa_qemu -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l ubuntu 192.168.168.2

References

https://kevrocks67.github.io/blog/qemu-host-only-networking.html

https://gist.github.com/extremecoders-re/e8fd8a67a515fee0c873dcafc81d811c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment