Installing Gentoo Linux on a Vultr Server

Updated on November 21, 2023
Installing Gentoo Linux on a Vultr Server header image

According to Gentoo's official site,

Welcome to Gentoo, a flexible, source-based Linux distribution that becomes just about any system you need—and much more.

As you can see, installing Gentoo on your beloved Vultr machine enables you to manage your server in a more controlled manner, as you choose what's on the server and even more, you choose which features to compile in in order to save the precious resources on your server.

For a Gentoo installation on Vultr we basically need to do the following things:

  • Load the installation ISO onto your Vultr account.
  • Create a machine using the Custom ISO template.
  • Fire up sshd in the LiveCD environment.
  • Install the OS

Let's start!

Preparation work

First navigate to the ISO Management page and upload your ISO from remote. Almost any medium is fine for installing Gentoo, and we'll use the official minimal CD. Fill this (or another minimal Gentoo ISO link) into the URL box and hit upload, then wait a few minutes. After the ISO has been successfully uploaded, we can proceed to creating a machine.

Choose Custom ISO in the operating system section. Choose other options according to your need, and remember to check Enable IPv6 as we'll be deploying a IPv6-enabled install. Once you're sure that everything is correct, create the machine and fire it up.

Choose View console in the machine's control panel and you will be greeted with Gentoo minimal CD's shell. As operations from the VNC can be difficult, we'll enable sshd. Use passwd to change your root password, and then do the following:

/etc/init.d/sshd start

Then find the server IP address in your control panel and connect to the server with:

ssh root@203.0.113.1

Note that any IP address which falls in 203.0.113.0/24 is virtual and you should replace them with your server's real IPv4 address.

Installation

Now that we get to a proper shell in LiveCD, we can start the installation process. Basically we'll be doing the following things:

  • Partition and mount the disk.
  • Get and unpack the stage3 tarball.
  • Grab the portage tree.
  • Chroot into the target environment.
  • Select profile and perform world update.
  • Install necessary tools.
  • Reboot and check.

First, we'll divide the disk. Use cfdisk:

cfdisk /dev/vda

Choose mbr in the popup and create 2 partitions: one is your root partition and the other is your swap partition. I recommend you reserve 2*memory size on small memory plans while smaller swaps are possible if you have a lot of physical memory. Write the partition table and quit after you have made the right alternations.

Then format the partitions. We will just use ext4 here. You can choose whatever you want, but remember to enable support for that fs later when configuring the kernel, or the system won't boot. Do the following:

mkfs.ext4 /dev/vda1
mkswap /dev/vda2

Assuming that your root partition is vda1 and swap partition is vda2.

Then mount the filesystems.

mount /dev/vda1 /mnt/gentoo
swapon /dev/vda2

Grab the LATEST stage3 tarball (look what's inside the folder on the mirror first!) from a mirror which is geographically close to your server. Unpack the tarball into the new root.

cd /mnt/gentoo
wget http://ftp.iij.ad.jp/pub/linux/gentoo/releases/amd64/autobuilds/current-stage3-amd64/stage3-amd64-20161103.tar.bz2
tar xvjpf stage3-amd64-20161103.tar.bz2 --xattrs

We'll need to choose a source which is geographically close to the server in order to save time while downloading source codes. Gentoo's minimal install CD provides us with a perfect tool mirrorselect. Select the ones that are in your server's region:

mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf

For example, a server located in Tokyo would want to have IIJ and Jaist's mirror enabled, so hit Spacebar and mark those lines with a *. Exit and review make.conf changes:

nano -w /etc/portage/make.conf

A second important step in selecting mirrors is to configure the main Gentoo repository via the /etc/portage/repos.conf/gentoo.conf file. Definitions about the remote source of portage tree is located there. Do the following:

mkdir -p /mnt/gentoo/etc/portage/repos.conf
cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
nano -w /mnt/gentoo/etc/portage/repos.conf/gentoo.conf

Substitute the server address in the sync-uri setion. A list of rsync mirrors can be found here. Regional rsync servers are usually provided by using URLs like rsync://rsync.jp.gentoo.org/gentoo-portage/. This round-robin way will ensure that the configuration will still work even if some servers failed in the server pool.

A little hint here: as rsync is costly, many servers don't allow too many frequent syncs. I recommend you sync once daily or less frequently. If you try to (even involuntarily) disturb the server's rotation, you may be added to a temporary ban list.

Copy resolv.conf into the chroot.

cp -L /etc/resolv.conf /mnt/gentoo/etc/

Mount necessary filesystems so that we can chroot.

mount -t proc proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev

Then chroot into the environment.

chroot /mnt/gentoo /bin/su
export PS1="(chroot) $PS1"

Grab the portage snapshot.

emerge-webrsync

Read the news:

eselect news list
eselect news read

Select the profile:

eselect profile list
eselect profile set 1

Do world update:

emerge -avuDN @world

Set the timezone. Suppose the timezone of choice is Europe/Brussels:

echo "Europe/Brussels" > /etc/timezone
emerge --config sys-libs/timezone-data

Set the locale.

nano /etc/locale.gen
locale-gen
eselect locale list
eselect locale set en_US.UTF-8

Reload the environment.

env-update && source /etc/profile && export PS1="(chroot) $PS1"

Install the Linux kernel. Emerge the source first:

emerge -av sys-kernel/gentoo-sources

Then configure the kernel. As we're performing the installation on a VPS, we need to enable VirtIO related options. You can use the standard menuconfig tool:

make menuconfig

Use / key to search for VIRTIO_PCI and VIRTIO_MMIO. After you have located them, enable them as well as the new items that may show up after enabling these two. Also select Maintain a devtmpfs file system to mount at /dev so that critical device files are already available early in the boot process (CONFIG_DEVTMPFS and CONFIG_DEVTMPFS_MOUNT).

Save and quit the menuconfig.

If you have a rather small terminal or you're not fond of searching around for options, you can also hack .config on your own. Just search for VIRTIO and change

# CONFIG_VIRTIO_PCI is not set
# CONFIG_VIRTIO_MMIO is not set
# CONFIG_VIRTIO_BLK may not be set

to:

CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_MMIO=y
CONFIG_VIRTIO_BLK=y

CONFIG_DEVTMPFS=y
CONFIG_DEVTMPFS_MOUNT=y

... and answer y to all the questions about VIRTIO in the following kernel compile section (but be sure to read the prompts before pressing enter).

We're going to compile the kernel now. Once you have returned to the shell, do:

make -j2 && make modules_install

The -j2 argument here means that the compile will run concurrently with 2 jobs. Change this to the number of your machine's cores plus one to best shorten the compile time. After you have started the compile, grab a cup of coffee and relax!

After the compile has finished, install the kernel to /boot by:

make install

Then let's configure the fstab.

nano -w /etc/fstab

Change /dev/ROOT to /dev/vda1 and /dev/SWAP to /dev/vda2, and comment out all the other lines.

Change the hostname to match the one you set when creating the machine.

nano /etc/conf.d/hostname

Install net-misc/netifrc and configure the network.

emerge -a --noreplace net-misc/netifrc
nano /etc/conf.d/net

And add the following line:

config_eth0="dhcp"

Start the network interface at boot time.

cd /etc/init.d
ln -s net.lo net.eth0
rc-update add net.eth0 default

Edit the hosts file.

nano -w /etc/hosts

Change the root password.

passwd

Install various utilities including syslog-ng, logrotate, cronie, and dhcpcd.

emerge -av syslog-ng logrotate cronie dhcpcd

Enable the services:

rc-update add sshd default
rc-update add cronie default
rc-update add syslog-ng default

Review sshd_config (make sure that you don't lock yourself out!):

nano -w /etc/ssh/sshd_config

Install the grub bootloader:

emerge -a sys-boot/grub:2

Install the bootloader and generate the config:

grub-install /dev/vda
grub-mkconfig -o /boot/grub/grub.cfg

Test the installation

We're all set now! Exit from the chroot and issue the ultimate command for testing:

exit
reboot

If everything is correct, you'll be able to log in via ssh in moments after the system has booted up. If you encounter problems, don't panic. Use the VNC console as well as the minimal ISO to check what was wrong, and fix the errors.

You now have a brand-new Gentoo install sitting on your Vultr machine. Enjoy!