Differences
This shows you the differences between two versions of the page.
— |
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. | ||
+ | |||
+ | < | ||
+ | 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. | ||
+ | </ | ||
+ | |||
+ | ===== 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 (< | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | dd if=/ | ||
+ | </ | ||
+ | |||
+ | Once you've created the file, you need to change it's permissions to keep it secure. | ||
+ | |||
+ | < | ||
+ | chmod 600 /swapfile | ||
+ | </ | ||
+ | |||
+ | Now that your swap file is secure, you can turn it into a usable swap file: | ||
+ | |||
+ | < | ||
+ | mkswap /swapfile | ||
+ | </ | ||
+ | |||
+ | Now it's ready to be used, so let's turn it on: | ||
+ | |||
+ | < | ||
+ | swapon /swapfile | ||
+ | </ | ||
+ | |||
+ | That's all there is to it! | ||
+ | |||
+ | You can test to make sure that it was added by running the free -m command: | ||
+ | |||
+ | < | ||
+ | # free -m | ||
+ | total used free shared | ||
+ | Mem: 983 | ||
+ | Swap: 63 | ||
+ | </ | ||
+ | |||
+ | If you would like to have your swapfile always mounted on boot, you will need to add it inside of **/ | ||
+ | |||
+ | < | ||
+ | /swapfile swap swap defaults 0 0 | ||
+ | </ | ||
+ | \\ | ||
+ | \\ |