This tutorial will explain for QEMU virtual machine users how you can make two-ways network connection between host and guest on Ubuntu Desktop. This enables internet access automatically for the guest if available on the host. This tutorial gives you an example exercise, that is, to remote login with SSH from host to guest and reversely from guest to host. Now let's start!
Subscribe to UbuntuBuzz Telegram Channel to get article updates.
Requirements
- Host OS: Ubuntu Desktop 21.10
- Guest OS: Ubuntu Desktop 16.04
- Hardware: Intel VT or AMD-V enabled
- Host memory: 4GB minimum, 6GB recommended for graphical OSes
- Software: AQEMU with KVM capability
- You are capable to do basic command lines
- Administrator privilege not required
Command Line
$ qemu-system-x86_64 -enable-kvm -smp 2 -m 2G -cdrom ubuntu-16.04-desktop-amd64.iso -nic user,hostfwd=tcp::8888-:22
Explanation:
- qemu-system-x86_64: the qemu computer emulator for PC 64-bit architecture.
- -enable-kvm: the virtual machine. With this, your virtualization runs faster.
- -smp 2: determine to use 2 CPU cores instead of 1.
- -m 2G: determine to use 2 GigaByte of RAM.
- -cdrom [filename]: determine to use the [filename] image file. In this example, [filename] is ubuntu-16.04-desktop-amd64.iso.
- -nic user: enable user mode networking that works in two-ways.
- ,hostfwd=tcp::8888-:22: connect between host and guest by their port numbers, respectively, 8888 and 22. As a result, when host user connects to localhost at port 8888, they will be automatically connected ("forwarded") to guest at port 22.
Step 1. Preparation
Prepare the ISO image file in your Home directory.
Step 2. Virtual Machine Setup
Run the command line above.
Step 3. IP Address Check
Virtual machine runs. The guest OS is now connected to network (including the internet, if any) to the host OS. Now you should know the IP address of both guest and host.
- On host: run the command line 'ip addr' and look at either your wired or wireless interface's IP address. At ours, we use wireless internet and ours is 192.168.1.96.
- On guest: by default, it is 10.0.2.15. If you are unsure, use 'ip addr' command.
Step 4. Install SSH Server on Host and Guest
Installing SSH server program on both host and guest can be done with same command line below:
$ sudo apt-get install openssh-server
For guest, you will need to setup a password first for the default username 'ubuntu' with whatever password you like:
$ sudo passwd ubuntu
Step 5. Connect
As the exercise, we will try to remote login from host to guest and vice versa.
From host to guest:
$ ssh ubuntu@127.0.0.1 -p 8888
From guest to host:
$ ssh master@192.168.1.96
Note:
- ubuntu is guest username.
- master is host username.
- 127.0.0.1 is localhost IP address at host.
This example of SSH connection is just one of many examples possible. You can switch it instead with another ones, like FTP server, mail server, or even more modern, Nextcloud server. Try it.
Final Result
Now, you should be able to connect via secure shell between both host and guest in two-ways. As a result, you now can browse freely the filesystem of either one from another as well as run any command line remotely, for example, do a shutdown. Happy hacking!
References
https://wiki.archlinux.org/title/QEMU
https://www.linux-kvm.org/page/Networking
https://en.wikibooks.org/wiki/QEMU/Networking
This article is licensed under CC BY-SA 3.0.