Initial Server Setup with Ubuntu 24.04 and 22.04

Initial Server Setup with Ubuntu 24.04 and 22.04

Secure a new Ubuntu 24.04 or 22.04 server: updates, a sudo user, SSH key login, no root login, UFW firewall and automatic security updates.

Initial server setup on Ubuntu means doing a handful of security and housekeeping steps before you install anything else: update the system, create a regular user with sudo rights, log in with an SSH key instead of a password, turn off root login over SSH, and enable the UFW firewall. On a new Cloud VPS Hosts server this takes about fifteen minutes.

This guide covers Ubuntu 24.04 LTS and Ubuntu 22.04 LTS. Run the commands in order on a fresh server. The examples use 203.0.113.10 as the server IP and deploy as the new username, so replace both with your own.

Why should you not work as root?

The root account can change anything on the server without asking, so a single typo or a stolen root password can take the whole machine down. A regular user with sudo rights has to confirm admin actions, every admin command is logged, and attackers who scan the internet for root logins find nothing to guess.

Step 1: Log in and update the system

Use the IP address and root password (or key) from your welcome email:

ssh root@203.0.113.10

Bring every package up to date before you change anything else:

apt update && apt upgrade -y

If the upgrade installed a new kernel, reboot with reboot and log in again.

Step 2: Create a user with sudo rights

adduser deploy
usermod -aG sudo deploy

adduser asks for a password and optional details. Use a strong password, because sudo asks for it. The second command adds the user to the sudo group so it can run admin commands.

Step 3: Set up SSH key login for the new user

SSH keys are far harder to crack than passwords. If you do not have a key yet, create one on your own computer first with our guide to setting up an SSH key. Then, from your computer, copy the public key to the server:

ssh-copy-id deploy@203.0.113.10

If root on the server already uses your key, you can copy it to the new user from the server instead:

rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

Open a new terminal window and test the login before you go further. Keep your root session open until this works:

ssh deploy@203.0.113.10
sudo whoami

sudo whoami should print root after you enter the user’s password.

Step 4: Turn off root login and password logins

Once key login works for your new user, close the two doors attackers try most often. On Ubuntu, put your settings in a separate file so package updates never overwrite them:

sudo nano /etc/ssh/sshd_config.d/99-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no

Check the configuration and restart SSH:

sudo sshd -t
sudo systemctl restart ssh

On Ubuntu 22.04 the service may be called ssh or sshd. Both names work with systemctl. Test a new login as deploy in a second window before you close your current session.

Step 5: Enable the UFW firewall

UFW is installed on Ubuntu by default but switched off. Allow SSH first, then enable it, otherwise you lock yourself out:

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

The status should show OpenSSH ALLOW Anywhere. Open more ports only when you install a service that needs them, for example sudo ufw allow 'Nginx Full' for a web server.

Step 6: Set the time zone and automatic security updates

Correct timestamps make logs readable. List zones with timedatectl list-timezones and set yours:

sudo timedatectl set-timezone Europe/Berlin

Ubuntu can install security updates on its own. Make sure it is turned on:

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

Choose Yes when asked whether to download and install stable updates automatically.

Step 7: Optional extra protection with Fail2ban

Fail2ban watches your logs and blocks IP addresses that fail to log in too often. With password logins already off it is a second layer rather than a must, but it keeps log noise down:

sudo apt install fail2ban -y
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Checklist: what your server looks like now

Item State after this guide How to check
Packages Up to date, security updates automatic apt list --upgradable
Admin user deploy with sudo rights groups deploy
SSH login Keys only, root login off sudo sshd -T | grep -E "permitrootlogin|passwordauthentication"
Firewall UFW on, only SSH open sudo ufw status
Brute force protection Fail2ban active (optional) sudo fail2ban-client status

What should you install next?

Your server is ready for real work. Common next steps on a Cloud VPS Hosts server:

Need a fresh Ubuntu server to start with? Our Linux VPS plans include full root access and Ubuntu 22.04 and 24.04 images.

Frequently asked questions

I locked myself out after changing SSH settings. What now?

Open a support ticket from the client area and ask for console access, or use the VNC console in your server panel if your plan includes one. From the console you can log in locally and undo the change in /etc/ssh/sshd_config.d/99-hardening.conf.

Should I change the SSH port from 22?

It cuts down automated login attempts in your logs, but it is not real protection on its own. Key-only login and Fail2ban matter much more. If you do change it, allow the new port in UFW before you restart SSH.

Do these steps work on Debian?

Almost all of them. On Debian 12, install sudo and UFW first with apt install sudo ufw, because they are not always present on minimal images.

How often should I update the server?

Unattended upgrades handle security patches daily. Run sudo apt update && sudo apt upgrade yourself about once a month to pick up other updates, and reboot when a new kernel is installed.

Post Your Comment

Knowledgebase
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.