{"id":75,"date":"2021-09-15T14:17:00","date_gmt":"2021-09-15T14:17:00","guid":{"rendered":"https:\/\/bestdedicatedhosting.in\/blog\/?p=75"},"modified":"2021-11-02T13:12:20","modified_gmt":"2021-11-02T13:12:20","slug":"how-to-create-a-linux-swap-file","status":"publish","type":"post","link":"https:\/\/bestdedicatedhosting.in\/blog\/how-to-create-a-linux-swap-file\/","title":{"rendered":"How To Create a Linux Swap File"},"content":{"rendered":"\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Description <\/strong><\/p>\n\n\n\n<p>We will explain swap space, and learn how to create swap space using a swap file in Linux: this is important in case we don\u2019t have a swap partition created on the hard disk. <\/p>\n\n\n\n<p>Swap space\/partition is space on a disk created for use by the operating system when memory has been fully utilized. <\/p>\n\n\n\n<p>It can be used as virtual memory for the system; it can either be a partition or a file on a disk.<\/p>\n\n\n\n<p>When the kernel runs out of memory, it can move idle\/inactive processes into swap creating room for active processes in the working memory. <\/p>\n\n\n\n<p>This is memory management that involves swapping sections of memory to and from virtual memory.<\/p>\n\n\n\n<p>Below are the steps we can follow to create a swap space using a file.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>How to Create and Enable Swap in Linux <\/strong><\/p>\n\n\n\n<p> <strong>1.<\/strong>&nbsp;In this example, we will create a swap file of size&nbsp;<strong>2GB<\/strong>&nbsp;using the dd command as follows. <\/p>\n\n\n\n<p>Note that&nbsp;<strong>bs=1024<\/strong>&nbsp;means read and write up to&nbsp;<strong>1024<\/strong>&nbsp;bytes at a time and&nbsp;<strong>count = (1024 x 2048)MB<\/strong>&nbsp;size of the file. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>dd if=\/dev\/zero of=\/mnt\/swapfile bs=1024 count=2097152<\/em><\/strong><\/pre>\n\n\n\n<p>Alternatively, use the&nbsp;<strong>fallocate command<\/strong> as follows.<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>fallocate --length 2GiB \/mnt\/swapfile<\/em><\/strong><br><\/pre>\n\n\n\n<p>And then set the appropriate permissions on the file; make it readable only by root user as follows.<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>chmod 600 \/mnt\/swapfile<\/em><\/strong><br><\/pre>\n\n\n\n<p><strong>2.<\/strong>&nbsp;Now setup the file for swap space with the&nbsp;<strong>mkwap command<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>mkswap \/mnt\/swapfile<\/em><\/strong><\/pre>\n\n\n\n<p><strong>3.<\/strong>&nbsp;Next, enable the swap file and add it to the system as a swap file. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>swapon \/mnt\/swapfile<\/em><\/strong><br><\/pre>\n\n\n\n<p><strong>4.<\/strong>&nbsp;Afterwards, enable the swap file to be mounted at boot time. Edit the&nbsp;<strong>\/etc\/fstab<\/strong>&nbsp;file and add the following line in it. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>\/mnt\/swapfile swap swap defaults 0 0<\/em><\/strong><br><\/pre>\n\n\n\n<p>In the line above, each field means: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>\/mnt\/swapfile<\/strong>&nbsp;\u2013 device\/file name <\/li><li> <strong>swap<\/strong>&nbsp;\u2013 defines device mount point <\/li><li> <strong>swap<\/strong>&nbsp;\u2013 specifies the file-system type <\/li><li> <strong>defaults<\/strong>&nbsp;\u2013 describes the mount options <\/li><li> <strong>0<\/strong>&nbsp;\u2013 specifies the option to be used by the dump program <\/li><li> <strong>0<\/strong>&nbsp;\u2013 specifies the fsck command option <\/li><\/ul>\n\n\n\n<p><strong>5.<\/strong>&nbsp;To set how often the swap file can be used by the kernel, open the&nbsp;<strong>\/etc\/sysctl.conf<\/strong>&nbsp;file and add the line below. <\/p>\n\n\n\n<p>Note that the default value of how frequent swap space can be used is&nbsp;<strong>60<\/strong>&nbsp;(maximum value is&nbsp;<strong>100<\/strong>).<\/p>\n\n\n\n<p> The higher the number, the more frequent swap space utilization by the kernel. When the value is set to&nbsp;<strong>0<\/strong>, the swap file will only be used if the operating system has fully utilized memory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>vm.swappiness=10<\/em><\/strong><br><\/pre>\n\n\n\n<p><strong>6.<\/strong>&nbsp;Now verify the swap file was created using the&nbsp;<strong>swapon command<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">swapon  -s <br><br>OR<br>free <br><br>OR <br>cat  \/proc\/swaps <\/pre>\n\n\n\n<p>We can optionally reboot the system to effect the above changes using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>reboot<\/em><\/strong><\/pre>\n\n\n\n<p>We hope you\u2019ve found this useful!&nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description We will explain swap space, and learn how to create swap space using a swap file in Linux: this is important in case we don\u2019t have a swap partition created on the hard disk. Swap space\/partition is space on a disk created for use by the operating system when memory has been fully utilized. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-linux"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":1,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/76"}],"wp:attachment":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}