DEV Community

hub
hub

Posted on

1

add swap file on the linux-install

well from time to time i install a new notebook and doing so i want to add swap file on the linux-install.

So here a little story: well my notebook (a old one) doesn’t have that much swap space and yes i really think the swap space is not adequate and much enough,

so i think i have to do something and to create swap file on my Linux notebook. a friend of mine told me that i can do even more - i can create multiple swap files as well he said.

well - it *allways helps me if i write down such steps *- so i can make sure if i have all steps together. And perhaps - if one tiny step is missing, feel free t add some lines - add a comment. Thank you in advance.

So here we start: this is the beginning

well i am trying to make up my mind how to create swap file on my Linux notebook. I am using a Ubuntu OS - so i guess that i have to test it on my ubuntu-notebook.

well i think that there are several preliminary steps do take - before i can set up a swap-file. the very very first step in the show:

we just** make a new swap file**

At the beginning i think that i ll have to create a file with the size of swap space Well let’s assume that I want to add 2 GB of swap space to my Notebook - and to the operating system.

I will go through the necessary steps - that means all the steps that are necessary to do a nicer and good little Swap-space-file.

So First things first: we do some preliminariy things:

at the very very beginning i think that i will have to use the so called fallocate command to create a file of size 2 GB. This is the command i have to add in the terminal:

sudo fallocate -l 2G /swapfile

well my girlfriend allways uses some of these commands when she is making a swapfile: the recommends me to allow only root to read and write to the swap file. So - to follow this recommendation (of my girlfriend) as she allways has good recommendations and i think it is a good thing i guess. We ll even see warning like “insecure permissions 0644, 0600 suggested” when we try to use this file for our new swap area (that we want to cr eate)

sudo chmod 600 /swapfile

Well to be frank We have to go further: what about the name of our tiny swap file!? btw: as mentioned above - yes it could be anything. If one needs more than one - if one needs
multiple swap spaces, he can give it any appropriate name

like so for example.

swapspace_file_a, swapsüace_file_b etc.
swapspace_file_c and so on and so forth

Well we see here:_ It’s just a file with a so called predefined size: that is pretty easy. And yes - i follow to the recommendation of my girlfriend.

That said we can go further in the story.

the next step is pretty important:

the filecreation-step 2: now we mark the new file as a so called swap space

Well at this point we need to tell the Linux system that our new file - (which w e have created now) that file will be used as a so called swap space: we can do that with mkswap tool.

sudo mkswap /swapfile

Well at this point we should be able to see an output like this:

Setting up swapspace version 1, size = 2048 MiB
and here we can do like so: use no label, UUID=xyz

and now - we can go ahead and do even more. We can do more in the step5

Step 3: the next step in the show: at this point we can do the good thing - and enable the swap file

With this step - our system knows that the file swapfile can be used as a so called swap space. And this is generally a good thing!

but wait: it is not done yet. We have to do even some steps more: that said - we ll need to enable the swap file so that our system can start using this file as swap. And believe me dear friend: this is a pretty important step We can not do the creation of the swap file without these preliminaries:

So the command here would be like the following command:

sudo swapon /swapfile

Now if we check the swap space, we should see that our Linux system recognizes and uses it as the swap area:

swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2048M 0B -2

so we ll have to take care that the changes are pretty permanent on the system - that is on my tiny notebook; so in the next step i ll have to take care for this. We move ahead in the next step the Step 4 we do even more: now we make the changes permanent. And my girlfriend said to me that this is damned important. So i belive this.

This step seems to be important for me - since i thin that doing it will stop me form doing the same thing over and over - again and again: Well whatever we have done a swap-file-creation it is (first of all) is temporary file.

But - sure thing: we want more - at least i want to have so i think that we have to reboot the system and all the changes will disappear. And yes: this is not what we want.

We want to make the changes permanent by adding the newly created swap file to /etc/fstab file.

That said - i think that i t is allways a good thing if we do a (quick and dirty) backup before we make any changes to the /etc/fstab file.

sudo cp /etc/fstab bla bla bla bla /etc/fstab.back

Now we have came thus far - i am happy - and my girlfriend is happy too:

Now we can add the following line to the end of /etc/fstab file:

/swapfile none swap sw 0 0

Woooh - wooh - we came thus far.
Now it is necessary t o do a little step


echo '/swapfile none swap sw 0 0'
|

and thus it is important to see that

sudo tee -a /etc/fstab

Now after all these many many i guess that we have everything in place. our swap file will be used even after we reboot the notebook. We have come thus fare - thats awesome: in the last step we have to adjust the so called swappiness

well the so called swappiness parameters determines how often the swap space should be used. My girlfriend says - it is this a pretty important thing. Well the so called swappiness value can be in the ranges from 0 (that is zero) to 120. Higher value means the swap space will be used even more often -that means even more frequently.

The default swappiness in Ubuntu desktop is about 50 : i guess while in server it is 1.

well - we can check the swappiness with the following command:

cat /proc/sys/vm/swappiness

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (4)

Collapse
 
gbhorwood profile image
grant horwood

you can confirm that your fstab file is good after editing it with:

mount -fav
Enter fullscreen mode Exit fullscreen mode

the 'a' is for 'all', the 'v' is for 'verbose' and the 'f' is the important part: it means 'fake'. basically that tests mounting fstab without actually doing any mounting.

Collapse
 
digital_hub profile image
hub

many many thanks for the reply . great to hear from you
As i am currently learning - its so awesome to get comments and ideas.

thank you alot!!!

i am very glad to be part of this great community.

Collapse
 
digital_hub profile image
hub

awesome - many many thanks!

Collapse
 
digital_hub profile image
hub

many thanks - this is a great idea. I am very happy that you have replied.
and yes - with my text i am just learning.

i am very happy to be part of this great community.
have a great day

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay