{"id":87,"date":"2022-03-28T21:00:53","date_gmt":"2022-03-29T01:00:53","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=87"},"modified":"2024-10-11T09:32:59","modified_gmt":"2024-10-11T13:32:59","slug":"create-a-linux-swap-file","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=87","title":{"rendered":"create a linux swap file"},"content":{"rendered":"\n<p><strong>How to add Swap File<\/strong><br>Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G.<br>\u00a0 \u00a0 Create a file that will be used for swap:<br>\u00a0 \u00a0 sudo fallocate -l 1G \/swapfile<br>\u00a0 \u00a0 If fallocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:<br>\u00a0 \u00a0 sudo dd if=\/dev\/zero of=\/swapfile bs=1024 count=1048576<br>\u00a0 \u00a0 Only the root user should be able to write and read the swap file. To set the correct permissions type:<br>\u00a0 \u00a0 sudo chmod 600 \/swapfile<br>\u00a0 \u00a0 Use the mkswap utility to set up the file as Linux swap area:<br>\u00a0 \u00a0 sudo mkswap \/swapfile<br>\u00a0 \u00a0 Enable the swap with the following command:<br>\u00a0 \u00a0 sudo swapon \/swapfile<br>\u00a0 \u00a0 To make the change permanent open the \/etc\/fstab file and append the following line:\u00a0 \u00a0 \/etc\/fstab<br>\u00a0 \u00a0 \/swapfile swap swap defaults 0 0<br>\u00a0 \u00a0 Copy<br>\u00a0 \u00a0 To verify that the swap is active, use either the swapon or the free command as shown below:<br>\u00a0 \u00a0 sudo swapon &#8211;show<br>\u00a0 \u00a0 NAME TYPE SIZE USED PRIO\u00a0 \u00a0 \/swapfile file 1024M 507.4M -1<br>\u00a0 \u00a0 sudo free -h<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 total used free shared buff\/cache available \u00a0 \u00a0 Mem: 488M 158M 83M 2.3M 246M 217M \u00a0 \u00a0 Swap: 1.0G 506M 517M<br><strong>How to adjust the swappiness value<\/strong><br>Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.<br>The default swappiness value is 60. You can check the current swappiness value by typing the following command:<br>cat \/proc\/sys\/vm\/swappiness<br>Output:60<br>While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.<br>For example, to set the swappiness value to 10, you would run:<br>sudo sysctl vm.swappiness=10<br>To make this parameter persistent across reboots append the following line to the \/etc\/sysctl.conf file:<br>\/etc\/sysctl.conf<br>vm.swappiness=10<br>The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.<br><strong>How to remove Swap File<\/strong><br>If for any reason you want to deactivate and remove the swap file, follow these steps:<br>\u00a0 \u00a0 First, deactivate the swap by typing:<br>\u00a0 \u00a0 sudo swapoff -v \/swapfile<br>\u00a0 \u00a0 Remove the swap file entry \/swapfile swap swap defaults 0 0 from the \/etc\/fstab file.<br>\u00a0 \u00a0 Finally, delete the actual swapfile file using the rm command:<br>\u00a0 \u00a0 sudo rm \/swapfile<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to add Swap FileFollow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G.\u00a0 \u00a0 Create a file that will be used for swap:\u00a0 \u00a0 sudo fallocate -l 1G \/swapfile\u00a0 \u00a0 If fallocate is not installed or if you get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-file-management"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":1,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}