10 Things You MUST Do Before Your Linux Server Goes Live

When you spin up a fresh Linux server, it comes with dangerous defaults: root login enabled, open ports, permissive access policies, no firewall, no continuous monitoring.

The internet is a dangerous place. Right now, bots are already scanning it, looking for servers exactly like yours to break into. That’s why hardening your server before it goes live isn’t optional — it’s the difference between staying invisible and becoming a target.

This guide walks you through the 10 things you need to do for Linux server hardening, in order, before you touch anything else.

Important note: This guide uses commands for Debian-based distributions (Debian, Ubuntu, and derivatives), since they’re the most common choice for cloud/VPS servers. If you’re running a RHEL-based distro (CentOS, Fedora, Rocky, AlmaLinux) or Arch, the core concepts still apply, but package manager commands (apt vs dnf/yum/pacman) and some group/user conventions will differ.

Update everything first

An unpatched Linux system is the number one attack vector for server compromise. That’s why keeping your system updated is essential — we’ll cover how to automate this later in the guide.

Run the command below immediately after you first gain access to a freshly provisioned server via SSH, before making any other changes.

sudo apt update && sudo apt upgrade -y

apt update refreshes the local index of available packages and their versions. Once that’s done, apt upgrade installs the newer versions of any packages that have updates available, based on that refreshed index.

Note: the -y flag auto-confirms all upgrades, which is fine here since the server isn’t live yet — avoid using it unattended on a server already in production.

Create a non-root user

You should never work as root on a daily basis. Instead, create a unique user for each person who needs access, and add them to the sudo group so they’re granted root-equivalent privileges.

Run these commands to do so:

adduser yourusername
usermod -aG sudo yourusername

After running adduser yourusername, you’ll be prompted to set a password and optionally fill in some user details (name, phone, etc.) — you can safely press Enter to skip the optional fields, but make sure to set a strong password when prompted.

Being in the sudo group means you can do anything the root user can do — but unlike root, you must authenticate with your own password before each privileged command runs. This keeps every action attributable to a real person instead of an anonymous “root.”

Revoking access is just as simple — remove the user from the sudo group:

sudo deluser yourusername sudo

The next three sections cover core SSH security measures: disabling root login, switching to key-based authentication, and changing the default port.

Disable root login over SSH

Now, switch into your new user:

su - yourusername

Root’s shell is now closed — from this point on, you’ll operate as your new sudo-enabled user instead.

Root is the most-targeted username in brute-force attacks, which is why we should disable root login over SSH entirely. Run this command:

sudo sed -i 's/^#*PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config

This automatically updates the PermitRootLogin property to no in /etc/ssh/sshd_config.

Confirm the change took effect:

grep "^PermitRootLogin" /etc/ssh/sshd_config

Restart SSH to apply the change:

sudo systemctl restart sshd

Critical safety note: restarting SSH won’t disconnect your current session. But if your config change contains an error — a typo, or a lockout you didn’t intend — new connections could fail once the daemon reloads. Keep your current session open and test a new connection in a second terminal before closing the original one. If the new connection fails, you’ll still have a working session to fix the mistake from.

Switch to SSH key authentication, disable password login

As you may already know, passwords can be brute-forced since SSH allows password login by default. You cannot brute-force SSH keys — you’re either authorized, or you’re not.

By default, SSH comes with PubkeyAuthentication set to yes, either implicitly (omitted from the config) or explicitly (set in sshd_config). That means key-based login is already possible — you just need to generate a key on client side and give the server your public half.

Step 1: Generate an SSH key pair (on your host machine, not the server)

ssh-keygen -t ed25519 -C "your_email@example.com"

This creates two files: a private key (~/.ssh/id_ed25519) that stays on your machine and must never be shared, and a public key (~/.ssh/id_ed25519.pub) that’s safe to share and needs to go on your server.

Step 2: Copy your public key to the server

ssh-copy-id yourusername@<server-ip>

This appends your public key to ~/.ssh/authorized_keys on the server, allowing that key to authenticate without a password.

Step 3: Confirm key-based login works before continuing

ssh yourusername@<server-ip>

If you’re logged in without being asked for a password, your key is working — it’s now safe to disable password authentication.

Step 4: Disable password authentication

This forces SSH to rely on key-based authentication only:

sudo sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config

Always confirm the change took effect:

grep "^PasswordAuthentication" /etc/ssh/sshd_config

Restart SSH to apply the change:

sudo systemctl restart sshd

Critical safety note: keep your current session open, and test a new SSH connection in a second terminal before closing the original one. Unlike the root-login step, there’s no fallback here if something goes wrong — if key auth isn’t working correctly, you’ll be locked out completely, with no password login to fall back on.

Change the default SSH port

This recommendation is optional but useful, it won’t stop a targeted attacker, but it will silence 90% of automated bot noise in your logs.

Run this command to change the port from default 22 to 2222:

sudo sed -i 's/^#*Port.*/Port 2222/' /etc/ssh/sshd_config

Confirm the change took effect:

grep "^Port" /etc/ssh/sshd_config

If the command above returns nothing, your config had no explicit Port line to replace — add one manually instead:

echo "Port 2222" | sudo tee -a /etc/ssh/sshd_config

Critical: before restarting SSH, if you’ve set up a firewall, allow the new port now — otherwise you could lock yourself out the moment SSH switches to it.

sudo ufw allow 2222/tcp

Now restart SSH to apply the change:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Because SSH is socket-activated, systemd itself listens on the port and hands off connections to the SSH daemon only when a connection comes in — the daemon isn’t actually bound to the port directly. That means:

Critical safety note: keep your current session open, and test a new SSH connection in a second terminal before closing the original one.

From now on, connect using the new port:

ssh -p 2222 yourusername@<server-ip>

Set up a firewall with UFW

For convenience, your server probably comes with the firewall disabled by default. This is dangerous: it means all inbound ports are open, so any service running on your machine is exposed to the internet and can receive data from anyone — including malicious traffic.

The recommended approach is to only expose the ports you actually need, so only the services you intend to be public are reachable, while everything else stays hidden from outside connections.

For that, you can use UFW (Uncomplicated Firewall), a simplified frontend for managing iptables/nftables rules on Linux. Here’s how it works:

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

This exposes only SSH and HTTP/HTTPS, then enables the firewall and confirms the rules are active. If you’ve changed your SSH port from the default 22, replace it with your actual port — otherwise you’ll lock yourself out.

For more complex rules, iptables or nftables can be used directly — we compare them with UFW in UFW vs iptables vs nftables: Which Linux Firewall Should You Use?

Install Fail2ban

One of the most important tools to secure your server from brute-force attackers is Fail2ban — it monitors your logs and automatically bans IPs that show repeated failed login attempts, blocking them at the firewall level for a configurable duration.

sudo apt install fail2ban

Confirm it’s running:

sudo systemctl status fail2ban

We configure this in depth in Stop Brute-Force Attacks: Fail2ban Setup Guide for Linux.

Set up automatic security updates

Automating package updates is key to closing the gap that causes most breaches. This automation updates packages to their latest versions — by default, every day — and can require a service restart or even a reboot. That’s why you should be aware of the risks involved and configure automatic updates carefully.

On Debian-based distributions, the package responsible for automatic updates is unattended-upgrades, which is installed by default on some distributions like Ubuntu.

If for some reason the package isn’t present, install it with:

sudo apt install unattended-upgrades

You can enable or disable automatic upgrades with:

sudo dpkg-reconfigure unattended-upgrades

This opens an interactive prompt — select “Yes” to enable automatic upgrades.

The configuration itself lives in /etc/apt/apt.conf.d/50unattended-upgrades and /etc/apt/apt.conf.d/20auto-upgrades — we’ll cover how to configure unattended-upgrades in depth in a dedicated post.

Disable unused services

Unused services represent a security risk, since each one is a potential attack surface. This risk should be mitigated by disabling any service you don’t actually use.

List all services and check which are enabled to start on boot:

systemctl list-unit-files --type=service --state=enabled

Then check what’s actively running right now:

systemctl list-units --type=service --state=running

Critical safety note: Before disabling anything, make sure you know what each service does — some are required by your OS, hosting provider, or other services you rely on. When in doubt, look it up before disabling it.

To disable a service:

sudo systemctl disable --now <service_name>

If you want to make sure a service can never be started again — even accidentally, by another package or dependency — use mask instead:

sudo systemctl mask <service_name>

disable prevents normal startup, but a service can still be started manually or pulled in as a dependency by another package. mask closes that loophole completely by linking the service to /dev/null, so it can’t be started by any means until unmasked.

Set up basic monitoring/logging

Don’t forget to configure basic monitoring and logging on your machine, so you can catch a potential attack early and respond before it breaches your system. A few common tools exist for this: journalctl (built-in, per-service log inspection), logwatch (daily email digest of log activity), and Netdata (a lightweight real-time dashboard for system metrics).

As a starting point, here’s how to follow SSH logs live using journalctl:

journalctl -u ssh.service -f

Watch for repeated Failed password entries from the same IP — a sign of a brute-force attempt, which tools like Fail2ban (covered earlier) can block automatically once detected.

We’ll cover logwatch and Netdata setup in a dedicated post.

These 10 steps should take about half an hour and eliminate the majority of opportunistic attacks — giving you a secure Linux server from day one. However, remember that Linux hardening is an ongoing process — regular checks, updates, and monitoring keep your defenses effective over time. Stay vigilant and keep learning to protect your servers.

Next, go deep on Fail2ban: Stop Brute-Force Attacks: Fail2ban Setup Guide for Linux.


Discover more from vigilnix

Subscribe to get the latest posts sent to your email.

Ismaël Qouiqa Avatar

Written by

2 responses to “10 Things You MUST Do Before Your Linux Server Goes Live”

  1. […] — the port Fail2ban should watch for SSH traffic. If you followed the previous guide and moved SSH off the default port, set it here — otherwise Fail2ban will be watching the wrong […]

  2. […] This guide assumes SSH keys only, automatic security updates, and a non-root sudo user. If that’s not done, start with 10 Things You MUST Do Before Your Linux Server Goes Live. […]

Leave a Reply