This article provides a quick reference guide for setting up an Ubuntu server. I found myself setting up multiple servers in the past, and always needed to look up the necesseary steps. This hopes to provide a nice and easy step-by-step reference.
This guide was directly compiled from Syntax FM’s brilliant YouTube video, which you can view here.
Steps for setting up your own Ubuntu server:
-
SSH into server
-
Update the package lists:
- Run
apt update,apt upgrade - Restart the server if necessary/prompted
- You can also run
ls /var/run/reboot-required, if the file exists, a reboot required - Run the
rebootcommand or reboot from the cloud provider dashboard
- Run
-
Change the root user password
- Run the
passwdcommand
- Run the
-
Add a non-root user
- Run the
addusercommand - Add the user to the sudo group:
usermod -aG sudo [username] - Run the
groupscommand to check which group it’s in
- Run the
-
Create ssh key login
- Generate an ssh key on your local machine:
ssh-keygen -t ed25519 -C "test@test.com"- Follow the directions in this link: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- Create an
.sshdirectory in thehome/[user]folder on the server - Create an
authorized_keysfile in the.sshdir - Copy the public key from the generated SSH key on your local machine from local machine into the
authorized_keysfile
- Generate an ssh key on your local machine:
-
Disable password login (optional)
- Edit the
/etc/ssh/sshd_configfile (might need to usesudo nano ...) - Set the
PasswordAuthenticationkey value toNo - Might also have to edit the same file in the
/etc/ssh/sshd_config.ddirectory and do the same thing - Restart the ssh service:
sudo service ssh restart
- Edit the
-
Disable root login
- Edit the
/etc/ssh/sshd_configfile (sudo nano ...) - Set the
PermitRootLoginkey value toNo - Restart the ssh service:
sudo service ssh restart
- Edit the
-
Set up network and firewall policies
-
Close unused ports (ufw)
- might need to enable firewall (
sudo ufw enable) - allow ssh traffic through the firewall:
sudo ufw allow ssh(do this before enabling the firewall) - useful commands:
- list all open ports:
sudo ss -tuln - list firewall status:
sudo ufw status verbose - list firewall rules for apps:
sudo ufw app list
- list all open ports:
- can change default ssh port if needs be
- might need to enable firewall (
-
can restrict ip addresses for allowed connections if needs be as well
-
-
Enable and configure automatic updates:
- Install the package for unattended upgrades:
sudo apt install unattended-upgrades - Enable automatic updates:
sudo dpkg-reconfigure unattended-upgrades - Hit
yeson the popup dialog - Can modify unattended upgrade settings - follow the guide here: https://github.com/mvo5/unattended-upgrades?tab=readme-ov-file#supported-options-reference
- Security updates are only enabled by default, can tweak settings for reboot time etc.
- Check service status:
sudo systemctl status unattended-upgrades
- Install the package for unattended upgrades:
Congratulations! You should have your Ubuntu server in a running state.