{"id":645,"date":"2013-08-12T12:26:42","date_gmt":"2013-08-12T11:26:42","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=645"},"modified":"2013-08-12T12:26:42","modified_gmt":"2013-08-12T11:26:42","slug":"how-to-add-swap-on-ubuntu","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=645","title":{"rendered":"How to Add Swap on Ubuntu"},"content":{"rendered":"<p>Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a \u201clinux swap\u201d can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.<\/p>\n<p>However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.<!--more--><\/p>\n<h2>Check for Swap Space<\/h2>\n<p>Before we proceed to set up a swap file, we need to check if any swap files have been enabled on the VPS by looking at the summary of swap usage.<\/p>\n<pre>sudo swapon -s<\/pre>\n<p>An empty list will confirm that you have no swap files enabled:<\/p>\n<pre>Filename\t\t\t\tType\t\tSize\tUsed\tPriority<\/pre>\n<h2>Check the File System<\/h2>\n<p>After we know that we do not have a swap file enabled on the virtual server, we can check how much space we have on the server with the\u00a0<code>df<\/code>\u00a0command. The swap file will take 512MB and since we are only using up about 8% of the \/dev\/sda, we can proceed.<\/p>\n<pre>df\r\nFilesystem     1K-blocks    Used Available Use% Mounted on\r\n\/dev\/sda        20907056 1437188  18421292   8% \/\r\nudev              121588       4    121584   1% \/dev\r\ntmpfs              49752     208     49544   1% \/run\r\nnone                5120       0      5120   0% \/run\/lock\r\nnone              124372       0    124372   0% \/run\/shm<\/pre>\n<h2>Create and Enable the Swap File<\/h2>\n<p>Now it\u2019s time to create the swap file itself using the dd command :<\/p>\n<pre>sudo dd if=\/dev\/zero of=\/mnt\/swap bs=1024 count=512k<\/pre>\n<ul>\n<li><em>if=\/dev\/zero\u00a0<\/em>means input file. In this case it&#8217;s linux zero-generator<\/li>\n<li><em>of=\/mnt\/swap<\/em>\u00a0specifies output file name. In this case the name is \/mnt\/swap.<\/li>\n<li><em>bs <\/em>means the block size allocated in bytes (1024b)<\/li>\n<li><em>count\u00a0<\/em>is the number how many blocks of <em>bs <\/em>size will be allocated (512 000 * 1024b)<\/li>\n<\/ul>\n<p>Subsequently we are going to prepare the swap file by creating a linux swap area:<\/p>\n<pre>sudo mkswap \/mnt\/swap<\/pre>\n<p>The results display:<\/p>\n<pre>Setting up swapspace version 1, size = 262140 KiB\r\nno label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb<\/pre>\n<p>Finish up by activating the swap file:<\/p>\n<pre>sudo swapon \/mnt\/swap<\/pre>\n<p>You will then be able to see the new swap file when you view the swap summary.<\/p>\n<pre>swapon -s\r\nFilename\t\t\t\tType\t\tSize\tUsed\tPriority\r\n\/mnt\/swap                               file\t\t262140\t0\t-1<\/pre>\n<p>This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.<br \/>\nOpen up the file:<\/p>\n<pre>sudo nano \/etc\/fstab<\/pre>\n<p>Paste in the following line:<\/p>\n<pre>\/mnt\/swap       none    swap    sw      0       0<\/pre>\n<p>To prevent the file from being world-readable, you should set up the correct permissions on the swap file:<\/p>\n<pre>sudo chown root:root \/mnt\/swap \r\nsudo chmod 0600 \/mnt\/swap<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a \u201clinux swap\u201d can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=645\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"How to Add Swap on Ubuntu","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[39,45,5],"tags":[184,183,182,181],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-infrastructure","category-linux","category-tools","tag-linux-2","tag-partitions","tag-ram","tag-swap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-ap","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=645"}],"version-history":[{"count":1,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/645\/revisions"}],"predecessor-version":[{"id":646,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/645\/revisions\/646"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}