1# Network 2 3The most convenient way to provide a network device to a guest is to setup a persistent TAP 4interface on the host. This section will explain how to do this for basic IPv4 connectivity. 5 6```sh 7sudo ip tuntap add mode tap user $USER vnet_hdr crosvm_tap 8sudo ip addr add 192.168.10.1/24 dev crosvm_tap 9sudo ip link set crosvm_tap up 10``` 11 12These commands create a TAP interface named `crosvm_tap` that is accessible to the current user, 13configure the host to use the IP address `192.168.10.1`, and bring the interface up. 14 15The next step is to make sure that traffic from/to this interface is properly routed: 16 17```sh 18sudo sysctl net.ipv4.ip_forward=1 19# Network interface used to connect to the internet. 20HOST_DEV=$(ip route get 8.8.8.8 | awk -- '{printf $5}') 21sudo iptables -t nat -A POSTROUTING -o "${HOST_DEV}" -j MASQUERADE 22sudo iptables -A FORWARD -i "${HOST_DEV}" -o crosvm_tap -m state --state RELATED,ESTABLISHED -j ACCEPT 23sudo iptables -A FORWARD -i crosvm_tap -o "${HOST_DEV}" -j ACCEPT 24``` 25 26The interface is now configured and can be used by crosvm: 27 28```sh 29crosvm run \ 30 ... 31 --net tap-name=crosvm_tap \ 32 ... 33``` 34 35Provided the guest kernel had support for `VIRTIO_NET`, the network device should be visible and 36configurable from the guest: 37 38```sh 39# Replace with the actual network interface name of the guest 40# (use "ip addr" to list the interfaces) 41GUEST_DEV=enp0s5 42sudo ip addr add 192.168.10.2/24 dev "${GUEST_DEV}" 43sudo ip link set "${GUEST_DEV}" up 44sudo ip route add default via 192.168.10.1 45# "8.8.8.8" is chosen arbitrarily as a default, please replace with your local (or preferred global) 46# DNS provider, which should be visible in `/etc/resolv.conf` on the host. 47echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf 48``` 49 50These commands assign IP address `192.168.10.2` to the guest, activate the interface, and route all 51network traffic to the host. The last line also ensures DNS will work. 52 53Please refer to your distribution's documentation for instructions on how to make these settings 54persistent for the host and guest if desired. 55