{"id":172,"date":"2024-03-02T13:26:33","date_gmt":"2024-03-02T13:26:33","guid":{"rendered":"https:\/\/blog.devops955.com\/swain\/?p=172"},"modified":"2024-03-06T13:22:22","modified_gmt":"2024-03-06T13:22:22","slug":"ubuntu-20-04-enable-swap-partition","status":"publish","type":"post","link":"https:\/\/blog.devops955.com\/swain\/2024\/03\/02\/ubuntu-20-04-enable-swap-partition\/","title":{"rendered":"How to Enable SWAP Partition on Ubuntu 20.04"},"content":{"rendered":"<blockquote>\n<p>Estimated reading time: 5 min<\/p>\n<\/blockquote>\n<h1>What is SWAP Partition<\/h1>\n<blockquote>\n<p>The purpose of writing this note is because the blog I recently set up crashed while previewing the theme ...<\/p>\n<\/blockquote>\n<p>Firstly, we know that the Linux kernel caches files in memory to improve read and write efficiency and speed. This portion of memory is called Cache Memory.<\/p>\n<p><strong>Swap<\/strong>, also known as swap space, is like virtual memory. To cope with applications that require a large amount of memory, the system uses a portion of the disk as memory. When physical memory is insufficient, some data needs to be swapped to the swap space.<\/p>\n<p>However, it's important to note that swap is slower in read and write speed compared to physical memory. The general recommendation is to have the swap space twice the size of physical memory (if physical memory is large enough, swap doesn't need to be enabled).<\/p>\n<h1>Steps to Enable<\/h1>\n<blockquote>\n<p>If you selected the SWAP partition during the installation process, you can skip this note.<\/p>\n<\/blockquote>\n<p>Because the cloud server I used didn't have SWAP enabled when created, I need to manually set up and enable the partition to avoid various issues due to insufficient memory.<\/p>\n<ol>\n<li>\n<p>Create a swap file<br \/>\nYou can use either <strong>fallocate<\/strong> or <strong>dd<\/strong> command to accomplish this:<\/p>\n<pre><code class=\"language-bash\">sudo fallocate -l 2G \/swap<\/code><\/pre>\n<pre><code class=\"language-bash\">sudo dd if=\/dev\/zero of=\/swap bs=1M count=2048<\/code><\/pre>\n<\/li>\n<li>\n<p>Set read and write permissions<br \/>\nFor security reasons, the swap file should only be readable and writable by the root user. So, modify the permissions:<\/p>\n<pre><code class=\"language-bash\">sudo chmod 600 \/swap<\/code><\/pre>\n<\/li>\n<li>\n<p>Make the swap file a swap partition<br \/>\nAfter creating the file, linux still don't know to use it as swap space, so it needs to be configured:<\/p>\n<pre><code class=\"language-bash\">sudo mkswap \/swap<\/code><\/pre>\n<p>After setting up, enable the SWAP partition:<\/p>\n<pre><code class=\"language-bash\">sudo swapon \/swap<\/code><\/pre>\n<\/li>\n<li>\n<p>Adjust the tendency to use SWAP according to requirements<br \/>\nThe system's Swappiness parameter controls the tendency to use swap space. The value ranges from <strong>0 to 100<\/strong>, with lower values making the kernel prefer physical memory and higher values making the kernel prefer swap space.<br \/>\nAdjust this according to your needs, default is 60:<\/p>\n<pre><code class=\"language-bash\">sudo sysctl vm.swappiness=20<\/code><\/pre>\n<p>Add to \/etc\/sysctl.conf file to ensure permanent effect:<\/p>\n<pre><code class=\"language-bash\">echo &#039;vm.swappiness=20&#039; | sudo tee -a \/etc\/sysctl.conf<\/code><\/pre>\n<\/li>\n<li>\n<p>Mount SWAP partition permanently:<br \/>\nBy default, swap is not automatically enabled when the system restarts. Edit the \/etc\/fstab file to make it permanent (can also be done with vim):<\/p>\n<pre><code class=\"language-bash\">echo &#039;\/swap none swap sw 0 0&#039; | sudo tee -a \/etc\/fstab<\/code><\/pre>\n<\/li>\n<\/ol>\n<h1>Configuration Completed<\/h1>\n<p>With these steps, you should have successfully set up a 2GB swap partition on Ubuntu 20.04 system. When you use the <code>free -h<\/code> command, you should see that the swap partition has been enabled.<\/p>\n<p>When there's resource pressure and memory usage is under stress, enabling swap can help alleviate the pressure and ensure the system runs smoothly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Estimated reading time: 5 min What is SWAP Partition The purpose of writing this note is because the blog I recently set up crashed while previewing the theme &#8230; Firstly, we know that the Linux kernel caches files in memory to improve read and write efficiency and speed. This portion of memory is called Cache&#8230;<\/p>\n","protected":false},"author":3,"featured_media":169,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.devops955.com\/swain\/wp-content\/uploads\/sites\/2\/2024\/03\/Ubuntu-1404-.jpg","_links":{"self":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/172"}],"collection":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":6,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/172\/revisions\/216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/media\/169"}],"wp:attachment":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}