Differences

This shows you the differences between two versions of the page.

Link to this comparison view

guides:how-to-create-swap-file-in-linux [2016-08-31 17:35:35] (current)
rory.blanchard created
Line 1: Line 1:
 +====== How to Create a Swap File ======
  
 +On servers with low amounts of memory, it can be a good idea to create a swap file to prevent out of memory crashes. A swap file is a a designated space on your disk that can be used in addition to regular memory to give your system more, albeit much slower, memory. 
 +
 +<note>
 +A swap file should be the same size as your available physical memory. Having a very large swap file is not a good idea and if you are experiencing frequent crashing, you should expand your physical memory.
 +</note>
 +
 +===== Creating the swap file =====
 +
 +To create the swap file, log into the console through SSH.
 +
 +Once in, you will want to create the file that you would like to use with the following command:
 +
 +<note tip>
 +Change the count value to the size that you need. You can use the formula (<count>/1024) to determine how many megabytes you will be setting aside for swap. In this example, we are crafting a 64MB swap.
 +</note>
 +
 +<code>
 +dd if=/dev/zero of=/swapfile bs=1024 count=65535
 +</code>
 +
 +Once you've created the file, you need to change it's permissions to keep it secure.
 +
 +<code>
 +chmod 600 /swapfile
 +</code>
 +
 +Now that your swap file is secure, you can turn it into a usable swap file:
 +
 +<code>
 +mkswap /swapfile
 +</code>
 +
 +Now it's ready to be used, so let's turn it on:
 +
 +<code>
 +swapon /swapfile
 +</code>
 +
 +That's all there is to it!
 +
 +You can test to make sure that it was added by running the free -m command:
 +
 +<code>
 +# free -m
 +              total        used        free      shared  buff/cache   available
 +Mem:            983         343          12          51         627         561
 +Swap:            63                    63
 +</code>
 +
 +If you would like to have your swapfile always mounted on boot, you will need to add it inside of **/etc/fstab**. You can add the following line to mount your swap file on boot.
 +
 +<code>
 +/swapfile swap swap defaults 0 0
 +</code>
 +\\
 +\\