basforge

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

    RU

    First login to a server: updates, hostname, a sudo user, key login

    The first 20 minutes of a fresh Ubuntu VPS: update packages, give the server a name and a time zone, create your own user with sudo and a key, and close password and root logins. You need the server address, the root password or a key from the provider’s panel, and an SSH key on your computer (SSH keys).

    What you end up with

    before                                after
    root logs in with a password          root can't log in over SSH
                                          example-user logs in with a key, sudo asks example-user's password
    packages as of the image              all updates installed, server rebooted into the new kernel
    name vm-4821, UTC                     name web1, your time zone

    All steps are on one server: address 203.0.113.10, user example-user. Until you reach step 9 password login is still open, so don’t leave this for tomorrow.

    Setup

    1. Check the fallback access

      In the provider’s panel, open the server’s web console (VNC, “Console”, “Emergency access” — every provider names it differently) and log in as root. If the console won’t let you in or there’s no root password, sort it out now while SSH is open: later it’s the only way onto the server if you lock yourself out (Provider console).

    2. Log in as root

      ssh [email protected]

      On the first login ssh asks about the server key. Compare the fingerprint through the console from the previous step: ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub (Server fingerprint).

    3. Install updates

      apt update
      apt full-upgrade -y

      full-upgrade also removes packages when an upgrade can’t go in otherwise; on a fresh server that’s safe. If apt says Could not get lock, the server is already updating itself — wait (see “If it doesn’t work”).

      If the kernel was updated, reboot right away, while nothing runs on the server yet:

      cat /var/run/reboot-required 2>/dev/null && reboot
      # *** System restart required ***

      Log in again after a minute. uname -r shows the new kernel: 6.8.0-… on 24.04, 7.0.0-… on 26.04.

    4. Set the hostname

      The name shows in the shell prompt, in logs and in mail from the server. It’s how you tell servers apart with several windows open.

      hostnamectl set-hostname web1
      sed -i 's/^127\.0\.1\.1.*/127.0.1.1 web1/' /etc/hosts
      grep -q '^127.0.1.1' /etc/hosts || echo '127.0.1.1 web1' >> /etc/hosts

      Without the /etc/hosts line every sudo prints unable to resolve host. If the provider manages the name through cloud-init, it comes back after a reboot — use preserve_hostname: true, see “If it doesn’t work”.

    5. Set the time zone

      timedatectl set-timezone Europe/Berlin     # your own zone; list: timedatectl list-timezones
      timedatectl
      #                Local time: Sun 2026-10-18 21:40:11 CEST
      #            Universal time: Sun 2026-10-18 19:40:11 UTC
      #                 Time zone: Europe/Berlin (CEST, +0200)
      # System clock synchronized: yes
      #               NTP service: active

      The time zone decides when “4:00” comes for the automatic reboot (unattended-upgrades) and for cron. If you have servers in several countries and compare their logs, keep them all on UTC. The zone list: timedatectl list-timezones.

      System clock synchronized: yes means the clock syncs. If it says no, deal with it now (Swap, journal and time): without accurate time, certificates aren’t issued and logs lie.

    6. Create your own user

      adduser example-user
      usermod -aG sudo example-user

      adduser asks for a password — make it long and store it in a password manager. You won’t log in over SSH with it, but sudo and the provider console need it.

      The sudo group grants sudo through the %sudo ALL=(ALL:ALL) ALL rule in /etc/sudoers. No separate file in sudoers.d is needed.

      On 26.04, sudo-rs replaces classic sudo. Rules and the group are the same; the differences are small: the password shows as asterisks, and sudo -E doesn’t pass environment variables — list the ones you need with sudo --preserve-env=HTTP_PROXY ….

    7. Give the user a key

      If root already has the key (the provider’s panel put it there), copy it with the right ownership:

      rsync --archive --chown=example-user:example-user /root/.ssh /home/example-user/

      If there’s no key on the server, run this from your computer while passwords are still open: ssh-copy-id [email protected] (on Windows, the command from SSH keys, step 3). Add a second key from another device right away.

    8. Log in as the new user — in a second window

      Don’t close the root window (Keep the old session open). In a new window:

      ssh [email protected]
      sudo -v && sudo whoami
      # [sudo] password for example-user:
      # root

      A login without the user’s password (only the key’s passphrase was asked) and root in the reply — all set.

    9. Close password and root logins

      In the second window, now as example-user:

      sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
      # Local sshd settings. Named 00-: sshd uses the first value it finds
      PasswordAuthentication no
      KbdInteractiveAuthentication no
      PermitRootLogin no
      AllowUsers example-user
      EOF
      sudo sshd -t && sudo systemctl restart ssh

      The file is called 00-… so it’s read before 50-cloud-init.conf: that one often has PasswordAuthentication yes, and sshd uses the first value. The rest of the file — MaxAuthTries, forwarding, timeouts — is covered in sshd_config.

      Test from a third window (see “How to check”), and only then close the root window.

    10. Lock the root password

      sudo passwd -l root
      sudo passwd -S root
      # root L 2026-10-18 0 99999 7 -1

      L in the second field means the password is locked. Root can no longer log in with a password anywhere, including the provider console — there you log in as example-user and use sudo. So do this step only after example-user has logged in through the console with its password at least once.

    Next: the firewall (The ufw firewall).

    If it doesn’t work

    Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (unattended-upgr)

    Cause
    In the first minutes after boot the server installs security updates on its own.
    Fix
    Wait for the process to finish: while pgrep -x unattended-upgr >/dev/null; do sleep 5; done. Don’t delete the lock files.

    During the upgrade — a dialog A new version of configuration file /etc/ssh/sshd_config is available

    Cause
    The provider or cloud-init changed the file, and the package doesn’t know whose version to keep.
    Fix
    Pick keep the local version currently installed: your own settings live in sshd_config.d/ anyway (sshd_config).

    sudo: unable to resolve host web1: Name or service not known

    Cause
    The hostname was changed, but /etc/hosts doesn’t have it.
    Fix
    Add the line 127.0.1.1 web1 to /etc/hosts.

    After a reboot the hostname is vm-4821 again

    Cause
    cloud-init sets the name from the provider’s settings on every boot.
    Fix
    echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-hostname.cfg, then hostnamectl set-hostname again.

    example-user is not in the sudoers file

    Cause
    The user isn’t in the sudo group, or was added but the session is old: groups are read at login.
    Fix
    As root: usermod -aG sudo example-user, then log out and back in; check with groups example-user.

    The new user logs in but gets Could not chdir to home directory /home/example-user

    Cause
    The user was created with useradd without -m — there’s no home directory.
    Fix
    Create users with adduser: it makes the directory, copies /etc/skel and asks for a password.

    How to check

    From your computer, in a new window. No one’s password is accepted, and root can’t log in even with a key:

    ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password [email protected]
    # [email protected]: Permission denied (publickey).
    ssh [email protected]
    # [email protected]: Permission denied (publickey).

    Your own login works, and so does sudo:

    ssh -t [email protected] 'sudo whoami'
    # [sudo] password for example-user:
    # root

    On the server, what’s in effect with all files applied:

    sudo sshd -T | grep -Ei '^(passwordauthentication|permitrootlogin|allowusers) '
    # permitrootlogin no
    # passwordauthentication no
    # allowusers example-user