Skip to content
Tarisu Limited

Secure a New Linux VPS: SSH Keys, Firewall, and Safe Updates

A practical first-hours checklist for a new Linux VPS: create a personal account, install SSH keys, lock down SSH, enable a firewall, and turn on automatic updates.

Guides 7 min read Tarisu Limited

Secure a New Linux VPS: SSH Keys, Firewall, and Safe Updates

Who this guide is for

This guide is for anyone who has just received a Linux VPS and wants to make it safe before installing a website, mail service, game server, or application. It assumes you can log in as root with the initial password or key from the provider. The goal is to remove common early risks: password-only SSH access, unrestricted root login, missing firewall rules, and unpatched packages.

The commands are written for Ubuntu 22.04, Ubuntu 24.04, Debian 12, AlmaLinux 9, and Rocky Linux 9. Keep your provider control panel or console open while you work. If you use a Tarisu VPS, the control panel can help confirm the public IP, hostname, and network status, but the steps below apply inside the operating system.

Prerequisites

  • A new Linux VPS with root access.
  • The server public IP address.
  • A local computer with SSH. Windows 10 and 11 can use the built-in OpenSSH client in PowerShell or Windows Terminal.
  • Provider console, VNC, or rescue access for recovery.
  • A snapshot or backup if available. This is not a password recovery task, but a snapshot is sensible before SSH and firewall changes.

Step 1: Confirm the operating system and install base tools

Run the first commands as root. Confirm the distribution because Ubuntu and Debian use apt, while AlmaLinux and Rocky use dnf.

cat /etc/os-release; uname -a

You should see the distribution name and kernel version.

Ubuntu 22.04/24.04 and Debian 12

apt update; apt install -y sudo ufw curl

AlmaLinux 9 and Rocky Linux 9

dnf update -y; dnf install -y sudo firewalld curl

Step 2: Create a personal administrator account

Do not use root for daily work. Create a personal account and add it to the administrator group.

Ubuntu 22.04/24.04 and Debian 12

adduser alex; usermod -aG sudo alex; id alex

Replace alex with your username. The adduser command asks for a password. The id command should show the sudo group.

AlmaLinux 9 and Rocky Linux 9

useradd -m alex; passwd alex; usermod -aG wheel alex; id alex

The id command should show the wheel group. Open a second SSH session as the new user before closing the root session.

ssh alex@YOUR_SERVER_IP

Step 3: Install an SSH public key

Generate a key pair on your local computer if you do not already have one.

ssh-keygen -t ed25519 -C vps-key

Accept the default path unless you need another one. A passphrase is optional but useful.

Linux or macOS

ssh-copy-id alex@YOUR_SERVER_IP

Windows 10 or 11 PowerShell

Get-Content $HOME/.ssh/id_ed25519.pub | ssh alex@YOUR_SERVER_IP 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

Open a new terminal and log in again.

ssh alex@YOUR_SERVER_IP

You should reach the shell without the account password. Do not continue until key login works.

Step 4: Harden SSH

Back up the configuration.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Edit the file.

sudo nano /etc/ssh/sshd_config

Set or add these values. If a line exists, change it instead of adding a duplicate.

  • PubkeyAuthentication yes
  • PasswordAuthentication no
  • PermitRootLogin no
  • KbdInteractiveAuthentication no
  • ChallengeResponseAuthentication no, if present

Leave UsePAM as supplied unless you have a reason to change it. On Ubuntu and Debian, also check for drop-in overrides.

sudo grep -R PasswordAuthentication /etc/ssh/sshd_config.d/

If a drop-in file sets a conflicting value, edit that file too. Test the configuration.

sudo sshd -t

No output usually means the file is valid. Restart SSH using the service name for your distribution.

Ubuntu 22.04/24.04 and Debian 12

sudo systemctl restart ssh; sudo systemctl status ssh --no-pager

AlmaLinux 9 and Rocky Linux 9

sudo systemctl restart sshd; sudo systemctl status sshd --no-pager

Keep the current session open. In a new terminal, test the personal account.

ssh alex@YOUR_SERVER_IP

If login fails, use the provider console and restore the backup.

sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config; sudo systemctl restart ssh

On AlmaLinux or Rocky, use sshd in the restart command.

Step 5: Enable a host firewall

Allow SSH before enabling the firewall.

Ubuntu 22.04/24.04 and Debian 12 with ufw

sudo ufw allow 22/tcp; sudo ufw enable; sudo ufw status verbose

You should see the firewall active and port 22/tcp allowed.

AlmaLinux 9 and Rocky Linux 9 with firewalld

sudo systemctl enable --now firewalld; sudo firewall-cmd --permanent --add-service=ssh; sudo firewall-cmd --reload; sudo firewall-cmd --list-all

The ssh service should appear in the output.

Step 6: Enable automatic security updates

Ubuntu 22.04/24.04 and Debian 12

sudo apt update; sudo apt install -y unattended-upgrades; sudo dpkg-reconfigure -plow unattended-upgrades

Check the periodic settings.

grep -R Unattended-Upgrade /etc/apt/apt.conf.d/20auto-upgrades

You should see unattended upgrades enabled. If the file is missing, create it and enable periodic updates and upgrades.

AlmaLinux 9 and Rocky Linux 9

sudo dnf install -y dnf-automatic; sudo nano /etc/dnf/automatic.conf

Set the following value.

apply_updates = yes

Enable and check the timer.

sudo systemctl enable --now dnf-automatic.timer; systemctl status dnf-automatic.timer --no-pager

The timer should be active. Automatic updates do not replace monitoring and backups.

Step 7: Review listening services

sudo ss -tulpn

Look for services you do not recognize. If a service is not needed, stop it or bind it to the loopback address according to the application documentation.

What you should see at the end

  • You can log in as your personal account with an SSH key.
  • Password login and direct root SSH login are disabled.
  • The SSH configuration passes sshd -t.
  • The firewall is active and SSH is allowed.
  • Automatic security updates are enabled.
  • You can recover through the provider console if needed.

Troubleshooting common errors

Permission denied, publickey

Check the key and permissions. From your local machine, run:

ssh -vvv alex@YOUR_SERVER_IP

On the server, check the files.

ls -ld /home/alex/.ssh; ls -l /home/alex/.ssh/authorized_keys

The .ssh directory should be mode 700 and owned by the user. The authorized_keys file should be mode 600. Repair carefully.

sudo chown alex:alex /home/alex/.ssh /home/alex/.ssh/authorized_keys; sudo chmod 700 /home/alex/.ssh; sudo chmod 600 /home/alex/.ssh/authorized_keys

sshd -t reports an error

Correct the file and test again. If you cannot fix it, restore the backup through the provider console.

sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config

Firewall locked out after enabling ufw or firewalld

Use the provider console. For ufw:

sudo ufw allow 22/tcp; sudo ufw enable

For firewalld:

sudo firewall-cmd --permanent --add-service=ssh; sudo firewall-cmd --reload

sudo not found or user not in administrator group

Ubuntu and Debian use the sudo group.

sudo apt install -y sudo; sudo usermod -aG sudo alex

AlmaLinux and Rocky use the wheel group.

sudo dnf install -y sudo; sudo usermod -aG wheel alex

SSH service name differs

Ubuntu and Debian usually use ssh. AlmaLinux and Rocky usually use sshd. Check the list if a restart fails.

systemctl list-units --type=service --no-pager

Security notes

These steps are a baseline. Add backups, monitoring, and application hardening as needed. Do not share root. If a key is lost or exposed, remove it from authorized_keys and issue a new one. Provider firewalls and host firewalls should not contradict each other. A Tarisu SSL certificate can protect website traffic once the server is stable, but it does not replace SSH hardening or firewall rules.

Frequently asked questions

Should I change the default SSH port?

It can reduce noise, but it is not a substitute for key-only login and a firewall. If you change it, update the firewall and test from a new session.

Can I disable root login on day one?

Only after your personal account can log in with a key and use sudo. Keep the provider console available.

What if I use a provider firewall and ufw or firewalld?

Choose one main policy. Many users block nonessential ports at the provider level and allow SSH and specific application ports in the host firewall.

Are automatic updates enough?

They help, but you should also review logs, keep backups, remove unused services, and update applications.

Which SSH key type should I use?

Ed25519 is a good default. Use RSA with a strong key size only when an older system requires it.

How do I rotate an SSH key?

Add the new key, test login, then remove the old key. Do this while you still have a working session and provider console access.

Related posts

We use strictly necessary cookies to keep you signed in and remember your preferences. We would also like to measure how the site is used, but only with your agreement. Read our privacy policy