Difference between revisions of "Install Linux in a virtual machine"

From Computer Science
Jump to navigationJump to search
Line 4: Line 4:
 
Linux, but this will get you a running system that you can play with.
 
Linux, but this will get you a running system that you can play with.
  
Download the install media [http://www.cs.middlebury.edu/~rlichenstein/Files/middlinux-2020.02.09-x86_64.iso here].
+
Download the install media [http://www.cs.middlebury.edu/~rlichenstein/Files/middlinux-2020.04.16-x86_64.iso here].
  
  

Revision as of 21:32, 16 April 2020

Guides> linux > Install Linux in a virtual machine

NOTE: These instructions are not canonical. There are many ways of installing Linux, but this will get you a running system that you can play with.

Download the install media here.


Boot the virtual install media

Select your virtual machine from the list. Click "Settings". Select "Storage". Under the "Controller: IDE" heading, select the stylized DVD labeled "Empty". In the right pane (labeled Attributes), on the line labeled "Optical Drive", click the button with the stylized DVD. Select "Choose Virtual Optical Disk File..." Navigate to and select the install media you just downloaded. Click Ok.

Click the big green arrow labeled "Start".

You'll see a menu show up: hit Enter to select the default option, "Boot Arch Linux (x86_64)".

A bunch of opaque nonsense will fly by and eventually you'll be presented with a prompt that looks something like this:

Arch Linux 5.4.8-arch1-1-ARCH (tty1)

archiso login: root (automatic login)

root@archiso ~ # _

This tells you that you've been automatically logged in as the user "root" on the machine called "archiso" and that your current working directory is "~" (i.e., root's home directory). The "#" is the prompt awaiting input. The "\_" is the cursor. (The name of the program awaiting input is not Linux itself, but rather a program Linux has started for you, precisely to accept your input and do your bidding. This program is called the *shell*.)

Prepare disk

Before you can install anything on the virtual disk inside your virtual machine, you need to partition it and format the partition.

Use the `fdisk` program to partition the disk, referred to as `/dev/sda`.

root@archiso ~ # fdisk /dev/sda

You'll see some mildly informative messages and then a different prompt indicating that `fdisk` (rather than the shell) is awaiting input. Like the shell, `fdisk` expects you to enter a command (most are one letter) followed by Enter.

Command (m for help): _

Enter the command "n" to create a new partition; select "p" to make it a primary partition; with the partition number "1"; the default first sector and the default last sector. (For any of these, you can press enter to pick the default instead of actually typing the option explicitly.) Here's a transcript from my run through these steps:

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-33554431, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-33554431, default 33554431):

Created a new partition 1 of type 'Linux' and of size 16 GiB.

Note that for first sector and last sector I just hit enter to pick the default. Some of the exact numbers might be different depending on the size of the virtual hard drive you created previously.

You should now be back at the `fdisk` prompt. Enter "p" to print the partition table; the list of partitions at the end should look very similar to this:

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1        2048 33554431 33552384  16G 83 Linux

If it doesn't, enter the command "d" to delete the partition and re-create it according to the instructions in the previous pair of paragraphs.

Once all is well, enter the command "w" to write the partition table to disk and exit `fdisk`. Upon doing so, you should once again see the shell prompt.

Now format the partition you just created:

root@archiso ~ # mkfs.ext4 /dev/sda1

And then make that formatted partition available to the running system:

root@archiso ~ # mount /dev/sda1 /mnt

Install and configure

Install the base set of packages into the virtual drive. This is the bare minimum of software required for a running system (base), along with the graphical environment (XFCE), some tools to make running inside VirtualBox easier (virtualbox-guest-utils), code that gets the system up and running (grub), and the program that lets you run *other* programs with elevated privileges (sudo).

root@archiso ~ # pacstrap -c /mnt base xfce4 xorg-server virtualbox-guest-utils ttf-dejavu dhcpcd grub sudo man man-pages

The preceding command will produce an error if you mistype any of the package names. For instance, if I misspell "sudo" as "sud" it will complain at me like so:

error: target not found: sud
==> ERROR: Failed to install packages to new root
1 root@archiso # _

If you see such an error, press the up arrow to bring up the previous command, fix the typo, and re-run the command. When successfully complete, it will print a message telling you how long it took, followed by a new prompt, e.g.:

pacstrap -c /mnt base xfce4 xorg-server grub   38.73s user 16.03s system 80% cpu 1:07.99 total
root@archiso # _

Now you're ready to make sure the partition you've just created gets mounted at runtime:

root@archiso ~ # genfstab -p /mnt >> /mnt/etc/fstab

Your new installation is located at /mnt. Start up a new shell that thinks its whole world is the collection of files under /mnt, called a *chroot*. (Normally you'd just use the `chroot` program, but for this particular purpose, there are some other things that need to happen, so the Arch Linux maintainers have wrapped the raw chroot command up in the bigger script you'll be running.)

root@archiso ~ # arch-chroot /mnt

On success, your prompt should change to a stripped-down version (because this is a shell running in a stripped-down environment, and thus hasn't been customized to show things like username, hostname, and current working directory):

[root@archiso /]#

Replace "middlinux" in the following command to set the computer name to anything you like (though limit yourself to lowercase letters, numbers, and underscore).

[root@archiso /]# echo middlinux > /etc/hostname

Set the timezone. (This assumes the hardware clock on the host machine is set to UTC rather than localtime. It's entirely probable this assumption is invalid; we can deal with it later, it's not a big deal even if it's never fixed.) The `date` command should show the correct timezone.

[root@archiso /]# ln -snf /usr/share/zoneinfo/US/Eastern /etc/localtime
[root@archiso /]# date

Set up localization. The sed command uncomments all lines that contain the string "en_US", thus enabling US-centric English-language localization, as opposed to British (en_UK), Australian (en_AU), or Canadian (en_CA); or, alternatively, completely different locales like China (zh_CN, zh_HK, zh_TW, etc).

[root@archiso /]# mv /etc/locale.gen /etc/locale.gen.pacnew
[root@archiso /]# sed -ne '/^#en_US/ s/^#//p' /etc/locale.gen.pacnew > /etc/locale.gen
[root@archiso /]# locale-gen
Generating locales...
  en_US.UTF-8... done
  en_US.IDO-8859-1... done
Generation complete.
[root@archiso /]# echo "LANG=en_US.UTF-8" > /etc/locale.conf


Edit our boot environment creation script, to avoid unruly bugs.

[root@archiso /]# sed -i -e '/^MODULES=/ s/()/(i915)/' /etc/mkinitcpio.conf


Create the initial boot environment, customized to your particular system. (You will probably see a couple warnings about possibly missing firmware: you can ignore these.)

[root@archiso /]# mkinitcpio -p linux
(lots of messages)

Install and configure the bootloader.

[root@archiso /]# grub-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
[root@archiso /]# grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-linux
Found initrd image(s) in /boot: initramfs-linux.img
Found fallback initrd image(s) in /boot: initramfs-linux-fallback.img
done

Make sure networking starts at boot. (The last letter of the first word is the letter ell, *not* the number one.)

[root@archiso /]# systemctl enable dhcpcd@enp0s3
Created symlink
/etc/systemd/system/multi-user.target.wants/dhcpcd@enp0s3.service -> /usr/lib/systemd/system/dhcpcd@.service

Enable some VirtualBox-specific services.

[root@archiso /]# systemctl enable vboxservice
/etc/systemd/system/multi-user.target.wants/vboxservice.service -> /usr/lib/systemd/system/vboxservice.service
[root@archiso /]# echo vboxsf > /etc/modules-load.d/vbox.conf

Configure sudo, which allows one user to run a program using the privileges of another. (Usually used to perform administrative activities without having to log out and log back in as root.) The following `sed` command reads the file `/etc/sudoers`, searches for all lines that contain the string within the first pair of forward slashes, and removes the leading # and space from those lines.

[root@archiso /]# sed -i -e '/wheel ALL=(ALL) ALL$/ s/^# //' /etc/sudoers

Set the root (superuser, administrator) password and exit from the chroot. (Note that it won't even print asterisks to let you know you're typing.)

[root@archiso /]# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
[root@archiso /]# exit

It should print an account of the time you spent in the chroot and then the prompt should now revert to what it was before:

exit
arch-chroot /mnt  10.96s user 2.74s system 5% cpu 4:26.84 total
root@archiso ~ #

Remove the virtual install media from the virtual drive

Shut down the system:

root@archiso ~ # halt -p

Now, in the VirtualBox UI, select your virtual machine from the list. Click "Settings". Select "Storage". Under the "Controller: IDE" heading, select the stylized DVD labeled with the name of the install media. To the right, click the stylized DVD with the little down-arrow. Select "Remove Disk from Virtual Drive". Click Ok.


Congratulations

Proceed to your first boot.


Commands Only

The below commands are just the instructions from above, without any annotation.

root@archiso ~ # fdisk /dev/sda
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-33554431, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-33554431, default 33554431):

Created a new partition 1 of type 'Linux' and of size 16 GiB.

Command (m for help: w
root@archiso ~ # mkfs.ext4 /dev/sda1
root@archiso ~ # mount /dev/sda1 /mnt
[root@archiso /]# pacstrap -c /mnt base xfce4 xorg-server virtualbox-guest-utils ttf-dejavu dhcpcd grub sudo man man-pages
[root@archiso /]# genfstab -p /mnt >> /mnt/etc/fstab
[root@archiso /]# arch-chroot /mnt
[root@archiso /]# echo middlinux > /etc/hostname
[root@archiso /]# ln -snf /usr/share/zoneinfo/US/Eastern /etc/localtime
[root@archiso /]# date
[root@archiso /]# mv /etc/locale.gen /etc/locale.gen.pacnew
[root@archiso /]# sed -ne '/^#en_US/ s/^#//p' /etc/locale.gen.pacnew > /etc/locale.gen
[root@archiso /]# locale-gen
[root@archiso /]# echo "LANG=en_US.UTF-8" > /etc/locale.conf
[root@archiso /]# sed -i -e '/^MODULES=/ s/()/(i915)/' /etc/mkinitcpio.conf
[root@archiso /]# mkinitcpio -p linux
[root@archiso /]# grub-install /dev/sda
[root@archiso /]# grub-mkconfig -o /boot/grub/grub.cfg
[root@archiso /]# systemctl enable dhcpcd@enp0s3
[root@archiso /]# echo vboxsf > /etc/modules-load.d/vbox.conf
[root@archiso /]# sed -i -e '/wheel ALL=(ALL) ALL$/ s/^# //' /etc/sudoers
[root@archiso /]# passwd
[root@archiso /]# exit
root@archiso ~ # halt -p