Skip to contentcloudvix
cloudvix
GuideServers & hosting

Initial server setup on Ubuntu 24.04: the first hour

Denys DunaievskyiFounder, Cloudvix
  • 13 min read
  • Published
  • LevelBeginner
Guides
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.

On your computerbash
ssh root@SERVER_IP

Update the system

Install pending updates first. If one of them needs a reboot, the last line restarts the server; log in again afterwards.

On the serverbash
apt update
apt upgrade -y
test -f /var/run/reboot-required && reboot

Create 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.

On the serverbash
adduser ops
usermod -aG sudo ops

Give 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.

On the serverbash
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_keys

Check 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.

On your computerbash
ssh ops@SERVER_IP
sudo whoami

Turn 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.

On the server · /etc/ssh/sshd_config.d/00-hardening.confbash
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'
Warning

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.

On the serverbash
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose
# only if this server runs a web site:
sudo ufw allow 80,443/tcp
Warning

Allow 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.

On the serverbash
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
sudo unattended-upgrade --dry-run --debug

Optional: 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.

On the serverbash
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 sshd

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.

On your computerbash
ssh -o PubkeyAuthentication=no ops@SERVER_IP
ssh root@SERVER_IP

If 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.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server