{"id":1348,"date":"2019-01-14T15:27:25","date_gmt":"2019-01-14T09:57:25","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1348"},"modified":"2019-04-27T07:26:40","modified_gmt":"2019-04-27T01:56:40","slug":"how-to-create-a-linux-swap-file","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-create-a-linux-swap-file\/","title":{"rendered":"How To Create a Linux Swap File"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>Description <\/strong><\/p>\n\n\n\n<p>In this article, 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<ol class=\"wp-block-list\"><li>In this example, we will create a swap file of size 2GB using the dd command as follows. <\/li><\/ol>\n\n\n\n<p>Note that bs=1024 means read and write up to 1024 bytes at a time and count = (1024 x 2048)MB size of the file.<\/p>\n\n\n\n<p><em><strong>dd if=\/dev\/zero of=\/mnt\/swapfile bs=1024 count=2097152<\/strong><\/em><\/p>\n\n\n\n<p>Alternatively, use the fallocate command as follows.<\/p>\n\n\n\n<p><em><strong>fallocate &#8211;length 2GiB \/mnt\/swapfile<\/strong><\/em><\/p>\n\n\n\n<p>And then set the appropriate permissions on the file; make it readable only by root user as follows.<\/p>\n\n\n\n<p><em><strong>chmod 600 \/mnt\/swapfile<\/strong><\/em><\/p>\n\n\n\n<p># Now setup the file for swap space with the mkwap command.<\/p>\n\n\n\n<p><em><strong>mkswap \/mnt\/swapfi<\/strong><\/em><\/p>\n\n\n\n<p># Next, enable the swap file and add it to the system as a swap file.<\/p>\n\n\n\n<p><em><strong>swapon \/mnt\/swapfile<\/strong><\/em><\/p>\n\n\n\n<p># Afterwards, enable the swap file to be mounted at boot time. Edit the \/etc\/fstab file and add the following line in it.<\/p>\n\n\n\n<p><em><strong>\/mnt\/swapfile swap swap defaults 0 0<\/strong><\/em><\/p>\n\n\n\n<p>In the line above, each field means:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\/mnt\/swapfile \u2013 device\/file name<\/li><li>swap \u2013 defines device mount point<\/li><li>swap \u2013 specifies the file-system type<\/li><li>defaults \u2013 describes the mount options<\/li><li>0 \u2013 specifies the option to be used by the dump program<\/li><li>0 \u2013 specifies the fsck command option<\/li><\/ul>\n\n\n\n<p># To set how often the swap file can be used by the kernel, open the \/etc\/sysctl.conf 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 60 (maximum value is 100). <\/p>\n\n\n\n<p>The higher the number, the more frequent swap space utilization by the kernel. <\/p>\n\n\n\n<p>When the value is set to 0, the swap file will only be used if the operating system has fully utilized memory.<\/p>\n\n\n\n<p><em><strong>vm.swappiness=10<\/strong><\/em><\/p>\n\n\n\n<p># Now verify the swap file was created using the swapon command.<\/p>\n\n\n\n<p><em><strong>swapon  -s<br> OR<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>free<br>OR<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>cat  \/proc\/swaps<\/strong><\/em><\/p>\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<p><em><strong>reboot<\/strong><\/em><\/p>\n\n\n\n<p>We hope you\u2019ve found this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description In this article, 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1349,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1348","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1348"}],"version-history":[{"count":1,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":1350,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1348\/revisions\/1350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1349"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}