Set Up a VPS for the First Time Without a Lockout
This guide takes a fresh Ubuntu server to key-only logins, a running firewall and security fixes that install themselves. It follows the Ubuntu Server guides for OpenSSH, the firewall and automatic updates, and each setup step ends with a check you can run.
Turning on the firewall first can lock you out of your server: the ufw manual page warns that switching it on may drop SSH connections, and SSH stays open only if you allowed it first. A settings file you never wrote can quietly keep password login on. Both traps get extra space below.
The server commands are for current Ubuntu LTS releases. The laptop commands run on macOS and Linux. Debian and AlmaLinux use different tools for some steps, so do not copy these server commands there.
What do you need before you start?
You need the server's IP address, the login your provider gave you, a terminal on your laptop and a second way into the server.
- A terminal. On macOS or Linux, use Terminal. On Windows, check on Microsoft Learn whether your version includes the OpenSSH client.
- A way back in: your provider's browser console, in case SSH stops letting you in.
- A snapshot, taken before you change login settings, if your provider offers snapshots.
Keep one logged-in window open the whole time. Test every change from a second window.
How do you log in and update the server?
Log in as root over SSH, then run the two update commands.
On your laptop:
ssh root@YOUR_SERVER_IPOn the server:
sudo apt update
sudo apt upgradeUbuntu's automatic updates run these same two steps later. Check: you see the server's command prompt. Then run ls /var/run/reboot-required. If the file exists, run sudo reboot and log back in. If ls says the file does not exist, you don't need to reboot.
How do you stop working as root?
Create your own user, give it admin (sudo) rights and use it for everything after this step.
adduser yourname
usermod -aG sudo yournameCheck: in a second window, run ssh yourname@YOUR_SERVER_IP, then sudo whoami. It should print root.
How do you switch to key login?
Make a key pair on your laptop, then put the public half on the server for your new user.
On the laptop, run the command below and set a passphrase when asked. Ubuntu recommends ed25519, with RSA 4096 (ssh-keygen -t rsa -b 4096) as the alternative.
ssh-keygen -t ed25519
cat ~/.ssh/id_ed25519.pubThe second command prints your public key. Copy that line. The private key, ~/.ssh/id_ed25519, never leaves your laptop.
On the server, logged in as yourname, paste the key into the file and save it:
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
chmod go-w ~/.ssh/authorized_keysThe chmod line stops other users from writing to the file. If your laptop has ssh-copy-id, then ssh-copy-id yourname@YOUR_SERVER_IP does the same job.
Check: ssh yourname@YOUR_SERVER_IP logs you in without the account password. It may ask for the key passphrase. If it fails, watch the log in your open window with sudo journalctl -fu ssh.service and try again.
How do you turn off password login so it stays off?
Put your settings in a new file whose name sorts ahead of the usual files there, so files that load after it cannot override them.
sudo nano /etc/ssh/sshd_config.d/00-hardening.confAdd these three lines:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin noUbuntu reads this folder at the very top of the main SSH settings file. The sshd_config manual page says OpenSSH keeps the first value it finds and loads these files in name order. Both password settings default to yes. A file another tool left in that folder could turn password login back on, and the 00 name makes your file load before the usual numbered files. With password login off, nobody can log in over SSH by guessing a password. Now test the settings and restart SSH:
sudo sshd -t
sudo systemctl restart ssh.serviceCheck:
sudo sshd -T | grep -i -E 'passwordauthentication|kbdinteractiveauthentication|permitrootlogin'All three lines must end in no. Then open a new window and log in with your key. Close the old window only after that works.
How do you turn on the firewall without locking yourself out?
Allow SSH first, then switch the firewall on. Ubuntu's firewall tool, ufw, starts switched off.
sudo ufw --dry-run allow 22
sudo ufw allow 22
sudo ufw enableThe dry run previews the rule without applying it. Open 80 and 443 only once you run a website. The Ubuntu firewall guide linked above lists the full command set.
Check: sudo ufw status verbose shows Status: active and port 22 allowed. A new SSH login from your laptop still works.
How do you confirm automatic security updates work?
Ubuntu turns security updates on right after installation. To confirm, read one file, do a dry run and look at the timers:
cat /etc/apt/apt.conf.d/20auto-upgrades
sudo unattended-upgrade --dry-run
systemctl list-timers 'apt-daily*'
ls /var/log/unattended-upgrades/Check: the file holds APT::Periodic::Update-Package-Lists "1"; and APT::Periodic::Unattended-Upgrade "1";. The dry run ends without errors. Both apt-daily timers appear, because updates run once a day through them. The log folder fills up after each run.
Updates do not reboot the server by default, so kernel fixes wait for you. You have two choices. One is to set these lines in /etc/apt/apt.conf.d/50unattended-upgrades:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";When an update needs a restart, the server then restarts by itself at that time on the server's clock, without asking you. Pick an hour when nobody uses your site. The other choice is a weekly reminder on your phone's calendar to run ls /var/run/reboot-required.
Who hears about it when the server needs you?
Ubuntu leaves the update report address empty, so out of the box no report goes to anyone.
Setting an address alone does nothing. Email needs a mail program set up separately. Point it at your email provider's sending (SMTP) settings and send yourself a test. Then add:
Unattended-Upgrade::Mail "you@example.com";
Unattended-Upgrade::MailReport "only-on-error";on-change also works and is the default. Also sign up for an outside uptime monitor that emails or texts you when your site or server stops answering. Never run these checks from a laptop that sleeps.
How do you check everything in one go?
Save this script on the server as first-day-check.sh and run sudo sh first-day-check.sh after any change.
#!/bin/sh
# Run on the server: sudo sh first-day-check.sh
echo "== SSH login (all three should say no) =="
sshd -T | grep -i -E 'passwordauthentication|kbdinteractiveauthentication|permitrootlogin'
echo "== Firewall (want: active, 22 allowed) =="
ufw status verbose
echo "== Automatic updates (want: both \"1\") =="
cat /etc/apt/apt.conf.d/20auto-upgrades
echo "== Update timers =="
systemctl list-timers 'apt-daily*' --no-pager
echo "== Reboot needed? =="
if [ -f /var/run/reboot-required ]; then echo "Yes, reboot soon"; else echo "No"; fiWhat goes wrong most often?
- Password login still works. A file in
sshd_config.dsets it back to yes. Use the 00 file and confirm withsudo sshd -T. - You get locked out after
ufw enable. Log in through your provider's browser console, runsudo ufw allow 22, then checksudo ufw status. - Key login fails. Usually the key file has the wrong permissions or sits in the wrong user's folder. Run
chmod go-w ~/.ssh/authorized_keysas that user and watchsudo journalctl -fu ssh.servicewhile you retry.
How does this work on a PomeloHost VPS?
Every PomeloHost KVM VPS comes with full root access. If an SSH change goes wrong, the browser console works even when SSH does not, and you can take a snapshot before you touch login settings. A weekly backup is stored outside the data center. It does not replace your own backups. Images include Ubuntu, Debian and AlmaLinux, and the server is self-managed unless you add the managed option. Outbound port 25 is closed by default and may be opened on request after review. Ticket support runs 24/7 and is answered by people.
What should you remember?
Change one thing at a time. Keep one window open, prove each change from a second one, and run the check script when you're done. If a step goes wrong, that open window or the browser console gets you back in.
Frequently asked questions
Is unattended-upgrades on by default in Ubuntu?
Yes. On Ubuntu Server, unattended-upgrades is installed by default, and security updates are on right after installation. It runs once a day through two system timers. By default it does not reboot the server and sends no email report. You set up both yourself, as shown in the steps above.
Why does password login still work after I set PasswordAuthentication no?
Another file in /etc/ssh/sshd_config.d/ is probably setting it to yes and loading first. OpenSSH keeps the first value it finds. Put your settings in a file named 00-hardening.conf, test with sudo sshd -t, restart SSH, and confirm all three lines read no with sudo sshd -T.
Do these commands work on Debian or AlmaLinux?
Treat the server commands as Ubuntu-only. They follow the Ubuntu Server documentation. Debian and AlmaLinux use different tools for some steps, such as the firewall and automatic updates, so read their own documentation before you copy anything. The key steps on your laptop stay the same.
When should I take a snapshot?
Right before you change SSH login settings or turn on the firewall. If something goes wrong, you roll back instead of rebuilding. On a PomeloHost VPS, you can take snapshots, and a weekly backup is stored outside the data center. Keep your own backups too.