{"id":927,"date":"2025-09-24T09:49:07","date_gmt":"2025-09-24T09:49:07","guid":{"rendered":"https:\/\/tensorzen.blog\/?p=927"},"modified":"2025-09-24T09:49:07","modified_gmt":"2025-09-24T09:49:07","slug":"adding-swap-space-on-ubuntu-24-04","status":"publish","type":"post","link":"https:\/\/tensorzen.blog\/?p=927","title":{"rendered":"Adding Swap Space on Ubuntu 24.04"},"content":{"rendered":"\n<p>source page: <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-add-swap-space-on-ubuntu-22-04\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-add-swap-space-on-ubuntu-22-04<\/a><\/p>\n\n\n\n<p>We can see if the system has any configured swap by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t get back any output, this means your system does not have swap space available currently. You can verify that there is no activate swap using the free utility:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>free -h<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"959\" height=\"113\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image.png\" alt=\"\" class=\"wp-image-929\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image.png 959w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-300x35.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-768x90.png 768w\" sizes=\"auto, (max-width: 959px) 100vw, 959px\" \/><\/figure>\n\n\n\n<p>Since  the server in our example has 0.5G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fallocate -l 1G \/swapfile<\/code><\/pre>\n\n\n\n<p>We can verify that the correct amount of space was reserved by typing:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"47\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-1.png\" alt=\"\" class=\"wp-image-930\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-1.png 624w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-1-300x23.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>Make the file only accessible to root by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 600 \/swapfile<\/code><\/pre>\n\n\n\n<p>Verify the permissions change by typing:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"50\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-2.png\" alt=\"\" class=\"wp-image-931\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-2.png 527w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-2-300x28.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/figure>\n\n\n\n<p>As you can see, only the root user has the read and write flags enabled. We can now mark the file as swap space by typing:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"75\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-3.png\" alt=\"\" class=\"wp-image-932\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-3.png 790w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-3-300x28.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-3-768x73.png 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/figure>\n\n\n\n<p>After marking the file, we can enable the swap file, allowing our system to start using it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapon \/swapfile<\/code><\/pre>\n\n\n\n<p>Verify that the swap is available:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"69\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-4.png\" alt=\"\" class=\"wp-image-933\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-4.png 518w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-4-300x40.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/figure>\n\n\n\n<p>We can check the output of the free utility again to corroborate out findings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"885\" height=\"92\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-5.png\" alt=\"\" class=\"wp-image-934\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-5.png 885w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-5-300x31.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-5-768x80.png 768w\" sizes=\"auto, (max-width: 885px) 100vw, 885px\" \/><\/figure>\n\n\n\n<p>Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our \/etc\/fstab file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile       swap    swap    defaults        0       0<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"84\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-6.png\" alt=\"\" class=\"wp-image-935\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-6.png 1002w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-6-300x25.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-6-768x64.png 768w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/figure>\n\n\n\n<p>The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represent a percentage. With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are <code>expensive<\/code> in that they take a lot longer that interactions  with RAM and they can cause a significant reduction in performance.<\/p>\n\n\n\n<p>Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. We can see the current swappiness value by typing:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"55\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-8.png\" alt=\"\" class=\"wp-image-937\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-8.png 685w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-8-300x24.png 300w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/figure>\n\n\n\n<p>We can set the swappiness to a different value by using the sysctl command:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"50\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-9.png\" alt=\"\" class=\"wp-image-938\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-9.png 638w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-9-300x24.png 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/figure>\n\n\n\n<p>This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our \/etc\/sysctl.conf file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"420\" src=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-10.png\" alt=\"\" class=\"wp-image-939\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-10.png 848w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-10-300x149.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2025\/09\/image-10-768x380.png 768w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>source page: https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-add-swap-space-on-ubuntu-22-04 We can see if the system has any configured swap by typing: If you don&#8217;t get back any output, this means your system does not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[36],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux"],"_links":{"self":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=927"}],"version-history":[{"count":3,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions\/941"}],"wp:attachment":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}