Adding swap file to Linux

If there are no free partitions on a disk, you can create a swap file.   Here is how you add a 12GB file.

# Check what you have for swap.   We have one 4GB  swap partition.

server# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/vg_root-lv_swap             partition       4194296 0       -1

# Create a 12GB swap file

server# dd if=/dev/zero of=/swapfile bs=1024 count=$((12*1024))k

12582912+0 records in
12582912+0 records out
12884901888 bytes (13 GB) copied, 40.9898 seconds, 314 MB/s

# Check the size with ls.

server# ls -lh /swapfile
-rw-r–r– 1 root root 12G Jan 23 16:47 /swapfile

server# mkswap /swapfile
Setting up swapspace version 1, size = 12884897 kB

 

# Activate the swap file with a low priority, you don’t really want to use it.

server# swapon -p 9 /swapfile
server# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/vg_root-lv_swap             partition       4194296 0       -1
/swapfile              file            12582904        0       9

 

# If you want it to survive after reboot, at it to the /etc/fstab.

Leave a Reply

Your email address will not be published.