basforge

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

    RU

    Swap, journal and time: three settings for a small VPS

    A 2 GB swap file so a memory spike doesn’t end in Out of memory; a 500 MB cap on the systemd journal so it doesn’t eat the disk; a check that the clock syncs — timesyncd on 24.04, chrony with NTS on 26.04.

    Swap

    A VPS with 1–2 GB of RAM usually comes without swap. As long as memory suffices, you don’t notice. When it doesn’t — an image build, apt, a database spike — the kernel calls the OOM killer, which kills the biggest process. Most often that’s the database or Docker itself. With swap, the kernel moves rarely used pages to disk, and the server gets through the spike slower but alive.

    With How it works When to use
    a swap file on disk memory pages go to /swapfile almost always: created in a minute, resized without repartitioning
    zram compressed swap in memory itself, about 2–3 to 1 (zram-tools) a slow disk or one with a write limit; 1 GB of RAM or less
    no swap memory is plentiful, or every container has a limit 8 GB of RAM or more, a known load

    The size is 2 GB with 1–4 GB of RAM. A server doesn’t need more: if it lives in swap all the time, it’s short on memory, and swap only drags that out.

    Create the swap file

    1. Check there’s no swap

      swapon --show
      # empty — no swap
      free -h
      #                total        used        free      shared  buff/cache   available
      # Mem:           1.9Gi       612Mi       230Mi       4.0Mi       1.2Gi       1.2Gi
      # Swap:             0B          0B          0B
    2. Create and enable it

      sudo fallocate -l 2G /swapfile
      sudo chmod 600 /swapfile
      sudo mkswap /swapfile
      sudo swapon /swapfile

      chmod 600 is required: swap receives whatever was in memory, process passwords included. On ext4 — the file system of Ubuntu cloud images — fallocate creates the file instantly. On btrfs, see “If it doesn’t work”.

    3. Enable it at boot

      echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

      Check the line before rebooting: a mistake in /etc/fstab can stop the boot at the emergency console. sudo findmnt --verify checks the file without a reboot.

    4. Make the kernel less eager to swap

      vm.swappiness is how readily the kernel moves pages to disk. The default of 60 is a desktop setting. On a server, set 10: swap kicks in when memory is in fact short.

      echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
      sudo sysctl --system | grep swappiness
      # vm.swappiness = 10

    The systemd journal

    journald keeps the journal in /var/log/journal and by default takes up to 10% of the file system, capped at 4 GB. On a 25 GB disk that’s 2.5 GB — a tenth of the server for logs. Set the limit explicitly.

    rsyslog runs alongside: it writes the same events to /var/log/syslog and /var/log/auth.log, which logrotate rotates. Don’t remove it — CrowdSec reads login attempts from /var/log/auth.log (CrowdSec against SSH brute force).

    1. See how much it takes

      journalctl --disk-usage
      # Archived and active journals take up 1.4G in the file system.
    2. Set the limit

      sudo mkdir -p /etc/systemd/journald.conf.d
      sudo tee /etc/systemd/journald.conf.d/00-size.conf >/dev/null <<'EOF'
      [Journal]
      SystemMaxUse=500M
      SystemKeepFree=1G
      EOF
      sudo systemctl restart systemd-journald

      SystemMaxUse=500M caps the journal at 500 MB; on a small server that’s a few weeks of history. SystemKeepFree=1G: if less than 1 GB is left on disk, the journal gives way before hitting its cap. A separate file in journald.conf.d survives package upgrades, just as with sshd and apt.

    3. Remove the excess right away

      sudo journalctl --vacuum-size=500M
      # Vacuuming done, freed 912.0M of archived journals from /var/log/journal/…

    Docker container logs are a separate story; their limit goes in daemon.json (Docker on Ubuntu).

    Time

    An accurate clock isn’t cosmetic. A browser rejects a certificate whose notBefore is in the future, two-factor codes live 30 seconds, and CrowdSec and logs from different servers are matched by time.

    Ubuntu 24.04 Ubuntu 26.04
    service systemd-timesyncd chrony on new installs; an upgrade from 24.04 keeps timesyncd
    protocol NTP, UDP 123 NTS — NTP with server authentication: TCP 4460 for key exchange plus UDP 123
    your own servers /etc/systemd/timesyncd.conf.d/*.conf, NTP= /etc/chrony/sources.d/*.sources, then sudo chronyc reload sources
    check timedatectl timesync-status chronyc tracking, chronyc -N authdata

    NTS protects against time being spoofed on the way: plain NTP can be forged, and the server will trust someone else’s clock.

    1. Check the sync

      timedatectl
      #                Local time: Sun 2026-10-18 19:40:11 UTC
      #            Universal time: Sun 2026-10-18 19:40:11 UTC
      #                 Time zone: Etc/UTC (UTC, +0000)
      # System clock synchronized: yes
      #               NTP service: active

      yes and active — the clock is fine, and you’re done.

    2. If it says no — find which server is unreachable

      On 24.04:

      timedatectl timesync-status
      #        Server: 185.125.190.56 (ntp.ubuntu.com)
      #  Poll interval: 34min 8s (min: 32s; max 34min 8s)
      #         Offset: -1.204ms

      No Server line, or an Offset in seconds — look for what blocks outbound UDP 123.

      On 26.04:

      chronyc -N authdata
      # Name/IP address             Mode KeyID Type KLen Last Atmp  NAK Cook CLen
      # =========================================================================
      # 1.ntp.ubuntu.com             NTS     4   30  128  24d    0    0    8   64
      # 2.ntp.ubuntu.com             NTS     4   30  128  24d    0    0    8   64
      # ntp-bootstrap.ubuntu.com     NTS     4   30  128  24d    0    0    8   64

      This is the working state: KeyID isn’t zero, Cook is the stock of keys for requests, Last is how long ago chrony got keys over TCP 4460. The default sources are Ubuntu’s pools from /etc/chrony/sources.d/ubuntu-ntp-pools.sources, all with nts.

      If outbound TCP 4460 is closed, there are no keys, and chrony sends no time requests at all, even though UDP 123 is open:

      chronyc sources
      # MS Name/IP address         Stratum Poll Reach LastRx Last sample
      # ===============================================================================
      # ^? ntp-nts-2.ps5.canonical.>     0   6     0     -     +0ns[   +0ns] +/-    0ns
      # ^? ntp-nts-3.ps5.canonical.>     0   6     0     -     +0ns[   +0ns] +/-    0ns
      
      chronyc -N authdata
      # 1.ntp.ubuntu.com             NTS     0    0    0    -    2    0    0    0
      # 2.ntp.ubuntu.com             NTS     0    0    0    -    2    0    0    0
      
      journalctl -u chrony --since "-10min" | grep NTS-KE
      # chronyd[442730]: NTS-KE session with 203.0.113.123:4460 (2.ntp.ubuntu.com) timed out

      The signs: every source ^? with Reach 0, zeros in authdata and a growing Atmp attempt counter, NTS-KE session … timed out in the journal.

      A closed port doesn’t show right away. chrony keeps its keys in /var/lib/chrony, reuses them after a restart, and fetches new ones only every few weeks. So a server whose egress firewall closed 4460 keeps syncing for a while, and then the clock stops being checked for no visible reason.

    If it doesn’t work

    swapon: /swapfile: swapon failed: Invalid argument or skipping - it appears to have holes

    Cause
    The file was created with fallocate on btrfs or an old XFS — there it ends up with “holes”, and the kernel won’t use it as swap.
    Fix
    Create the file with dd: sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress, then the same chmod, mkswap, swapon. On btrfs, use btrfs filesystem mkswapfile.

    swapon: /swapfile: insecure permissions 0644, 0600 suggested

    Cause
    Everyone can read the swap file, and it can hold passwords and keys from process memory.
    Fix
    sudo chmod 600 /swapfile.

    The journal still takes several gigabytes

    Cause
    The cap applies to new writes; journald removes old files only when it writes new ones.
    Fix
    sudo journalctl --vacuum-size=500M removes the old data right away.

    timedatectl: System clock synchronized: no

    Cause
    Outbound UDP 123 is closed (at the provider or in your own egress firewall), and on 26.04 TCP 4460 for NTS as well.
    Fix
    Look at timedatectl timesync-status (24.04) or chronyc sources -v (26.04); open the outbound ports or set the provider’s NTP server.

    26.04: chronyc sources shows every source as ^? with Reach 0, though UDP 123 is open

    Cause
    Outbound TCP 4460 is closed: chrony gets no NTS keys and without them sends no time requests.
    Fix
    Check journalctl -u chrony | grep NTS-KE — it shows NTS-KE session … timed out; open TCP 4460 outbound and run sudo systemctl restart chrony.

    Failed to set ntp: NTP not supported, or chronyd won’t start

    Cause
    A VPS on container virtualization (LXC, OpenVZ): the clock is shared with the host, and the guest can’t change it.
    Fix
    Do nothing — the provider keeps the clock. Only check that the time is right: date -u against an accurate clock.

    How to check

    swapon --show
    # NAME      TYPE SIZE USED PRIO
    # /swapfile file   2G   0B   -2
    cat /proc/sys/vm/swappiness
    # 10
    journalctl --disk-usage
    # Archived and active journals take up 488.0M in the file system.
    timedatectl show -p NTPSynchronized --value
    # yes

    After a reboot, the same: swap came up from /etc/fstab, and swappiness is still 10.