Extend Memory with Swap - How to create swap file in linux

This article is part 2 of the series Not Enough Memory? Extend Memory with Swap

Adding swap

Suppose we want to add 750M of memory. Invoke the following commands:


The following command create a new file /var/swap.1 and copying 750 blocks from /dev/zero - Each block has size of 1MB (1M Bytes). Thus, this command creates a 750MB file of zeros.

Step 1 - Create file of zeros
 main@work:~# sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=750
 750+0 records in
 750+0 records out
 786432000 bytes (786 MB, 750 MiB) copied, 1.57251 s, 500 MB/s


As this file will be used as swap, for security reason, we need to change the permissions so only the root user can read and write to it.

Step 2 - Update permissions
 main@work:~# sudo chmod 600 /var/swap.1


Now , will set up a Linux swap area on our new device (The newlly created file) and enable it so the system can start using it.

Step 3 - Set up a Linux swap area from the file
 main@work:~# sudo /sbin/mkswap /var/swap.1
 Setting up swapspace version 1, size = 750 MiB (786427904 bytes)
 no label, UUID=a6de..
 main@work:~# sudo /sbin/swapon /var/swap.1