Secure a New Dedicated Server First Steps: Ubuntu 24.04

Generic server rack with payment servers, backup storage and network switch under green accent lighting
🔊 Listen to this article UK Voice ~10 min
0:00 / --:--

 

TL;DR

  • The first hour on a fresh Ubuntu Server 24.04 LTS dedicated server, in five ordered steps.
  • Admin user with an SSH (Secure Shell) key, password and root login off, ufw (Uncomplicated Firewall), fail2ban, then automatic updates.
  • Every step ends with a check and a rollback.
  • Needs: the provider’s first login and a console that does not depend on SSH.

Anyone who has to secure a new dedicated server first steps through its logins, its firewall and its update settings. This run sheet puts those jobs in the order that is safe to run them, each with a check and a way back. It is for small business IT teams, agencies, developers and system administrators who run the server themselves.

The commands target Ubuntu Server 24.04 LTS (long-term support) and come from the official documentation linked in each step. The wider owner jobs, from backups to restore drills, sit in our owner’s checklist for managing your own dedicated server.

BEFORE YOU START

  • Access: the provider’s first login and a KVM (keyboard, video and mouse) console that does not depend on SSH. Does the shipped image allow password or root login over SSH?
  • Release: a fresh Ubuntu Server 24.04 LTS install with no applications yet. Which versions does apt policy openssh-server ufw fail2ban report?
  • Free disk: is there room on the root file system for fail2ban and update downloads?
  • Maintenance window: is an hour booked when nobody else depends on this server?
  • Backup: which snapshot or backup path does the provider’s panel offer, and has it been taken?
  • Rollback: each step names its undo. One SSH session kept open all hour gives a way back.
  • SAFETY NOTE: these are general steps, written against Ubuntu Server 24.04 LTS with OpenSSH, ufw, fail2ban and unattended-upgrades from the Ubuntu archive. Every server differs in roles, firewall, applications and network, so a technical person who knows this server should review each step before it is run, and try it on a test or non-production server first where one exists.
CRITICAL

  • Step 2 can lock you out: run it only after a key login as the new admin user works in a second session.
  • Step 3 can lock you out: run sudo ufw enable only after the SSH rule is in place.
  • Step 4 can ban your own address: confirm the out-of-band console works first.

Why the Order of the Five Steps Is Fixed

Three of the five steps can cut off your own access, so the order is fixed. The key login comes before password login goes off, and the SSH rule comes before the firewall goes on.

For every SSH or firewall change, session one stays logged in while a new session two tests it. If session two works, the change stays. If it fails, session one rolls it back.

Step 1: Create an Admin User and Install Your SSH Key

The Ubuntu user management guide says that adding an account to the sudo group gives that account full root access through sudo. After a login with the provider’s credentials, two commands create the user and add it to that group:

sudo adduser opsadmin
sudo adduser opsadmin sudo

Next, a key pair made on your own computer goes across to the server. The Ubuntu OpenSSH guide recommends the ed25519 algorithm for its shorter key size and lower computational requirements.

ssh-keygen -t ed25519
ssh-copy-id [email protected]

Check: in a second terminal, ssh [email protected] should log in without the account password. A passphrase prompt is expected if the key has one.

Wrong result: a password prompt means the server did not accept the key. The log in sudo journalctl -fu ssh.service shows the reason. As the new user, chmod go-w .ssh/authorized_keys tightens the file’s permissions.

Rollback: sudo deluser opsadmin removes the account; the guide notes the home folder stays until you delete it.

Step 2: Turn Off Password and Root Login Over SSH

The OpenSSH manual lists yes as the default for PasswordAuthentication, so password logins stay open until a setting changes them. A drop-in file, /etc/ssh/sshd_config.d/01-first-hour.conf, holds three lines:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

The 01 prefix is deliberate. The manual says included files are processed in lexical order, and for most keywords the first value obtained is used.

As in the Ubuntu guide, a syntax test comes before the restart:

sudo sshd -t
sudo systemctl restart ssh.service

Check: with session one still open, a key login as opsadmin from a new terminal should work. A login as [email protected] should then be refused.

Wrong result: if sudo sshd -t prints an error, the named line needs fixing before the restart.

Rollback: from session one, sudo rm /etc/ssh/sshd_config.d/01-first-hour.conf and a restart of ssh.service undo the change.

Step 3: Allow SSH in ufw, Then Turn It On

The Ubuntu firewall guide notes that ufw is initially disabled. The SSH rule goes in first, and the firewall goes on after it:

sudo ufw allow 22
sudo ufw enable

Connecting from one fixed office or VPN (virtual private network) address? The guide’s narrower rule, sudo ufw allow proto tcp from 192.168.0.2 to any port 22, takes your address in place of the sample. Once it tests fine, sudo ufw delete allow 22 removes the open rule.

Check: sudo ufw status verbose should show the firewall active with the port 22 rule, and a new SSH session should still connect.

Rollback: sudo ufw disable turns the firewall off again.

Step 4: Add fail2ban for the sshd Jail

The install command is sudo apt install fail2ban. The Ubuntu 24.04 jail.conf manual recommends leaving .conf files unchanged and putting customisations in .local files. Following its example, /etc/fail2ban/jail.local enables the sshd jail with two lines, and a restart loads it:

[sshd]
enabled = true
sudo systemctl restart fail2ban

Check: sudo fail2ban-client status sshd should return the jail’s status, as the Ubuntu 24.04 fail2ban-client manual page describes. An error means the jail did not start.

Wrong result: if your own address is banned, the console still gives a way in. From there, sudo fail2ban-client set sshd unbanip followed by the address lifts the ban.

Rollback: sudo fail2ban-client stop sshd stops the jail. Does the sshd jail start again after a restart, and which file enables it?

Step 5: Confirm Automatic Security Updates

This step installs nothing; it confirms updates are on. The Ubuntu automatic updates guide says unattended-upgrades is installed by default and runs once per day. The file /etc/apt/apt.conf.d/20auto-upgrades should hold:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Then sudo unattended-upgrade -v –dry-run shows what it would install without changing anything. Logs of each run sit in /var/log/unattended-upgrades.

Reboots are separate. Automatic-Reboot defaults to false, so an update that needs a reboot waits for you, and /var/run/reboot-required flags it.

Rollback: setting both 20auto-upgrades values to “0” disables unattended upgrades.


What to Check After the First Hour

Check Command Expected result
Admin key login ssh [email protected] Key login, no account password
Root login ssh [email protected] Refused
Firewall sudo ufw status verbose Active, port 22 rule listed
fail2ban sudo fail2ban-client status sshd sshd jail status returned
Updates sudo unattended-upgrade -v –dry-run Allowed origins listed, no errors
Pending reboot ls /var/run/reboot-required Absent, or a reboot booked

Sources: Ubuntu Server documentation and Ubuntu 24.04 manual pages.

Any row that does not match points back to its step and its rollback. A dated record of these results is the starting point for a full baseline, which our CIS benchmark Linux server hardening audit walks through on one server.

When to Stop and Hand Over

  • Is there no console path apart from SSH? Stop before Step 2 and ask the provider for one.
  • Does cPanel, Plesk or another panel manage SSH and the firewall itself?
  • Is the server on AlmaLinux or Debian? That distribution’s documentation gives the equivalent commands.
  • Will anyone read the update logs and fail2ban status after today? If not, our comparison of managed and unmanaged dedicated servers covers handing over.

FAQs

Should I change the SSH port on a new dedicated server?

None of these steps needs it, and Step 3 allows port 22 only. If SSH moves to another port, has the ufw rule for that port been added and the sshd jail port updated before ssh.service is restarted?

How do I get back in after locking myself out of SSH?

The out-of-band console is the way back in. From there, the rollback of the step that locked you out undoes it. The step can then run again with two sessions open.

Where the Server Stands Now

After this hour, the server takes key logins from a named admin only, allows SSH through ufw, bans repeat failures and checks for security updates daily.

NEXT STEP
Sizing the next machine you will harden this way? See the configurations on our dedicated and bare metal servers page.

 

Leave a Reply

Your email address will not be published. Required fields are marked *