Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

How to set up and secure a new Ubuntu 24.04 VPS

A fresh Ubuntu 24.04 VPS needs a short round of setup before it is safe to use. This guide walks through each step in order, with the exact commands and a check after every change so you never lose access.

Short answer: Log in as root with your SSH key, run apt update and apt upgrade, create a sudo user and copy your key to it, then add an sshd_config.d drop-in that disables root login and password authentication. Allow OpenSSH in ufw before enabling it, confirm unattended-upgrades is on, set the timezone, hostname and swap, check ports with ss -tlnp and take a snapshot.

What initial setup on an Ubuntu 24.04 VPS involves

A new Ubuntu 24.04 LTS VPS gives you a root login and little else. Before you put anything on it, update the packages, create a normal user with sudo rights, make SSH key-only with root login turned off, switch on a firewall, confirm automatic security updates are running, and take a snapshot. The whole job takes about fifteen minutes, and every step below uses commands that ship with Ubuntu Server 24.04.

Steps 1 and 2 run as root. From Step 3 on, the commands are run as your new user with sudo, which is called deploy here. Swap in your own name.

Quick checklist

  1. Log in as root with your SSH key and run apt update and apt upgrade.
  2. Create a user, add it to the sudo group and copy your key to it.
  3. Test a login as that user in a second terminal.
  4. Add an sshd_config.d drop-in that disables root login and passwords, test it with sshd -t, restart ssh and test again.
  5. Allow OpenSSH in ufw, then enable it.
  6. Check unattended-upgrades, set the timezone and hostname, add swap if RAM is small.
  7. Check listening ports with ss -tlnp and take a snapshot.

Don't lock yourself out

Three steps here can cut off your own access: the SSH changes, the firewall and a wrong key. Keep your first root session open until you have opened a second terminal and logged in successfully with the new settings. If that second login fails, fix the problem from the session you still have. Make one change at a time and test after each. If you do get locked out, you need a console that does not depend on SSH, which most VPS panels provide.

Step 1: first login and package updates

Connect as root with the key you added when you ordered the server:

ssh root@203.0.113.10

Then refresh the package lists and install pending updates:

apt update
apt upgrade -y

If the upgrade pulls in a new kernel, the file /var/run/reboot-required will exist. Reboot with reboot once the rest of the setup is done, rather than in the middle of it.

Step 2: create a sudo user and copy your key

Working as root all the time means one mistyped command can damage the whole system. Create a normal user and give it sudo rights:

adduser deploy
usermod -aG sudo deploy

adduser asks for a password. Set a strong one: you will not use it to log in over SSH, but sudo asks for it. Now copy root's authorised keys to the new user and set the ownership and permissions SSH insists on:

mkdir -p /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys

From your own computer, open a new terminal and test: ssh deploy@203.0.113.10, then sudo whoami, which should print root. Do not continue until both work.

Step 3: disable root login and password authentication

On Ubuntu, /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, and for most settings OpenSSH uses the first value it reads. So a drop-in file overrides the main file, and a file whose name sorts first wins over other drop-ins, including any a cloud image may have added. Create /etc/ssh/sshd_config.d/00-hardening.conf:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no

Test the syntax, restart the service, then check the values sshd will actually use:

sudo sshd -t
sudo systemctl restart ssh
sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication)'

All three should read no. If sshd -t prints an error, fix it before restarting. Restarting does not drop your existing session.

The ssh.socket change in Ubuntu 24.04

Since Ubuntu 22.10, the SSH server is socket-activated by default: systemd's ssh.socket holds port 22 and starts ssh.service when a connection arrives. Check with systemctl is-active ssh.socket. This matters in two ways:

  • For settings such as PermitRootLogin and PasswordAuthentication, sudo systemctl restart ssh is enough, as above.
  • If you change Port or ListenAddress, restarting ssh is not enough, because the socket decides where to listen. Run sudo systemctl daemon-reload and then sudo systemctl restart ssh.socket, and confirm with systemctl status ssh.socket. Open the new port in every firewall first.

Now repeat the test from a second terminal: ssh deploy@203.0.113.10 should work, and ssh root@203.0.113.10 should be refused with Permission denied (publickey). If you get connection refused instead, see SSH connection refused: causes and fixes.

Step 4: turn on the ufw firewall

Ubuntu ships ufw but leaves it disabled. Its default policy denies incoming connections, so allow SSH before you enable it:

sudo ufw app list
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose

ufw enable warns that the command may disrupt existing SSH connections; answer y only after the allow rule is in place. OpenSSH is the profile the openssh-server package installs; it opens port 22. If you moved SSH to another port, allow that port number instead. Add web ports only when you run a web server: sudo ufw allow 80,443/tcp.

Step 5: automatic security updates

On Ubuntu Server the unattended-upgrades package is installed and switched on by default, and it applies security updates only. Confirm rather than assume:

cat /etc/apt/apt.conf.d/20auto-upgrades
systemctl status unattended-upgrades

Both APT::Periodic lines should be "1" (daily). If the file is missing, run sudo apt install unattended-upgrades and then sudo dpkg-reconfigure -plow unattended-upgrades. Settings live in /etc/apt/apt.conf.d/50unattended-upgrades; automatic reboots are off by default, so check for /var/run/reboot-required now and then and reboot at a quiet time.

Step 6: timezone and hostname

Servers default to UTC. Setting Indian time makes logs and cron schedules easier to read:

sudo timedatectl set-timezone Asia/Kolkata
timedatectl

Give the server a name you will recognise in your prompt and in logs:

sudo hostnamectl set-hostname web1

Add the same name to /etc/hosts on the 127.0.1.1 line so sudo does not warn that it cannot resolve the host. If the old name comes back after a reboot, cloud-init is managing it: set preserve_hostname: true in a file such as /etc/cloud/cloud.cfg.d/99-hostname.cfg, which a package upgrade will not replace the way it can /etc/cloud/cloud.cfg.

Step 7: add a swap file on small plans

With 1 or 2 GB of RAM, a swap file gives the kernel somewhere to put idle memory and makes out-of-memory kills during an apt run or a build less likely. It is not a substitute for enough RAM; how much RAM a VPS needs covers sizing. Check first with swapon --show; if a swap file already exists, skip this step. On the default ext4 filesystem, create a 2 GB file:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
free -h

Run the tee -a line only once, or the entry is duplicated. A typo in /etc/fstab can stop the server booting, so check the file with sudo findmnt --verify before you reboot.

Step 8: fail2ban (optional)

With passwords disabled, brute-force attempts against SSH cannot succeed, but they still fill your logs. fail2ban bans IPs that fail repeatedly:

sudo apt install fail2ban
sudo fail2ban-client status sshd

Ubuntu's package enables the sshd jail out of the box. Put your own changes in /etc/fail2ban/jail.local, not in jail.conf, which package updates can replace. If you use a fixed office IP, add it to ignoreip so you never ban yourself.

Step 9: check what is listening

Before you install anything else, see which TCP ports are open and which process owns each:

sudo ss -tlnp

On a fresh server you should see port 22 (owned by systemd and sshd because of socket activation) and the local DNS resolver on 127.0.0.53 and 127.0.0.54. Anything bound to 0.0.0.0 or [::] is reachable from outside unless a firewall blocks it. Run the same check after every new service you install.

Step 10: snapshot and backups

Once the base system is clean, take a snapshot. If a later change goes wrong you can roll back to a hardened, updated server instead of starting over. A snapshot sits on the same platform as the server, so also keep an off-server copy of your data and configuration, and test restoring it.

From here you can install your stack; deploying a Python app on a VPS picks up where this guide ends.

Setting up an Ubuntu VPS on Bigdomainhost

A Bigdomainhost KVM VPS offers Ubuntu 20.04, 22.04 and 24.04 images with full root access, in Mumbai (BOM-1) or Delhi NCR (DEL-1). Paste your SSH public key at order time: the VPS quickstart notes this disables password login from the first boot, so you log in as root with your key straight away.

The panel has its own per-instance firewall in front of ufw. Its default denies inbound traffic except SSH on port 22, so a new web or custom SSH port has to be opened in both places. If an SSH or firewall change locks you out, the out-of-band console in the control panel still gets you in. Snapshots are taken and restored from the instance view and billed by the space they use; scheduled off-server backups are an add-on, and the snapshots and backups docs explain the difference. If you would rather go through the setup with someone, call +91 75994 50220 during IST hours, WhatsApp the same number at any time, or email support@bigdomainhost.com.

Sources (checked )

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

What should I do first on a new Ubuntu VPS?

Update the packages with apt update and apt upgrade, then create a normal user with sudo rights and copy your SSH key to it. After that, disable root login and password authentication, enable the ufw firewall with OpenSSH allowed, and check that automatic security updates are running. Take a snapshot once the base system is clean.

How do I restart SSH on Ubuntu 24.04?

Run sudo sshd -t to test the configuration, then sudo systemctl restart ssh. That applies settings such as PermitRootLogin and PasswordAuthentication. Because Ubuntu 24.04 uses socket activation, a change to Port or ListenAddress also needs sudo systemctl daemon-reload followed by sudo systemctl restart ssh.socket before the new port takes effect.

Why should I use sshd_config.d instead of editing sshd_config?

Ubuntu's sshd_config includes every .conf file in /etc/ssh/sshd_config.d at the top, and OpenSSH keeps the first value it reads for most settings. A drop-in therefore overrides the main file, keeps your changes separate from the packaged defaults and survives package upgrades that replace sshd_config. Name it so it sorts before other drop-ins.

Will enabling ufw disconnect my SSH session?

It can, if SSH is not allowed first. ufw denies incoming connections by default once enabled, so run sudo ufw allow OpenSSH, or allow your custom SSH port, before sudo ufw enable. Then confirm with sudo ufw status verbose and test a new login from a second terminal before closing your current one.

Do I need swap on a VPS?

On a plan with 1 or 2 GB of RAM, a small swap file is worth adding because it reduces the chance of the kernel killing processes during upgrades or builds. It is much slower than RAM, so it is a safety margin, not extra capacity. If the server swaps constantly, move to a plan with more memory.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.