Install Arch Linux in Virtual Machine (KVM/QEMU)
Contents
Installing Arch Linux on a KVM/QEMU virtual machine is a fairly startightforward process. We will use command line approach in this post.
Create a virtual machine
Here we use virt-install
to create a virtual machine. Run the following as root:
|
|
What is happening here?
- Here we specify name for the virtual machine, which is arch.
- Next we specify type and variant.
- The amount of RAM is specified in megabytes.
- We assign four virtual CPUs to the VM.
- In
disk path
we have given the default location where the virtual machine image is stored. - Size is in gigabyes.
- We have used spice for graphics. Alternatively, VNC can be used.
- In
cdrom
we have provided the location of the boot media. - Finally we asked
virt-install
to use the default network profile.
Pressing Enter will start the creation of the virtual machine. When done, it will start virt-viewer with live media running.
Install Arch Linux
This part is the same as any Arch Linux installation. We partition disks, run bootstrap script, generate locales, set timezone, hostname, install bootloader and optionally a desktop environment.
Partition disks
The virtual storage goes by /dev/sda
. We partition disks using cfdisk
. Select dos partition tabke since we are not using UEFI. Create just one partition and use it as root. Write then quit the program.
Now we format the partition.
|
|
Mount root partition and install
Now we are ready to install Arch Linux. Mount root partition on /mnt
with mount /dev/vda1 /mnt
. Then we run the bootstrap script that will create the necessary files and install the specified packages.
|
|
What is happening here?
- Here we asked
pacstrap
to install Arch Linux on/mnt
. - Then we specified the packages to be installed.
- The meta package
base
contains a minimal package set that defines a basic Arch Linux installation. It includes basic utilities like glibc, bash, coreutils, networking tools, systemd, etc. - Next is the
base-devel
group. It contains tools to build many packages like gcc, make, binutils, etc. linux
is the kernel.- We require
vim
to configure various things. Any text editor will do. dhcpcd
will allow us to connect to the internet. Alternatively,networkmanager
can be used.
Generate fstab
This file tells which partitions to mount during boot. Instead of writing by hand, we use a tool to simplify things for us. Running the following
|
|
will generate a fstab file. We asked it to use /mnt
partition. Any partition under /mnt
will also be included, so if you created separate /home
and /var
partitions, they will also be included. The -U
parameter tells genfstab
to use UUID.
Configuration
In this section we set timezones, locale and hostname.
Chroot into new root
|
|
Set timezone
List timezones by running timedatectl list-timezones
. Set timezone with
|
|
Replace Asia/Kolkata with your timezone.
Set locale
Uncomment the desired locales in locale.gen
|
|
Save and exit then generate them by running
|
|
And set locale config
|
|
Replace en_US.UTF-8 with your locale.
Configure network
Set hostname
Your virtual machine will be identified by this name on the network
|
|
Create /etc/hosts
Your computer will look here for resolving domain names before looking out on the internet. Put the following information in /etc/hosts
:
|
|
Enable dhcpcd
This will enable the virtual machine to connect to the internet
|
|
Create a user
Before we create a regular user, it becomes necessary to set a root password. Set it by running
|
|
Create a new user with
|
|
What is happening here?
-m
tellsuseradd
ro create a home directory for this user.-G
tells which groups this user will belong to.-s
specifies the login shell for this user.- Replace <username> with a username of your choice.
Set a password for this user with
|
|
Install ssh server
If working on command line is all you have to do, then working via SSH is more convenient than using Spice client (or VNC if you chose that). Install it
|
|
and enable it
|
|
Install bootloader
The instructions thus far will provide us a working bare minimum Arch Linux environment. Now it is time to install a bootloadet and finish the installation. We will go with grub.
|
|
What is happening here?
- First we install grub package.
- Then we install it on the device
/dev/vda
. - Next we create a configuration file. It can be modified at
/etc/default/grub
. Every time we modify the configuration, it must be generated with the last command for the changes to take place.
Reboot
The installation is complete. Next time we boot, we will be dropped into a working Ach Linux environment.
Exit chroot.
|
|
Unmount partition
|
|
And reboot
|
|
Connecting to the virtual machine
Wait for a minute and the virtual machine will be up. FInd the virtual machine’s IP address on host machine (assuming arch is the name of your virtual machine)
|
|
Save the IP address in your host’s /etc/hosts
|
|
Replace <ip-address>
with the IP address of the virtual machine and <guest hostname>
with the hostname you set while installing the virtual machine. It wuold be something like
|
|
Create SSH key pair
While not necessary on a local network, it is generally a good habit to use key pair instead of password authentication.
Go to ~/.ssh
and create a key pair.
|
|
What is happening here?
ssh-keygen
generates keys.-t
specifies the type of the key. We chose Ed25519. Other popular types are EdDSA, RSA, DSA, etc.-f
specifies the filename of this key pair.-C
is used to put comment in the key.
This process will ask for a passphrase and generates a private key and a public key. The public key will have extension .pub
.
Create a config file. This will tell SSH which key to use with respect to host. The file is named config
. Put the following in the file
|
|
The private key is used to identify you. Anybody with your private key will be impersonate you so keep it safe.
Now copy the public key over to your virtual machine. It can be achieved by programs like scp
or rsync
. However, we will use ssh-copy-id
to conveniently put our key while abstracting most parts.
|
|
It is assuming arch is the username of the normal user of the virtual machine.
Now log in by running
|
|
Modify /etc/ssh/sshd_config
to only use public key for authentication.
- Change
PubkeyAuthentication
toyes
- Change
PasswordAuthentication
tono
- Change
X11Forwarding
toyes
Now restart sshd
for changes to take place.
|
|
Exit and try logging in again. This time it should ask for passphrase.
Install a desktop environment
This part is entirely optional if you wish to use GUI. In this post, we will install MATE desktop environment.
|
|
The desktop environment can be started by running exec mate-session
. If you want to use xinit then put the command in ~/.xinitrc
. Then install xorg-xinit
package. Start it with startx
.
Alternatively you can use a display manager like GDM, LightDM, SDDM. etc. Install sddm
, enable and start it
|
|
If you wish to run GUI programs over X11 forwarding, a desktop environment might be overkill. In that case just install xorg
, xorg-server
. To use X11 forwarding, log in via ssh with
|
|
Author Aditya
LastMod 2022-05-07 (f8e6362)