{"id":502,"date":"2021-11-01T16:20:44","date_gmt":"2021-11-01T16:20:44","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=502"},"modified":"2022-04-11T20:22:59","modified_gmt":"2022-04-11T20:22:59","slug":"installing-ubuntu-server-20-04-part-1","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/linux\/installing-ubuntu-server-20-04-part-1\/","title":{"rendered":"Installing Ubuntu Server 20.04 – part 1"},"content":{"rendered":"\n
The plan for today:<\/p>\n
NOTE:<\/strong> I prefix commands to be run on the server with a I also prefer to use a public-private encryption key pair for each machine I connect to. In other words one key pair = one machine only.<\/p>\n Download the latest Ubuntu live server image from here: https:\/\/ubuntu.com\/download\/server<\/a>.<\/p>\n Choose "Option 2: Manual server installation" to get the live server image.<\/p>\n I will be installing the NOTE:<\/strong> Ubuntu has phased out the previous generations of installers and you can’t find it on the alternative images anymore. At first I was a bit worried because in the past I had issues with the live server image. However this time round I loved it!<\/p>\n Since I am on a Mac most of the time, I will be using balena Etcher<\/a> to write the image to a USB disk.<\/p>\n <\/p>\n Use an entire disk: ✅ (Make sure it is the correct disk!)\nSet up this disk as an LVM group: ✅\nEncrypt the LVM group with LUKS: ✅ (Specify the Passphrase you already generated and stored in your password manager)<\/p>\n On the Storage configuration screen the installer chose to only use 111GB out of 222GB to be mounted as \/ and thus leaving 50% free. To enlarge this partition select the ubuntu-lv item under Used Devices and select Edit. I chose to use the max size.<\/p>\n This will create the following layout (as seen by using The first priority is to configure the network and secure the remote logging that was enabled by SSH.<\/p>\n Login with the user created during installation.<\/p>\n Check the network is working:<\/p>\n<\/li>\n<\/ul>\n It goes without saying that is it super important to keep a log of all changes made to a server. I have learned the hard way on Linux why this is important. Therefore I want to track all changes made to config files either by myself or by installing programs. This can be done using See the official Ubuntu server documentation<\/a> for more information about See GitHub’s documentation<\/a> for more information.<\/p>\n Paste in the Public<\/strong> key (the filename with .pub as extension). On Mac you can use Allow write access ✅<\/p>\n Add the key.<\/p>\n$<\/code> to indicate the prompt. Commands to be run from your other computer (like a laptop) will be indicated using
local$<\/code>.<\/p>\n
Prerequisites<\/h2>\n
\n
# Here is an example of doing a full disk copy to a file\n$ dd if=\/dev\/sda of=\/media\/some-other-disk\/sda_backups\/sda_20211001.img\n<\/code><\/pre>\n
\n
Creating a bootable USB drive with the Ubuntu installer on<\/h2>\n
ubuntu-20.04.3-live-server-amd64.iso<\/code>.<\/p>\n
Installing Ubuntu Server 20.04 LTS<\/h2>\n
\n
lsblk<\/code> after the installation):<\/p>\n
sdb 8:16 0 223.6G 0 disk\n\u251c\u2500sdb1 8:17 0 1M 0 part\n\u251c\u2500sdb2 8:18 0 1G 0 part \/boot\n\u2514\u2500sdb3 8:19 0 222.6G 0 part\n \u2514\u2500dm_crypt-0 253:0 0 222.6G 0 crypt\n \u2514\u2500ubuntu--vg-ubuntu--lv 253:1 0 222.6G 0 lvm \/\n<\/code><\/pre>\n
\n
Initial hardening<\/strong><\/h2>\n
\n
$ ping google.com\n\n# Get the IP that was assigned by the DHCP server\n$ ip address show\nor\n$ ip a\n<\/code><\/pre>\n
\n
local$ ssh user@192.168.x.x\n# Ok boet now you can do the rest all from the sofa instead of hunched over in the "server room"\n<\/code><\/pre>\n
\n
$ sudo apt -y update && sudo apt -y upgrade\n$ sudo apt autoremove\n<\/code><\/pre>\n
\n
netplan<\/code> for sometime now and you configure via a yaml file.<\/li>\n<\/ul>\n
$ ls \/etc\/netplan\n00-installer-config.yaml\n\n$ sudo cp \/etc\/netplan\/00-installer-config.yaml \/etc\/netplan\/00-installer-config.bak\n$ sudo vi \/etc\/netplan\/00-installer-config.yaml\n# Here is what my config looks like. The internet router is .y.y\n\n# This is the network config written by 'subiquity'\nnetwork:\n ethernets:\n enp2s0:\n dhcp4: false\n addresses: [192.168.x.x\/24]\n gateway4: 192.168.y.y\n nameservers:\n addresses: [192.168.y.y,8.8.8.8]\n enp5s0:\n dhcp4: true\n version: 2\n<\/code><\/pre>\n
\n
sudo reboot now<\/code>.<\/li>\n
$ sudo cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.bak\n$ sudo vi \/etc\/ssh\/sshd_config\n\nPort 8204\nPermitRootLogin no\n\n<\/code><\/pre>\n
\n
local$ ssh-keygen -t rsa -b 4096 -f ~\/.ssh\/id_rsa_descriptive_name -C "Server name - user etc."\n\n# Add to macOS keychain\nlocal$ ssh-add -K ~\/.ssh\/id_rsa_descriptive_name\n<\/code><\/pre>\n
\n
.pub<\/code> extension over!<\/li>\n<\/ul>\n
local$ scp -P 8204 ~\/.ssh\/id_rsa_descriptive_name.pub user@192.168.x.x:\/home\/user\n<\/code><\/pre>\n
\n
local$ ssh -p 8204 -o PreferredAuthentications=publickey -i ~\/.ssh\/id_rsa_descriptive_name user@192.168.x.x\n$ # In like flynn\n<\/code><\/pre>\n
\n
$ sudo vi \/etc\/ssh\/sshd_config\n\nPasswordAuthentication no\n\n$ sudo systemctl restart ssh\n$ sudo systemctl status ssh\n<\/code><\/pre>\n
\n
# Edit your laptop's \/etc\/hosts file\n# Add an entry like this\n192.168.x.x servername\n\n# Verify this works\nlocal$ ping servername\n<\/code><\/pre>\n
\n
local$ vi ~\/.ssh\/config\n\n# Servername\nHost servername\n\tHostName servername\n\tPreferredAuthentications publickey\n\tIdentityFile ~\/.ssh\/id_rsa_descriptive_name\n\tIdentitiesOnly yes\n\tPort 8204\n\n# Now you should be able to connect using only this\nlocal$ ssh -p 8204 user@servername\n<\/code><\/pre>\n
Track all changes made using etckeeper & git<\/h2>\n
etckeeper<\/code>. I will also be using
git<\/code> as the version control software and configure a remote private git repo to be used as origin.<\/p>\n
etckeeper<\/code>.<\/p>\n
\n
$ sudo apt install etckeeper\n<\/code><\/pre>\n
\n
$ sudo etckeeper vcs log\n\ncommit 686b2f2a3297f9279952a741fe5ea5cfb96c636a\nAuthor: user <user@servername>\nDate: Sun Oct 31 14:00:24 2021 +0000\n\n Initial commit\n<\/code><\/pre>\n
Setting up a private GitHub repo<\/h3>\n
\n
git remote add origin git@github.com:username\/name-of-repo.git\n<\/code><\/pre>\n
\n
\n
-t rsa -b 4096<\/code> for a 4K RSA key).\nI have setup email privacy on GitHub and thus I will use the "@users.noreply.github.com" one as the comment.<\/li>\n<\/ul>\n
# The key will be owned by root\n$ sudo su\nroot$ ssh-keygen -t ed25519 -C "your_github_email@example.com"\n<\/code><\/pre>\n
\n
cat id_ed25519_github.pub | pbcopy<\/code> to copy the file to the clipboard.<\/p>\n
\n
root$ ssh -i \/root\/.ssh\/id_ed25519_github -T git@github.com\n\n# You should see this message\nHi ____! You've successfully authenticated, but GitHub does not provide shell access.\n\n# If not, use ssh -vT to get more information\n<\/code><\/pre>\n
\n
github-etckeeper<\/code> to specify the key and other SSH settings to be used.<\/li>\n<\/ul>\n
root$ vi \/root\/.ssh\/config\n\n# GitHub etckeeper\nHost github-etckeeper\n\tHostName github.com\n\tPreferredAuthentications publickey\n\tIdentityFile \/root\/.ssh\/id_ed25519_github\n\tIdentitiesOnly yes\n\tVisualHostKey=yes\n\tPort 22\n<\/code><\/pre>\n
\n
root$ ssh -T git@github-etckeeper\n<\/code><\/pre>\n
\n
root$ cd \/etc\n\n# Recall previously you saved a command given by GitHub that looked liked this:\n# git remote add origin git@github.com:username\/name-of-repo.git\n# This needs to be changed so that github.com becomes github-etckeeper (from the ssh config)\nroot$ git remote add origin git@github-etckeeper:username\/name-of-repo.git\n\n# Rename master to be main (so you are PC)\nroot$ git branch -M main\nroot$ git status\nOn branch main\nnothing to commit, working tree clean\n\n# Push all commits to the github repo\nroot$ git push -u origin main\n<\/code><\/pre>\n
\n
Configure etckeeper to push to the remote git repo<\/h3>\n
\n
etckeeper<\/code> to automatically push to origin.<\/li>\n<\/ul>\n
root$ vi \/etc\/etckeeper\/etckeeper.conf\n\nPUSH_REMOTE="origin"\n<\/code><\/pre>\n
\n
# Create a test file\nroot$ echo "testing 123" > \/etc\/test.txt\n\n# Commit all changes from \/etc\/ to the git repo\nroot$ etckeeper commit "Test 1"\n\n# Check that the commit was also pushed to GitHub\n# You should not have been asked for a passphrase to use the SSH key file\n\n# Make another change and check the cron job should work\nroot$ echo "456" >> \/etc\/test.txt\nroot$ \/etc\/etckeeper\/daily\n# This should have also pushed the change to GitHub\n\n# Finally\nroot$ rm \/etc\/test.txt\n# Now wait until the cron job runs the next day and verify.\n# I am also rebooting the server to verify everything works as I expect, and not find out 3 months from now when we had a power outage something doesn't work anymore\n<\/code><\/pre>\n