{"id":2749,"date":"2019-09-27T19:00:39","date_gmt":"2019-09-27T23:00:39","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=2749"},"modified":"2019-09-27T19:22:29","modified_gmt":"2019-09-27T23:22:29","slug":"add-a-swap-file-on-centos-7","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2019\/09\/27\/add-a-swap-file-on-centos-7\/","title":{"rendered":"Add a Swap file on CentOS 7"},"content":{"rendered":"<p><strong>Step 1: Open a terminal window<\/strong><\/p>\n<p># su &#8211;\u00a0 or # sudo -s<\/p>\n<p><strong>Step 2: Creation of the Storage File for the new swap file<\/strong><\/p>\n<p>Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size):<\/p>\n<p># dd if=\/dev\/zero of=\/swap bs=1024 count=524288<\/p>\n<p>If you don&#8217;t want to have to reboot use the following commands:<br \/>\n(This is useful for debugging automout issue)<\/p>\n<pre>524288+0 records in\r\n524288+0 records out\r\n536870912 bytes (537 MB) copied, 3.23347 s, 354 MB\/s<\/pre>\n<p>1. if=\/dev\/zero : Read from \/dev\/zero file. \/dev\/zero is a special file in that provides as many null characters to build storage file called \/swap.<br \/>\n2. of=\/swap : Read from \/dev\/zero write storage file to \/swapf.<br \/>\n3. bs=1024 : Read and write 1024 BYTES bytes at a time.<br \/>\n4. Count=524288 : Copy only 523288 BLOCKS input blocks.<\/p>\n<p><strong>Step 3 : Secure the new created swap file<\/strong><\/p>\n<p>Setup the correct file permission:<\/p>\n<p># chown root:root \/swap<br \/>\n# chmod 0600 \/swap<\/p>\n<p>A world-readable swap file is a <strong>huge local vulnerability.<\/strong><\/p>\n<p><strong>Step 4: Set up a Centos 7 swap area<\/strong><\/p>\n<p>Type the following command to set up a Linux swap area in a file:<\/p>\n<p># mkswap \/swap<\/p>\n<p>Sample outputs:<\/p>\n<p>Setting up swapspace version 1, size = 524284 KiB<br \/>\nno label, UUID=0e5e7c60-bbba-4089-a76c-2bb29c0f0839<\/p>\n<p><strong>Step 5: Enabling the swap file<\/strong><\/p>\n<p>Finally, activate \/swap swap space immediately, enter:<\/p>\n<p># swapon \/swapfile1<\/p>\n<p><strong>Step 6: Update \/etc\/fstab file<\/strong><\/p>\n<p>To activate \/swap after CentOS 7 system reboot, add entry to \/etc\/fstab file. Open this file using a text editor such as nano:<\/p>\n<p># nano \/etc\/fstab<\/p>\n<p>Copy &amp; Paste the following line:<\/p>\n<p>\/swap none swap sw 0 0<\/p>\n<p>Save and close the file.<\/p>\n<p>Verify if your swap file is activated?<\/p>\n<p># free -m<\/p>\n<pre>total       used       free     shared    buffers     cached\r\nMem:          1876       1798         77          0        119       1440\r\n-\/+ buffers\/cache:        237       1638\r\n<strong>Swap:         4607          0       4607<\/strong><\/pre>\n<p>Display swap usage summary<\/p>\n<p># swapon -s<\/p>\n<pre>Filename\t\t\t\tType\t\tSize\tUsed\tPriority\r\n\/dev\/sda6                               partition\t4194296\t0\t0\r\n<strong>\/swap                                   file\t\t524280\t0\t0<\/strong><\/pre>\n<p>Another option is to view \/proc\/meminfo file:<\/p>\n<p># less \/proc\/meminfo<br \/>\n# grep -i &#8211;color swap \/proc\/meminfo<\/p>\n<pre>SwapCached:        30748 kB\r\nSwapTotal:       6291448 kB\r\nSwapFree:        6154008 kB<\/pre>\n<p>Optional, test the new swap file:<\/p>\n<p># swapoff -a<br \/>\n# swapon -a<\/p>\n<p><em><strong>Enjoy!<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Open a terminal window # su &#8211;\u00a0 or # sudo -s Step 2: Creation of the Storage File for the new swap file Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size): # dd if=\/dev\/zero of=\/swap bs=1024 count=524288 If you don&#8217;t want to have to reboot &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2019\/09\/27\/add-a-swap-file-on-centos-7\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Add a Swap file on CentOS 7<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2749","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=2749"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2749\/revisions"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=2749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=2749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=2749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}