{"id":103089,"date":"2020-11-02T11:00:49","date_gmt":"2020-11-02T08:00:49","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/"},"modified":"2020-11-02T11:00:49","modified_gmt":"2020-11-02T08:00:49","slug":"how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/","title":{"rendered":"#How to Create and Enable a Swapfile At the Linux Command Line \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a288b158906a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a288b158906a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#What_is_a_Swapfile\" >What is a Swapfile?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#Swapfile_Advantages_and_Disadvantages\" >Swapfile Advantages and Disadvantages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#Some_Swapfile_Considerations\" >Some Swapfile Considerations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#How_to_Enable_a_Swapfile\" >How to Enable a Swapfile<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#Enabling_Our_New_Swap_at_Boot_Time\" >Enabling Our New Swap at Boot Time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line-cloudsavvy-it\/#Removing_Our_New_Swap_Space\" >Removing Our New Swap Space<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Create and Enable a Swapfile At the Linux Command Line \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure id=\"attachment_7741\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7741 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/66ad8d8aac89dc205a726f08f978213f\/p\/uploads\/2020\/10\/0f230d7a.png\" alt=\"\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/file-document-folder-transfer-icon-logo-1681804801\" data-credittext=\"Shutterstock\/ Mbok Menawa\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/file-document-folder-transfer-icon-logo-1681804801\">Shutterstock\/ Mbok Menawa<\/a><\/span><\/figcaption><\/figure>\n<p>Learning what a sawpfile is and does, you will discover how to create and enable one on your Linux instance. Armed with this knowledge, you will be able to ensure your system does not run out of memory again.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_a_Swapfile\"><\/span>What is a Swapfile?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us start by a rhetorical question: what h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ens when your computer runs out of memory?<\/p>\n<p>In Linux, a special monitoring process \u2013 the OOM monitoring\/killer process \u2013 will monitor whether a system is running out of memory, unless the OOM killer was disabled or configured differently.<\/p>\n<p>When that happens, this process will terminate one of the processes running on the system in order to free memory. This can lead to painful results like data corruption or unavailable services. The acronym OOM stands for \u201cOut of Memory\u201d<\/p>\n<p>One solution is to buy additional memory or to reduce the number of programs or services running simultaneously on the computer or server. But how about if we could use some our (usually cheap) disk space as \u201cextra memory\u201d?<\/p>\n<p>That is exactly what a swapfile is and does: a special file created on your storage device which then becomes part of the main memory. Not directly, and there is processing overhead, but indirectly and with considerable processing speed impact.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Swapfile_Advantages_and_Disadvantages\"><\/span>Swapfile Advantages and Disadvantages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Main memory is usually ultra fast when compared with slower disks. Even SSD\u2019s (solid state drives) are almost always slower then high-speed main memory chips. For HDD\u2019s (mechanical, spinning, traditional hard disk drives) the difference in speed is much bigger still.<\/p>\n<p>It is therefore a distinct advantage, in most setups, to use a slower disk (SDD and especially HDD) as a source of memory. The delay of writing data back and forth between main memory and disk (i.e. using the disk as memory) is called \u2018swapping\u2019 (swapping out memory with disk space)<\/p>\n<p>That said, it is always a good idea to have a reasonably sides swap file on your system. The reason is simple; let\u2019s say that we only require 1 extra Gigabyte of memory for a period of a few minutes to ensure our system does not invoke the OOM killer and terminates some processes. A swapfile is better in this case because even if a process runs out of usable RAM, it won\u2019t crash, just run slower for a bit until it finishes its task.<\/p>\n<p>However, if the system starts swapping heavily, it will almost definitely become slower (read: much slower). This will be very noticeable.<\/p>\n<p>In summary, there is a fine and delicate balance, which is somewhat arbitrary and depends on many different things within a given situation; the number of processes running on a system and their memory working sizes, the total memory in use, the memory available in the swap file, the specific swap file configuration of the system etc.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Some_Swapfile_Considerations\"><\/span>Some Swapfile Considerations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>On many Linux setups in the world today, there is a swap partition assigned on one of the disks within the system. This is a special area, usually (but not exclusively; it can be added later) reserved during the installation of the Linux operating system. It has the same the target functionality is the same as that of a swapfile. To check if your system has some form of swap currently enabled, simply execute the following in your terminal:<\/p>\n<pre><code>sudo apt -y install htop&#13;\nhtop&#13;\n<\/code><\/pre>\n<p>And locate the following output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7729\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a39c548e7b44a51793eaa8f85c08b6f6\/p\/uploads\/2020\/10\/7ac61a78.png\" alt=\"htop output of main memory and swap space\" width=\"612\" height=\"38\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Notice how on this system, there is 62.7G main memory, and 11.0G swap space. You can also see how only a small amount (23.5M) is currently in use. If you suspect your system is swapping, check how these two counters look.<\/p>\n<p>If they are both near-full, your system is highly likely swapping heavily and will run significantly slower. The reason is that now there is a heavy I\/O bound (i.e. disk bound) load, constantly swapping main memory with disk based memory.<\/p>\n<p>As a rule of thumb, some people prefer to use a certain percentage of system memory as the size to be allocated to a swap file. I personally take another route, and that is to consider how much additional memory I may need. If the size is considerable, for example a server with 8GB will likely end up using 14GB for a specific use case, then it makes much more sense to raise the server memory from 8GB to 16GB.<\/p>\n<p>However, if the system has 8GB and the usual use is 6-7GB with an occasional spike if I start too many programs at once, I may consider a 8GB additional swap space, or slightly more. Disk space is usually quite cheap, so over sizing it a little, provided you have the space, won\u2019t hurt.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Enable_a_Swapfile\"><\/span>How to Enable a Swapfile<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Ready to add a swapfile? This can be done on the fly. This method will work on both Debian (Ubuntu, Mint, \u2026) as well as RedHat (RedHat, Centos, \u2026) based systems.<\/p>\n<p>We will be creating a 1 Gigabyte, or 1000 Megabyte, or 1024000 Blocks (calculated as the number of Megabytes x 1024) swapfile in this example.<\/p>\n<p>We will name it swapfile123, located in the root (\/) directory, and we have given it that name to avoid overwriting any file you may already have in place. If you are confident you do not have any file named \/swapfile, you may use that instead if you like, taking care to change the filename at all places correctly.<\/p>\n<p>Please make sure to type any commands, and especially the <code>dd<\/code> command, carefully in order not to overwrite any data on your system. We use <code>sudo su<\/code> to enter superuser (su) mode via <code>sudo<\/code>. It is much the same as executing each command using <code>sudo<\/code>, though it is easier not to type each sudo command prefix by using this way.<\/p>\n<pre><code>sudo su&#13;\nif [ -r \/swapfile123 ]; then echo \"Stop please! \/swapfile123 already exists!\"; fi&#13;\ndd if=\/dev\/zero of=\/swapfile123 bs=1024 count=1024000&#13;\nchmod 0600 \/swapfile123&#13;\nmkswap \/swapfile123&#13;\nswapon \/swapfile123&#13;\n<\/code><\/pre>\n<p>You should see output similar to the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7730\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ac8688ebfdbf250d316dae7c5400073a\/p\/uploads\/2020\/10\/767b3fc9.png\" alt=\"Enabling swap files on Linux\" width=\"642\" height=\"182\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If so, congratulations! You have just enabled your first swapfile, dynamically (on the fly), at the Linux command line!<\/p>\n<p>First we used the <code>dd<\/code> tool to create our swapspace, using the <code>\/dev\/zero<\/code> device (a virtual device which just outputs zero\u2019s when called, by <code>dd<\/code> or any other tool). Next we set some appropriate security privileges.<\/p>\n<p>We next indicated to the operating system that the file was to be made a swapfile using the <code>mkswap<\/code> command. Finally we turned on the swapspace, dynamically at the command line, without having to restart our system.<\/p>\n<p>We can also easily check if our swap space has become larger by using <code>free -m<\/code>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7731\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/0c2483e31faa228c17db3e09c7cbaeab\/p\/uploads\/2020\/10\/10f6b794.png\" alt=\"free -m showing how swap space decreases when we deactivate it\" width=\"759\" height=\"181\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Great, you can see how we went from 12215M to 11215M (-1000M as expected) when we deactivated the newly created swapfile. You can also see how we used <code>swapoff<\/code> to dynamically turn off swap at the command line.<\/p>\n<p>It is nice to know that if we ever see our system getting into memory issues, and we can type fast enough, we are able to give it extra memory on the fly ? In practicality, it is better to do this upfront before issues occur.<\/p>\n<p>We now only have one small issue to address. If we were to reboot at this time, whilst our swapfile would remain on the disk, the system would not be using it. No setting change was made so far which will ensure that the swapspace is reloaded on system reboot.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Enabling_Our_New_Swap_at_Boot_Time\"><\/span>Enabling Our New Swap at Boot Time<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To enable the new swap space at boot time, simply add the following line (as the line) to \/etc\/fstab:<\/p>\n<pre><code>\/swapfile123  swap  swap  defaults  0  0<\/code><\/pre>\n<p>You can do so while still in <code>sudo su<\/code> mode (as indicated by the leading <code>#<\/code>) by starting the nano editor for \/etc\/fstab:<\/p>\n<pre><code>nano \/etc\/fstab&#13;\ncat \/etc\/fstab&#13;\n<\/code><\/pre>\n<p>Then simply add the line above to the file at the end and save (CTRL+X &gt; Y &gt; enter). Do not modify or delete any other lines as this may result in your system not booting correctly anymore. Also make sure to double check the contents of your \/etc\/fstab file before restarting by executing the <code>cat \/etc\/fstab<\/code> above.<\/p>\n<p>You can now reboot your system and confirm that your extra swap space is still available by using <code>free -m<\/code> again.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Removing_Our_New_Swap_Space\"><\/span>Removing Our New Swap Space<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you want to remove the new swap space, or perhaps create a somewhat larger one, you can do this by editing the \/etc\/fstab file again first, and removing the line which loads the swap file in full. Do not modify or delete any other lines as this may result in your system not booting correctly anymore! You can then execute:<\/p>\n<pre><code>sudo swapoff \/swapfile123&#13;\nrm \/swapfile123&#13;\n<\/code><\/pre>\n<p>All done! No need to reboot either, as this change was made dynamically. On the next reboot, the swapfile will not be activated anymore as we removed the matching line from the \/etc\/fstab file.<\/p>\n<p><strong>Enjoy Swapfiles!<\/strong>\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7728\/how-to-create-and-enable-a-swapfile-at-the-linux-command-line\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Create and Enable a Swapfile At the Linux Command Line \u2013 CloudSavvy IT&#8221; Shutterstock\/ Mbok Menawa Learning what a sawpfile is and does, you will discover how to create and enable one on your Linux instance. Armed with this knowledge, you will be able to ensure your system does not run out of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":103090,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/0f230d7a.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-103089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/103089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=103089"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/103089\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/103090"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=103089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=103089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=103089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}