Ubuntu: Add a new swap file

create the new file with your desired size (usually swap file is as big as RAM)
Attention: dd count parameter means number of blocks (thus 4GB = 4 million blocks with blocksize 1K)


SWAPFILE=/home/swap.file
SWAPMEGABYTES=4096

#create the new file
BLOCKCOUNT=$(($SWAPMEGABYTES * 1024))
dd if=/dev/zero of=${SWAPFILE} bs=1024 count=${BLOCKCOUNT}

#format the file with swap file system:
mkswap ${SWAPFILE}

add the new file with type swap to fstab:
vi /etc/fstab

add new entry for swap file:
/path/to/swap.file       none            swap    sw              0       0

remount fstab:
mount -a

activate swap for all entries in fstab:
swapon -a

show if everything worked well and swap space has changed:
free

Comments are closed.