I've been meaning to setup more consistant servers for awhile in my spare time outside of work. I never got around to implementing an Ansible server to deploy but was something I was looking into. However one thing that seemed easier to implement in my pretty simple setup would be to use XCP-NG/XOA VM Templates. VM Templates inside the hypervisor is what I ended up using for my use-case and they work great. Be aware that when copying Virtual Machines or cloning drives the uniqueness and ssh fingerprints need to be regenerated so that they are all unique. One extra step I would recommened doing to save time and hassel would be. When you create the VM, take snapshots at any semi-significant step. Then at the end, don't turn this VM into a template, make a full copy of this VM, then turn that copy into a template. This allows more flexibility as in the future if I need to add another tool, key, user, setting, system updates, ect. I just boot up the VM, make my change, run my cleanup script, shutdown, Copy, Make New Template, Rename or Delete old Template.

Here are the steps I take for setting up my Virtual Machine Template.

When initially setting up the Virtual Machine. Choose EUFI Bios Type, and install as LVM Partition (Logicial Volume Manager). This is important as it makes resizing the disk later much easier. I'd recommend not setting a Root Password. You can use `sudo su -` to switch to root with your user(s) password to become root if needed.

Add Apps and Tools

add a bunch of packages & tools.

sudo apt install cloud-guest-utils fzf ripgrep neovim vim rsync htop btop ufw wget curl tar tealdeer tmux byobu -y

Xen Virtual Machine Guest Agent Tools

sudo nvim /etc/apt/sources.list.d/xen-guest-agent.list
deb [trusted=yes] https://gitlab.com/api/v4/projects/xen-project%252Fxen-guest-agent/packages/generic/deb-amd64/ release/
sudo apt update
sudo apt install xen-guest-agent -y
sudo apt clean
sudo apt autoremove

Personal Preferences

put my basic nvim config in so I can have some basic setup like line numbers, preferences, ect.

mkdir -p /home/jake/.config/nvim/
nvim /home/jake/.config/nvim/init.vim
set nocompatible
set hlsearch
filetype on
filetype plugin on
filetype indent on
syntax on
set number
set shiftwidth=4

" Set tab width to 4 columns.
set tabstop=4

" Use space characters instead of tabs.
set expandtab

Custom Scripts to Prepare things

  1. Script to easily expand the disk size after increasing it in hypervisor (XCP-NG/XOA)
/usr/local/sbin/jake-grow-vm-disk.sh
#!/bin/bash
# 1) Grow the partition that holds the LVM PV
growpart /dev/xvda 3
# 2) Resize the LVM physical volume
pvresize /dev/xvda3
# 3) Extend root LV to all free space
lvextend -l +100%FREE /dev/deb13temp-vg/root
# 4) Grow the filesystem (ext4)
resize2fs /dev/deb13temp-vg/root
  1. Script to clean up system to prep for shutdown and cloning
/usr/local/sbin/jake-cleanup-vm-prep-clone.sh
#!/bin/bash
sudo rm -f /etc/systemd/network/*.network
sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
sudo rm /etc/machine-id && sudo touch /etc/machine-id
sudo rm /var/lib/dbus/machine-id && sudo touch /var/lib/dbus/machine-id
sudo rm /var/lib/systemd/random-seed && sudo touch /var/lib/systemd/random-seed
sudo apt-get clean
  1. after cloning regenerate the ssh fingerprints
sudo nvim /usr/local/sbin/jake-new-clone-fingerprints.sh
#!/bin/bash
sudo rm /etc/ssh/ssh_host_*
sudo ssh-keygen -A
sudo systemctl restart sshd
  1. fix permissions for gamegroup after game installs and modifications
sudo nvim /usr/local/sbin/jake-fix-permissions-gamegroup.sh
#!/bin/bash
chown -R lefty:gamegroup /home/lefty/
find /home/lefty/ -type d -exec chmod 770 {} +
  1. make all the custom scripts executable
sudo chmod +x /usr/local/sbin/jake*.sh

Groups (Optional)

create any preferred groups

groupadd docker

add the users and add them to the group

useradd -m -G docker myserviceuser
useradd -m -G docker user3

usermod -a -G docker jake
usermod -a -G systemd-journal jake
usermod -a -G systemd-journal myserviceuser
usermod -a -G systemd-journal user3

Setup your user's shell preferences

usermod -s /bin/bash myserviceuser
usermod -s /bin/bash user3

Create a separate user to run any services under. Don't grant them sudo privledge, don't make them sudoers. More restrictions can be placed on them later. In these examples I named my restricted user myserviceuser

Optionally if you prefer to login as them temporarily while doing setup, updates, adding mods, ect.

sudo -u myserviceuser /bin/bash/

Firewall Security

Going to use UFW Uncomplicated Firewall

sudo ufw allow OpenSSH
sudo ufw reload
sudo ufw enable

create the application profiles for easier management (optional)

sudo nvim /etc/ufw/applications.d/ufw-jake-app1
sudo nvim /etc/ufw/applications.d/ufw-jake-app2
sudo nvim /etc/ufw/applications.d/ufw-jake-app3

Here is what they look like

root@deb13temp:~# cat /etc/ufw/applications.d/ufw-jake-app1
[APP-NAME-HERE]
title=TitleGoesHere
description=DescriptionGoesHere
ports=27015:27020/udp

Only run the cmd for the required firewall rules

sudo ufw allow APP-NAME-HERE comment 'Allowing X for the Taco Service'

finally turn on ufw and check the status

sudo ufw reload
sudo ufw enable
sudo ufw status verbose

Security Lock Down

nvim /etc/ssh/sshd_config.d/01-jake-server.conf
nvim /etc/ssh/sshd_config.d/02-game-server.conf
root@deb13temp:~# cat /etc/ssh/sshd_config.d/01-jake-server.conf
AllowUsers jake
PasswordAuthentication no
PermitRootLogin no
root@deb13temp:~# cat /etc/ssh/sshd_config.d/02-game-server.conf
AllowUsers user3,user4
DenyUsers myserviceuser

I'd recommend referencing the book SSH Mastery - by michael w. lucas. It helped me to better understand how to configure and troubleshoot ssh configurations.

Another security setup is to not allow the restricted service user to login normally

usermod -s /usr/sbin/nologin myserviceuser

You can still access bash to do things as this user via:

sudo -u myserviceuser /bin/bash

Finale

sudo jake-cleanup-vm-prep-clone.sh
sudo cat /dev/null > ~/.bash_history && history -c
sudo shutdown now

At this point make a copy of your VM before converting to template.


After Finale

Before booting change the disk size in the Xen Hypervisor (Xen Orchestra) (XCP-NG/XOA)

After booting into the new clone.

  1. Regenerate the SSH Fingerprints for uniqueness:
sudo jake-new-clone-fingerprints.sh
  1. increase disk size via:
sudo jake-grow-vm-disk.sh

  1. Then after changing the disk size. Change the hostname via:

sudo hostname mynewhostnamehere
sudo nvim /etc/hosts
sudo nvim /etc/hostname
sudo shutdown -r now

The End, now go enjoy the new server.

As a side note. I did try and got cloud-init to work. Initially the hard part was learning that the validator doesn't actually make sure the file is valid. You have to run a linter like yaml-lint to make sure the spacing, symbols, ect are all correct. The order in which you setup the config file also matters. At the end of the day, unfortunately there was still networking issues. If I didn't use cloud-init networking it would fail. However if I did use it, then out of the box I had issues with 2 networking services conflicting with each other. The amount of effort put into to get a basic setup working consistantly without issues while keeping it as close to a standard linux install was just too many issues without enough reward. I abandoned cloud-init because for me it was not worth using it just to setup some users, basic apt application installs, and a few config files.