On this page
Key takeaways
- The order of the steps matters: do not turn off password login before you have logged in with your key as the new user.
- Working as root gives every typo full power.
- Keep the root session open. In a new window, log in as ops and confirm that sudo works.
- Allow SSH first, otherwise enabling the firewall cuts your own connection.
A new server is reachable from the whole internet within minutes of being created. This guide takes you from the first root login to a server with a normal user, key-only SSH, a firewall and automatic security updates.
Before you start
You need a Cloudvix server with Ubuntu 24.04 and its IPv4 address from the dashboard. Replace SERVER_IP with that address in every command. The order of the steps matters: do not turn off password login before you have logged in with your key as the new user.
- an Ubuntu 24.04 server and its IPv4 address
- an SSH key pair on your computer, with the public half added to your account when you ordered the server
- a second terminal window, to check each risky step before you close the first one
The steps
Log in as root
Use the address from the dashboard. The first time, SSH asks you to confirm the server’s fingerprint.
ssh root@SERVER_IPUpdate the system
Install pending updates first. If one of them needs a reboot, the last line restarts the server; log in again afterwards.
apt update
apt upgrade -y
test -f /var/run/reboot-required && rebootCreate a normal user with sudo
Working as root gives every typo full power. Create a user (this guide calls it ops) and add it to the sudo group. adduser asks for a password and some optional details; choose a long password and keep it in a password manager, sudo will ask for it.
adduser ops
usermod -aG sudo opsGive the new user your key
Copy the key that is already allowed for root, so the same private key on your computer works for ops. If root has no key, run ssh-copy-id ops@SERVER_IP on your computer instead; it uses the password you just set.
mkdir -p /home/ops/.ssh
cp /root/.ssh/authorized_keys /home/ops/.ssh/authorized_keys
chown -R ops:ops /home/ops/.ssh
chmod 700 /home/ops/.ssh
chmod 600 /home/ops/.ssh/authorized_keysCheck the new login in a second terminal
Keep the root session open. In a new window, log in as ops and confirm that sudo works. The last command must print root.
ssh ops@SERVER_IP
sudo whoamiTurn off password and root login
Put the settings in a file of their own. Files in /etc/ssh/sshd_config.d/ are read in name order and the first value of a setting wins, so a name starting with 00 beats a file that a cloud image may already contain. sshd -t checks the syntax (it must print nothing), the reload applies it, and the last command reads back the values in use.
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf > /dev/null <<'EOF'
PasswordAuthentication no
PermitRootLogin no
EOF
sudo mkdir -p /run/sshd
sudo sshd -t
sudo systemctl reload ssh
sudo sshd -T | grep -E 'passwordauthentication|permitrootlogin'Keep the old session open until a fresh ssh ops@SERVER_IP in a new window works.
Turn on the firewall
On Ubuntu ufw handles IPv4 and IPv6. Open ports 80 and 443 only when this server runs a web site. Note that Docker publishes container ports around ufw; the Docker guide shows how to avoid that.
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose
# only if this server runs a web site:
sudo ufw allow 80,443/tcpAllow SSH first, otherwise enabling the firewall cuts your own connection.
Install security updates automatically
Ubuntu has unattended-upgrades for this. It installs security updates daily and, in its default setup, does not reboot the server by itself. The dry run shows what it would do.
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
sudo unattended-upgrade --dry-run --debugOptional: fail2ban
With key-only login, guessed passwords cannot succeed, so fail2ban mostly keeps the log quieter and the bots’ load lower. It is optional. The file below tells it to read the systemd journal, which is where SSH logs on this system; if the status command still reports a missing log file, check the jail name and the backend line.
sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[sshd]
enabled = true
backend = systemd
maxretry = 5
bantime = 1h
EOF
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status sshdLet an assistant do it, with a plan first
If you use an AI assistant that can run SSH commands, paste the prompt below. Give it a key you can remove afterwards, not your main key. If the assistant should also create the server, use a Cloudvix API key: keys have limits (up to €500 a month and 25 servers), which caps what a mistaken assistant can order.
The prompt makes the assistant show a plan before it acts, check each step and stop when a check fails. Read the plan before you answer yes: while connected, the assistant has the same power over the server that you have.
You are helping me do the first-hour setup of a new Ubuntu 24.04 server at SERVER_IP. Connect over SSH with the key I give you.
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
Plan to propose: update packages (and tell me if a reboot is needed); create a sudo user named ops; copy my authorized key to ops; open a second connection as ops and confirm sudo works BEFORE touching root login; then put PasswordAuthentication no and PermitRootLogin no into /etc/ssh/sshd_config.d/00-hardening.conf, run sshd -t, reload with systemctl reload ssh and keep the existing session open; allow OpenSSH in ufw BEFORE enabling ufw; install unattended-upgrades.
Do not install or change anything else, and do not print passwords or private keys. At the end, list what you changed and how to undo each change.Check the result
From your computer, ask for a password login on purpose. It must fail with Permission denied (publickey). Root must be refused as well.
ssh -o PubkeyAuthentication=no ops@SERVER_IP
ssh root@SERVER_IPIf something goes wrong
Permission denied (publickey) as ops
The key was copied to the wrong place or the permissions are off. The .ssh folder must be mode 700 and authorized_keys mode 600, both owned by ops. Fix it from the root session you kept open.
sshd -t prints an error
Missing privilege separation directory: /run/sshd. On a fresh Ubuntu 24.04 server this folder may not exist yet. Run sudo mkdir -p /run/sshd, then sudo sshd -t again. If it prints something else, do not reload yet; fix the file first.
ufw warns that enabling it may disrupt SSH
You did not allow OpenSSH first. Answer n, run sudo ufw allow OpenSSH, then enable it.
Locked out completely
Reinstall the server from the dashboard and start again. A reinstall erases the disk.
Where to go next
The next guides build on this server: install Docker, then deploy an app to it, with or without an assistant. Links are at the end of this page.
Runs Cloudvix and is responsible for the guides published here.