Create a SWAP File in Linux – Step by StepGOZEN HOST
Step by Step on How To add a Linux Centos Swapfile. How do I add a swap file to Linux system using command line options without creating a new partition?
With Linux Kernel 2.6 swap files are just as fast as swap partitions, although it’s recommended to use a swap partition. The administrative flexibility of swap files outweighs that of partitions since modern high capacity drives can remap physical sectors, that said no partition is guaranteed to be contiguous.
You can add a swap file as a dedicated partition, you can use the following instructions to create a swap file.
How to Add a Swap File in Linux (ssh)
In order to create the swap file, we will be using the dd command.
The mkswap command is used to set up a Linux swap area on a device or in a file.
Login as Root User and create a Storage File, in our example we are going to create a 512MB swap file ( 1024 x 512MB = 524288 block size).
dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
The output should be something like this
524288+0 records in 524288+0 records out 536870912 bytes (537 MB) copied, 2.08634 s, 257 MB/s
chown root:root /swapfile1 chmod 0600 /swapfile1
This ensures that our swap file is readable only by the root user and nobody else as this would be a security issue.
Set up a Linux swap area in a file by using the following command:
Our swap file is now ready to be used, we just need to turn it on. Execute the following command to turn on the swap.
Update the /etc/fstab
Use your favorite editor and edit the fstab like this nano /etc/fstab
Append the following line: /swapfile1 none swap sw 0 0
Save and close the file, in order for changes to take effect, it’s advised that you restart your box, once it’s up and running again you can verify swap is activated or not using the following command.
[firstname.lastname@example.org]# free -m total used free shared buff/cache available Mem: 7821 2577 157 415 5086 4488 Swap: 4095 43 .4052
Leave a Reply