basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    A new Ubuntu server: what to set up in the first hour and why

    A fresh VPS comes with root, port 22 open and packages as old as the image — bots start guessing the password minutes after boot. The order: access, updates, firewall, system, Docker, brute-force bans. The base is Ubuntu 24.04 LTS; where 26.04 behaves differently, it says so next to it.

    What problem it solves

    A provider creates a server in a minute, and in that same minute bots start knocking on its address: data-center ranges are scanned nonstop. The provider’s image is built so you can log in, not so it can live on the internet for years.

    How the server arrives

    • login as root with a password from an email or a key from the panel
    • port 22 open to the whole internet, password login allowed
    • packages as of the image build, sometimes six months old
    • UTC time zone, a name like vm-4821
    • no firewall, no swap, a journal that grows to 10% of the disk (capped at 4 GB)

    How we leave it

    • your own sudo user, key-only login, root closed over SSH
    • only 22, 80 and 443 open to the outside
    • security updates install themselves every night, with a 4:00 reboot when needed
    • 2 GB of swap, a journal capped at 500 MB, the clock in sync
    • Docker from Docker’s repository, container logs capped at 30 MB
    • an address that guesses logins gets banned across the whole server

    The order

    The steps depend on each other: you can’t close passwords until key login is tested, and you can’t turn the firewall on until it has a rule for SSH. Hence this order:

    server from the provider panel · root · password

    1. Fallback accessprovider consoleBefore any change, check that the web console in the panel works. It bypasses the network and SSH and saves you if you lock yourself out (Provider console).
    2. Accessadduser · sudo · keyUpdate packages, create a user, add a key, log in with it, close passwords and root (First login to a server).
    3. Firewallufw · 22, 80, 443Rules first, then ufw enable. ufw doesn’t close Docker’s ports — that’s a separate story (Docker ports bypass the input chain).
    4. Updatesunattended-upgradesAlready on in Ubuntu, but without reboots: a new kernel waits until someone boots it (unattended-upgrades).
    5. Systemswap · journald · timeSwap for a small VPS, a journal limit, a clock check (Swap, journal and time).
    6. Dockerdocker-ce · daemon.jsonFrom Docker’s repository, with a log limit (Docker on Ubuntu). Before the first container: the limit in daemon.json applies only to containers created after it.
    7. Brute-force bansCrowdSecCrowdSec in a container bans an address that guesses logins on every port of the server (CrowdSec against SSH brute force).

    a server you can leave on the internet

    Install Docker in the first hour, not next to the first site: the services you add yourself from here on — CrowdSec, Traefik, sites and apps — run in containers. Each containerized service lives in its own directory with a compose file. The image tag sets the version. Update a service with docker compose pull and up -d; remove it with down and delete the directory. Service dependencies don’t mix with system packages. That’s why such a server can be rebuilt from files (Server configs in git). Next come the proxy and the first site (Traefik).

    Ubuntu 24.04 or 26.04

    Both are LTS, both get 5 years of standard support plus 5 more with Ubuntu Pro (free for personal use on up to 5 machines). The differences you’ll notice when setting up a server:

    24.04 LTS “Noble Numbat” 26.04 LTS “Resolute Raccoon”
    release and support April 2024, updates until April 2029 April 2026, updates until April 2031
    kernel 6.8 (newer via HWE) 7.0
    OpenSSH 9.6: no PerSourcePenalties 10.2: hybrid ML-KEM key exchange by default, DSA keys rejected
    sudo classic sudo sudo-rs in Rust; sudo -E doesn’t keep the environment — you need --preserve-env=VAR; the password shows as asterisks
    time systemd-timesyncd, plain NTP chrony with NTS (encrypted NTP, outbound TCP 4460) on new installs; an upgrade from 24.04 keeps timesyncd
    utilities GNU coreutils Rust coreutils (uutils); cp, mv and rm are still GNU — their switch was postponed to 26.10
    cgroup v1 still there removed: old containers that need v1 won’t start
    minimum RAM 1 GB 1.5 GB per the installer requirements; an installed system runs on 1 GB too
    Docker supported supported from release, resolute packages in Docker’s repository
    If… Use
    a new server, the provider offers a 26.04 image, and the stack is Docker and regular packages 26.04: two more years of updates, no move in 2029
    you need a third-party repository or agent that doesn’t support 26.04 yet 24.04
    the server runs old containers or software that needs cgroup v1 24.04
    a server already running 24.04 keep it or upgrade: once 26.04.1 is out, sudo do-release-upgrade offers 26.04 on 24.04 too

    The commands in the set are the same for both versions. Where 26.04 differs, the step says so.

    How to set it up: by hand, cloud-init or Ansible

    How it works When to use
    by hand from the manuals commands step by step, a check after each the first server: you see what each command does and what breaks without it
    cloud-init (cloud-init) YAML in the “user data” field at creation, runs on first boot servers get created often, or the provider supports user data
    Ansible a playbook run from your computer over SSH, repeatable: a second run changes nothing more than five servers, or the setup has to be repeated and compared

    The set starts with the manual path: cloud-init and Ansible do the same steps, and figuring out why something didn’t work is easier when you know the steps.

    What you get and what it costs

    What you get

    • Password guessing is useless. Key-only login, root closed over SSH; CrowdSec bans an address after a few attempts, and the ban covers both SSH and the sites (Docker ports bypass the input chain).
    • Three ports open to the outside. Everything else is deny incoming; services only the server itself needs listen on 127.0.0.1.
    • Holes close themselves. Once a day unattended-upgrades installs packages from noble-security (apt-daily-upgrade.timer, around 6:00 with up to an hour of jitter) — no login needed.
    • The server survives memory spikes. Swap gives the kernel time instead of an instant Out of memory.

    What it costs

    • A nightly reboot. With Automatic-Reboot, every few weeks the server reboots at 4:00 — sites are down for 1–2 minutes. Without it, a new kernel doesn’t run until you reboot yourself (unattended-upgrades).
    • ufw and Docker live apart. ufw deny 8080 doesn’t close a container’s published port: Docker writes its own rules around it (Docker ports bypass the input chain, The ufw firewall).
    • Lose the key, lose the login. Passwords are closed, and only the provider console is left. Add a second key right away.
    • 26.04 is still young. The Rust versions of sudo and coreutils behave differently in places: scripts with sudo -E silently lose variables.

    Where to start

    1. Overviewthis pageA new Ubuntu server: what to set up in the first hour and why
    2. ManualFirst login to a server: updates, hostname, a sudo user, key loginUpdates, hostname, time zone, your own sudo user with a key, key-only login. The first 20 minutes.
    3. ManualFirewallThe ufw firewall: everything closed except SSH and webufw: everything closed except SSH and web — and why it doesn’t close Docker’s ports.
    4. Configunattended-upgrades: security updates on their own and a reboot at 4:00unattended-upgrades block by block: what installs itself, when the server reboots, and how to check.
    5. ManualSwap, journal and time: three settings for a small VPSThree settings for a small VPS: a swap file, a journal size limit, clock sync.
    6. ManualDocker EngineDocker on Ubuntu: from Docker’s repository, with a log limitFrom Docker’s repository, not snap, with container log rotation. Before the bans: CrowdSec runs in a container.
    7. ManualCrowdSecCrowdSec against SSH brute force: a ban across the whole serverCrowdSec reads auth.log and bans addresses that guess logins, across the whole server.
    8. Cheat sheetAn Ubuntu server: everyday commandsServices, journal, packages, disk, network — everyday commands.
    9. Configcloud-init: a server configured the moment it’s createdThe same as a file at server creation: user, key, packages, firewall.

    If it doesn’t work

    After ufw enable SSH won’t let you in, new sessions hang

    Cause
    The firewall went on before the SSH port was allowed: the deny incoming policy closed it too.
    Fix
    Provider console (Provider console): sudo ufw allow OpenSSH or sudo ufw disable. For next time, follow the order in The ufw firewall.

    Could not get lock /var/lib/dpkg/lock-frontend

    Cause
    Right after first boot unattended-upgrades installs updates on its own and holds the apt lock.
    Fix
    Wait a few minutes: ps aux | grep -E 'unattended|apt' shows who holds it. Don’t delete the lock files — that’s how the package database gets broken.

    Login shows *** System restart required ***

    Cause
    The kernel or a library that can’t be swapped live was updated.
    Fix
    What needs the reboot: cat /var/run/reboot-required.pkgs; reboot at a quiet time (Is a reboot needed).

    Processes on a small VPS vanish, and dmesg shows Out of memory: Killed process

    Cause
    1–2 GB of RAM and no swap: the kernel kills the biggest process, often the database.
    Fix
    A 1–2 GB swap file (Swap, journal and time) and memory limits for containers.

    The disk filled up within a few months, though there’s little data

    Cause
    The systemd journal, Docker container logs without rotation, old kernels and images.
    Fix
    sudo du -xh / --max-depth=2 | sort -h | tail; limits — Swap, journal and time and Docker on Ubuntu.

    The server clock lags, TLS certificates are “not yet valid”

    Cause
    Clock sync doesn’t work: on 26.04 chrony uses NTS by default and needs outbound TCP port 4460.
    Fix
    timedatectl should say System clock synchronized: yes; on 26.04, chronyc sources (Swap, journal and time).