{"id":2464,"date":"2016-08-08T13:13:45","date_gmt":"2016-08-08T17:13:45","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=2464"},"modified":"2017-12-12T15:52:52","modified_gmt":"2017-12-12T20:52:52","slug":"swap-file-centos-7","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/","title":{"rendered":"How to Create a Swap File on CentOS 7"},"content":{"rendered":"<h1>How to Create a Swap File on CentOS 7<\/h1>\n<p>Swap files allow an operating system to free physical memory on your server by temporarily storing used memory to a hard drive. This can often increase performance, especially when your server is running a memory-intensive operation. For these reasons, it may be a good idea to create a swap file on your server. This guide demonstrates how to add more swap space to your server or create a new swap file if none exist.<\/p>\n<h2>Getting started<\/h2>\n<p>Confirm that you have the following before you follow this guide:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\">Cloud Server<\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\">Dedicated Server<\/a>) running CentOS 7.<\/p>\n<p>No special tools are needed to manage swap files on your CentOS system. All of the tools we are going to use are included by default on nearly every Linux system.<\/p>\n<h2>Tutorial<\/h2>\n<p><strong>Check your current swap configuration<\/strong><\/p>\n<p>First, check if your system already has a swap space configured. You can check using the \u201cswapon\u201d command:<\/p>\n<p><code>swapon -s<\/code><\/p>\n<p>If the command returns nothing, then you do not currently have any configured swap space. Otherwise it will return similar to the sample below. Each swap space will have a name and a size associated with it.<\/p>\n<p>Output Sample:<\/p>\n<p><code class=\"gris\">swapon -s<br \/>\nFilename        Type            Size    Used    Priority<br \/>\n\/dev\/sda2       partition       8388604 9820    -1<\/code><\/p>\n<p><strong>Creating and enabling a swap file<\/strong><\/p>\n<p>If your system doesn\u2019t currently have a swap file, or if you would like to add more swap to your system, the following steps will allow you to create one.<\/p>\n<p>First you must ensure that your system has available space to create a new swap file. To do so, use the \u201cdf\u201d command. The \u201c-h\u201d argument is used so that it will output human-readable sizes rather than only bytes.<\/p>\n<p><code>df -h<\/code><\/p>\n<p>Output Sample:<\/p>\n<p><code class=\"gris\">df -h<br \/>\nFilesystem      Size  Used Avail Use% Mounted on<br \/>\n\/dev\/sda3       909G   21G  842G   3% \/<br \/>\ntmpfs           3.8G     0  3.8G   0% \/dev\/shm<br \/>\n\/dev\/sda1       488M   62M  401M  14% \/boot<\/code><\/p>\n<p>Next choose the partition that you want to store your new swap file on. Make sure that the partition has enough space, as the swap file will often be several gigabytes in size. One you have chosen, create the new swap file using the \u201cfallocate\u201d command. In our sample command, \u201c16G\u201d means that it will create a swap file that is 16 gigabytes in size. This file will be placed into the root directory (\/) with the name \u201cswap16\u201d.<\/p>\n<p><code>fallocate -l 16G \/swap16<\/code><\/p>\n<p>If you system have more than one disk, you may put the swapfile on a disk that have low Input\/Output usage. SWAP on a primary disc may slowdown a lot your system in case the Input\/Output usage is already high.<\/p>\n<p>It is very important that you set the permissions on the new swap file so that it can only be accessed by the root user. This can be done as follows:<\/p>\n<p><code>chmod 600 \/swap16<\/code><\/p>\n<p>Now verify that the file was successfully created, with the correct permissions and size:<\/p>\n<p><code>ls -lh \/swap16<\/code><\/p>\n<p>Output Sample:<\/p>\n<p><code class=\"gris\">ls -lh \/swap16<br \/>\n-rw------- 1 root root 16G Jul 29 12:32 \/swap16<\/code><\/p>\n<p>If the file has the correct permissions and size, you can now create the new swap space using the file you just created:<\/p>\n<p><code>mkswap \/swap16<\/code><\/p>\n<p>The last step is to mount the swap space to your system so that it can be used:<\/p>\n<p><code>swapon \/swap16<\/code><\/p>\n<p>To verify that your new swap space has been correctly mounted to your system, use the \u201cswapon\u201d command, as we did previously:<\/p>\n<p><code>swapon -s<\/code><\/p>\n<p>Output Sample:<\/p>\n<p><code class=\"gris\">swapon -s<br \/>\nFilename          Type            Size    Used    Priority<br \/>\n\/dev\/sda2         partition       8388604 9820    -1<br \/>\n\/swap16           file            16777212        0       -2<\/code><\/p>\n<p>If \u201cswapon\u201d now shows your new swap file, then it has been successfully mounted and is now in use by your system.<\/p>\n<p><strong>Make your swap file permanent<\/strong><\/p>\n<p>Normally you will want to make your swap file permanent, so that will persist even after your system has been rebooted. To make your new swap file permanent, you will need to add it to the fstab (file systems table) file so that it will be automatically mounted when your system boots.<\/p>\n<p>Open the fstab file using your favorite file editor:<\/p>\n<p><code>nano \/etc\/fstab<\/code><\/p>\n<p>Now add the following line at the end of the fstab. Be careful to not modify the other lines in the file, as they are used to mount your other system partitions.<\/p>\n<p><code>\/swap16   swap    swap    sw  0   0<\/code><\/p>\n<p>Once you\u2019re done, save the file and exit. Your system will now automatically mount the new swap file when it boots.<\/p>\n<h2>Conclusion<\/h2>\n<p>You&#8217;ve now successfully created a swap file, added it to your system, and configured it to be mounted on boot. No more work needs to be done in the future to use your swap file; your system will handle it automatically. If you ever need to modify the amount of swap space on your system, you can create new swap files as we did here and add\/remove them from fstab as needed.<\/p>\n<p><em>If you found this article helpful, feel free to share it with your friends and let us know in the comments below!<\/em><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>How to Create a Swap File on CentOS 7 Swap files allow an operating system to free physical memory on your server by temporarily storing used memory to a hard drive. This can often increase performance, especially when your server is running a memory-intensive operation. For these reasons, it may be a good idea to<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-2464","post","type-post","status-publish","format-standard","hentry","category-ha-clustering","operating_system-centos-7"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Create a Swap File on CentOS 7 - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now &amp; free physical memory on your server.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create a Swap File on CentOS 7 - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now &amp; free physical memory on your server.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-08T17:13:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-12T20:52:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/\",\"name\":\"How to Create a Swap File on CentOS 7 - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\"},\"datePublished\":\"2016-08-08T17:13:45+00:00\",\"dateModified\":\"2017-12-12T20:52:52+00:00\",\"author\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now & free physical memory on your server.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.globo.tech\/learning-center\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create a Swap File on CentOS 7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\/\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create a Swap File on CentOS 7 - Globo.Tech","description":"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now & free physical memory on your server.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/","og_locale":"en_US","og_type":"article","og_title":"How to Create a Swap File on CentOS 7 - Globo.Tech","og_description":"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now & free physical memory on your server.","og_url":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/","og_site_name":"Globo.Tech","article_published_time":"2016-08-08T17:13:45+00:00","article_modified_time":"2017-12-12T20:52:52+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/","url":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/","name":"How to Create a Swap File on CentOS 7 - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-08-08T17:13:45+00:00","dateModified":"2017-12-12T20:52:52+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will show you how to create a Swap file on your CentOS 7 server. Read now & free physical memory on your server.","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/swap-file-centos-7\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"How to Create a Swap File on CentOS 7"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=2464"}],"version-history":[{"count":7,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2464\/revisions"}],"predecessor-version":[{"id":3998,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2464\/revisions\/3998"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=2464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=2464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}