A swap file can be created while installing Linux operating system, if you forgot to do so you can read this tutorial and create one after installation. Even if you need additional swap memory this tutorial will be useful. I’ve only explained the command line way of creating a swap file which most administrators will use, its upto you to explore the GUI technique. For executing the commands in this tutorial you need to have root access or atleast sudo permissions for these commands and permissions to edit the fstab file.
Step 1: Create a dummy file for the required size
If you are using a hard disk partition skip this step. The first step is to create a dummy file of the required file size. The dd command will be used, a dummy file of the specified size will be created. Use the following command
dd if=/dev/zero of=/swapfile1 bs=1M count=1024
The above command will create a 1 GB file /swapfile1. The bs is the number of bytes and count is the number of times to create such bytes so 1M*1024=1GB. You can specify any location for the swap e.g. /root/swpfile, /swaps/swapfile1 Just ensure the directory exists before executing the above command
Step 2: Format it with filesystem swap
The dummy file must be formatted to filesystem type swap, use mkswap to do this execute any one of the following command
mkswap -L myswap1 /swapfile1
The second command labels the file with the name myswap1. If you are formatting a device specify the device name
mkswap -L swap1 /dev/sda4
Step 3: Add an entry to the /etc/fstab file
Adding an entry to the /etc/fstab file ensures the file is mounted as swap everytime the system boots. Open /etc/fstab with a text editor and add any one of the following lines
/swapfile1 swap swap defaults 0 0
/dev/sda4 swap swap defaults 0 0
save the file and exit.
Step 4: Check if the configuration is right and mount the swap file.
Use the swapon command to do this
If you see any errors check the /etc/fstab file and correct it. Make sure you reboot the system only AFTER all errors are rectified.
Leave a Reply