Chapter 1 · Lesson 12 of 19
Server basics

Initial server setup checklist for Ubuntu

An initial server setup is a short, ordered list of safety steps you run once on a new Ubuntu server before you put anything on it.

By the end you will

Run the first safety steps on a new Ubuntu server: updates, sudo user, SSH key and firewall.

Time
20 min
Level
Beginner
Check
4 questions
You need
  • A new Ubuntu server and its IP address
  • An SSH key on your computer

The initial setup of a new Ubuntu server is five steps: update, create a sudo user, add your SSH key, turn on a firewall and plan updates. A new server is reachable from the whole internet within minutes of being created, and automated scanners find fresh addresses quickly. The default state is usually root login, whatever the image shipped with, and no firewall. This checklist closes the obvious gaps in a fixed order, before you install an app or put data on the machine.

How it works#

Each step protects the next one, so the order is the point.

  1. Update first. The image may be months old. Patching before anything else removes known holes.
  2. Create a normal user with sudo. Daily work happens with limited rights. Elevated rights are requested one command at a time.
  3. Install your SSH key for that user. Now you can log in without a password, and later you can turn password login off.
  4. Turn on the firewall (ufw), with SSH allowed first. The default policy becomes “deny incoming”, so anything you did not open is closed.
  5. Plan updates. A server that is patched once and forgotten is only safe for a short while.

The risky moments are steps 4 and, later, SSH hardening. Both can cut off your only way in. That is why you keep the first root session open until a second, fresh login proves the new setup works.

Step 1: Log in and update#

bash
ssh root@203.0.113.10
apt update && apt upgrade -y

Replace 203.0.113.10 with your server’s address. apt update refreshes the package list and apt upgrade -y installs updates, answering yes to the prompt.

What you should see: lists of packages being fetched and unpacked, ending back at the prompt. If a kernel update was installed, the system may say a restart is required. Run reboot and log back in after a minute.

If your provider gave you a non-root user instead, log in as that user and put sudo in front of the apt commands.

Step 2: Create a sudo user#

Follow Create a sudo user. In short:

bash
adduser deploy
usermod -aG sudo deploy

What you should see: adduser asks for a password and some optional details, then prints that the user was added. usermod prints nothing when it works. The -aG flags append the user to the sudo group without removing other groups.

Step 3: Add your SSH key#

From your own computer, follow Log in with SSH keys:

bash
ssh-copy-id deploy@203.0.113.10

What you should see: a message that one key was added, and advice to try logging in. Open a new terminal and confirm ssh deploy@203.0.113.10 works without asking for the account password. If your key has a passphrase, you are asked for that instead, which is expected.

Step 4: Turn on the firewall#

Allow SSH first, then enable. See Set up a firewall with UFW.

Warning: if you enable UFW before allowing SSH, you can lock yourself out.

bash
sudo ufw allow OpenSSH
sudo ufw enable

What you should see: Rules updated, then a prompt warning that enabling may disrupt existing SSH connections. Answer y. Because SSH is allowed, your session stays up. If you plan to serve a website, also allow ports 80 and 443 now.

Step 5: Turn on automatic updates#

Read Keep a server updated and decide how you want patches applied.

Check it worked#

In a new terminal, leaving the root session open:

bash
ssh deploy@203.0.113.10
sudo ufw status

You should log in with your key and no account password. ufw status should say Status: active and list OpenSSH (and its IPv6 twin, OpenSSH (v6)) as ALLOW. Run sudo whoami and it should print root, which proves sudo works. Only then close the root session. Once all of this works, harden SSH further.

Edge cases and gotchas#

  • Your provider may have its own firewall. Many hosts offer a network firewall outside the server. Traffic must pass both it and ufw. If ufw is right and you still cannot connect, check the provider panel.
  • Docker ignores ufw for published ports. Docker writes its own iptables rules, so a container published with -p 8080:80 can be reachable even if ufw has no rule for 8080. Plan for that before you run containers.
  • IPv6 needs its own rules. ufw handles IPv6 when IPV6=yes is set in /etc/default/ufw, which is the Ubuntu default. If your server has an IPv6 address, confirm the (v6) rules appear in ufw status.
  • Cloud images may pre-install your key for a default user. On such images the user is often ubuntu. Skipping step 2 is fine if you are happy with that account, but you must still lock down SSH.
  • A pending reboot hides behind a working server. After kernel updates, the running kernel is still the old one until you reboot. Check for /var/run/reboot-required.
  • The clock matters. Wrong server time makes logs confusing and can break TLS later. Run timedatectl and check that System clock synchronized says yes.
  • Do not set a weak password “just for now”. The new user’s password protects sudo. Use a long one even if SSH uses keys.

Troubleshooting#

SymptomLikely causeFix
Permission denied (publickey) for the new userKey was not copied to that userRun ssh-copy-id deploy@203.0.113.10 again, or log in as root and fix ~/.ssh/authorized_keys
deploy is not in the sudoers fileUser missing from the sudo groupAs root run usermod -aG sudo deploy, then log in again
Connection hangs after ufw enableSSH rule missing, or a custom SSH port not allowedUse the provider’s web console and run sudo ufw allow OpenSSH
E: Could not get lock /var/lib/dpkg/lock-frontendAnother apt process (often unattended upgrades) is runningWait a few minutes and retry
ufw: command not foundufw is not installed on this imagesudo apt install ufw, then allow SSH before enabling
Everything works but root still logs inRoot login is not disabled yetDo that in the next lesson, after confirming key login
E: Unable to locate packagePackage list is stale or the repository is missingRun sudo apt update first, then retry
The server asks to restart after upgradesA new kernel or library was installedRun sudo reboot, wait a minute and log in again

Real-world scenario#

You create a server at 09:00 and the provider shows its address, 203.0.113.10. You log in as root, run apt update && apt upgrade -y, and reboot because a kernel update arrived.

Back in, you create the user deploy, add it to the sudo group and copy your key with ssh-copy-id. From a second terminal you check that ssh deploy@203.0.113.10 logs in with no password prompt and that sudo whoami prints root.

Only then do you run sudo ufw allow OpenSSH and sudo ufw enable. You keep the root session open until a third login still works. The whole job takes about fifteen minutes and leaves a server that nothing else can reach but SSH.

Quick reference#

bash
apt update && apt upgrade -y
adduser deploy && usermod -aG sudo deploy
ssh-copy-id deploy@203.0.113.10
sudo ufw allow OpenSSH && sudo ufw enable
sudo ufw status

Security and safety notes#

  • Keep your first session open until a second login as the new user works.
  • Allow SSH in the firewall before you enable it.
  • If you are locked out, the provider’s web console is your way back in.

Next#

Close the weakest door with Disable SSH password login.

Check what you learned4 questions
1Why allow OpenSSH in ufw before running ufw enable?
2What is the point of the sudo user step?
3How do you confirm the setup worked?
4Your provider has its own network firewall. You allowed SSH in ufw but cannot connect. What do you check?

Frequently asked questions

Why should I stop using root on a new server?

Root can change or delete anything, and attackers target the root account first. A normal user with sudo limits the damage from mistakes and gives you a clearer audit trail.

Do I need to repeat this for every server?

Yes. Each new server starts with the provider defaults, so run the same checklist every time. A saved note or script makes it quick.

Why does the order matter?

Each step depends on the one before. If you enable the firewall before allowing SSH, or close root login before your new user works, you can lock yourself out.

The provider created a user for me already. Do I still need a new one?

Many images ship a default user such as ubuntu with sudo and your key already installed. You can use it, but check that it is the account you want to keep and that password login is off.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios