How to add Swap space on Ubuntu


How to add Swap space on Ubuntu

Create a file and preallocate size. In the following example we allocate 4GB

$ sudo fallocate -l 4G /swapfile

The prompt will return:

$ ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Feb  8 08:50 /swapfile

Enable SWAP file

We need to enable it before to use it, but first let’s make sure the permissions are right

$ sudo chmod 600 /swapfile

Check if the permissions were applied correctly:

$ ls -lh /swapfile
-rw------- 1 root root 4.0G Feb  8 08:50 /swapfile

So we have only read write only enabled for root.

Now that everything is correct we tell our OS to use this file as swap by typing:

$ sudo mkswap /swapfile

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=9457c9c4-96eb-4fd8-821e-dfdf202d7f45

All we have to do now is to *enable it:

$ sudo swapon /swapfile

Verify if the procedure was correct:

$ sudo swapon -s
Filename				Type		Size	Used	Priority
/swapfile               file    	4194300	  0	        -2

Make swap permanent

We have the swap activated, but after reboot it will be deactivated so we need to modify the fstab file

$ sudo nano /etc/fstab

At the end of the file add

/swapfile   none    swap    sw    0   0

Save and close the file when you are finished.

Picture offered by Pixabay

Newsletter


Related Posts

Tools for Analyzing Your Competition

Before knowing where your business stands, you should look at the market and your competition. You need to know what marketing tactics and strategies are working for your audience, what traffic the competition has, and how they position.

Free Online Photo Editor with Cool Effects | Edit Images Easily

Transform your images with our free online photo editor with cool effects. Add filters, adjust colors, and apply creative enhancements. Edit your photos easily and make them stand out.

How to find the size of a table in SQL?

Learn how to determine the size of a SQL table with this informative guide. Discover efficient methods for measuring table size.

MongoDB Query date by _id field

Query date by _id in mongodb. Straighforward guide to solve your issue right away.

Unleashing the Power of Free API for Scrape Reverse Geocoding

Learn how to harness the potential of reverse geocoding with our beginner's guide to scraping APIs for free. Unlock the power today.

Discover the Power of Whatsapp-web.js for Safe and Easy Automation

Automate WhatsApp with ease using Whatsapp-web.js, a NodeJS client library that connects through the official WhatsApp Web app, reducing ban risks. Perfect for user or business accounts.

Website tours js tools

Tools to create live tours for your web project using js libraries.

Unlocking SaaS Marketing Success: Essential Resources to Learn and Grow

Discover a comprehensive collection of valuable resources to master the art of SaaS marketing.

Where to launch your startup

Do you wanna launch grandiosely your startup? Get a list of platforms where people can discover your project and can push your project directly to the sky.

Email campaign improving my SaaS convertion - Manager Flota

Users subscribe to your SaaS, but don't let the money on your table. Here's how I do it!