ubto66
September 30, 2026, 4:11pm
1
Ubuntu Version
Ubuntu 24.04 LTS
Desktop Environment (if applicable)
GNOME
Problem Description
I was told you can get more swap space using these commands
mkswap -U clear --size 4G --file /path/to/swapfile
swapon /path/to/swapfile
Can you verify if this will work? Thanks.
steelf
September 30, 2026, 4:17pm
2
No, that won’t work.
mkswap has no --size or --file options - it only formats an already existing file or block device.
On 24.04, swap is normally /swap.img.
To make a 4 GB swapfile, allocate the file first, set permissions, format, and enable it
sudo swapoff /swap.img
sudo fallocate -l 4G /swap.img
sudo chmod 600 /swap.img
sudo mkswap /swap.img
sudo swapon /swap.img
Check the active size with swapon --show.
1 Like
Can we please confirm you are using default ext4 and not something like btrfs or xfs
Thanks
1 Like
Please change your topic title to
How to add secondary swap space as a swapfile?
The sequence you are looking for is the following, IF you want a swapFILE and not a swap partition:
swapfile="/path_to_swapfile"
swaplabel="SWAPFILE"
sudo fallocate -l 4G ${swapfile}
sudo chmod 600 ${swapfile}
sudo mkswap -L ${swaplabel} ${swapfile}
sudo swapon ${swapfile}
To make it permanent:
echo "\n\n# Manually created swapfile\n${swapfile} none swap sw 0 0" >>/etc/fstab
P.S. Sorry, I just saw that @steelf had already given the similar sequence with a hardcoded path.
1 Like
steelf
September 30, 2026, 5:43pm
5
No worries, now let’s see who gets the likes
1 Like