Create and Activate a Swap File with a systemd.swap Unit

Running low on memory and want swap that starts on boot without touching fstab: a native systemd.swap unit does the job in a handful of commands. You will create a swap file at /swapfile, describe it with a unit, activate it, and confirm the kernel is using it. Allow about 15 minutes on a host where you have root. It changes disk contents and memory paging, so read the warnings before running the privileged commands.

This guide targets the installed systemd.swap(5) manual from systemd 255.4-1ubuntu8.17. Unit syntax is local and explicit, but distribution packaging and manager defaults can differ.

1. Check the host and choose a path

Run these as your ordinary user. They show the systemd version, any existing swap, and the unit name systemd derives from the file path:

$ systemd --version | head -1
systemd 255 (255.4-1ubuntu8.17)
$ swapon --show
$ systemd-escape --path /swapfile
swapfile

swapon --show may already list active swap. That is not a problem, but check available space before adding more. The path in What= must be absolute, so a file at /swapfile belongs in swapfile.swap. Do not guess a unit name for a different path.

Checkpoint: stop if /swapfile already contains data you need. This setup is for a new file. Do not run mkswap on an existing file until you have confirmed overwriting it is fine.

2. Create and initialise the swap file

Warning: these commands need root and write a paging area. Replace 1G with a size that suits the host, and keep the commands together so a partially created file is easy to spot.

$ sudo fallocate --length 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size 1 GiB (1073741824 bytes)
no label, UUID=...

chmod 600 stops ordinary users reading the file. mkswap writes swap metadata, including a UUID, and can overwrite an existing file's contents. If fallocate or mkswap fails, do not push on to activation: inspect the file first.

$ sudo ls -lh /swapfile
$ sudo file /swapfile

A failed first command can leave a partial file behind. If it is the new file from this procedure and you want to abandon the setup, remove it with sudo rm -- /swapfile. That deletion is irreversible. If you are unsure whether the file was yours, stop and investigate instead of deleting.

3. Write the native unit

Create /etc/systemd/system/swapfile.swap with elevated privileges:

$ sudoedit /etc/systemd/system/swapfile.swap

Use this content:

[Unit]
Description=Swap file at /swapfile

[Swap]
What=/swapfile

[Install]
WantedBy=swap.target

What= is mandatory and points to the device or file used for paging. The file path creates an implicit dependency on its mount unit. The [Install] section only lets systemctl enable add the unit to swap.target; it does not activate anything by itself.

Do not add Options= just to fill the section: it passes an option string straight to swapon. Priority= is optional too, and is ignored when a pri= option already sets priority. This minimal unit avoids inventing a discard policy for storage that may not even support it.

Checkpoint: validate the file before asking systemd to use it:

$ sudo systemd-analyze verify /etc/systemd/system/swapfile.swap
$ systemctl cat swapfile.swap

A successful systemd-analyze verify normally prints nothing. systemctl cat should show all three sections and the exact What=/swapfile line. Fix the unit before continuing if verification reports an error.

4. Reload, enable, and activate it

Reload the system manager, then enable and start the unit in one go:

$ sudo systemctl daemon-reload
$ sudo systemctl enable --now swapfile.swap
Created symlink .../swap.target.wants/swapfile.swap -> /etc/systemd/system/swapfile.swap

The symlink path varies by distribution; what matters is that the command exits successfully. Swap units are system units, an unprivileged user manager cannot provide Linux swap support at all.

Check both systemd's state and the kernel's view:

$ systemctl is-active swapfile.swap
active
$ swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file  1G   0B   -2

Size, used amount and priority are host-specific. If the unit is not active, read its status and recent journal entries before trying again:

$ systemctl status --no-pager swapfile.swap
$ journalctl -b -u swapfile.swap --no-pager

Common causes: a missing file, wrong permissions, a typo in What=, or a file that was never initialised with mkswap. A non-zero result means inspect the error, not repeat the command blindly.

5. Test boot persistence without rebooting

Confirm the enablement points at the intended target:

$ systemctl is-enabled swapfile.swap
enabled
$ systemctl list-dependencies swap.target | grep -F swapfile.swap
swapfile.swap

Swap units normally pick up shutdown ordering and a relationship with the device or mount they use. The installed manual also says they are ordered before swap.target and deactivated before unmounting at shutdown, unless default dependencies are disabled. You do not need to add those relationships by hand for this unit.

Do not reboot solely to test this. The checks above already confirm the enablement link and current activation; use your normal maintenance process if a reboot is otherwise required.

6. Disable or remove the swap file

Warning: removing active swap can disrupt memory paging. Check usage first, and do this during a maintenance window if the host is under load.

$ swapon --show
$ sudo swapoff /swapfile
$ sudo systemctl disable swapfile.swap
$ sudo rm -- /etc/systemd/system/swapfile.swap
$ sudo rm -- /swapfile
$ sudo systemctl daemon-reload

swapoff must finish before you remove the file. If it reports that the system cannot free the swap, restore normal memory headroom and try again later; do not delete the file while it is still active. If you only want to stop it temporarily, run sudo systemctl stop swapfile.swap and leave the unit and file in place.

Done means