DEV Community

hubs
hubs

Posted on • Edited on

creating a swap-file on a computer - a stepwise HowTo

i have a very very old notebook a Thinkpad t530

and i need to do some clever steps to get a ubuntu (or other linux flavour ) up and running. Well i have heard that swap-files may help here.

so idid some digging into the topic. but to make sure i am doing right - have written down all the things i have read. So plz correct me if i have learned any thing wrong. I want you to give me hints and to correct me if i am doing wrong in creating a swap file.

and yes: this may be a help for others too. i hope so at least.
BTW: i am so happy about this awesome forum here:

A swap file is a type of swap memory for Computers.

Well a swap file is in the form of a computer file on an HDD or SSD that serves as an extension to physical memory (RAM). It is a good thing.
A machine uses a swap file to store data from RAM that is temporarily inactive - and we can also say - that is not used.
The so called swapping process frees lots of memory - so called up RAM space: How does this work?
Well frankly it works by moving infrequently used data in RAM to a so called swap file - and this is generally spoken a good thing.

Swap files are a special case of virtual memory management: i like this very much. Swap-files serve as a kind of - lemme say fallback mechanism in the computer.

we can say that the swap space has significantly less speed (some friends of myne say a so called slower access times) than RAM.

how to deal with swap-files - well thats the quesion: how to create one swap-file:

The free command with the -h tag shows us the total, available, and used swap space in our computer:

free -h

The /proc/meminfo file . it is another form to show the total, free, and used swap space in human readalbe format - here in kilobytes:

cat /proc/meminfo | grep Swap

explanation: The /proc/swaps file shows active swap devices:

cat /proc/swaps

but there is more: The swapon command is a very very cool command with the --show tag displays swap information:

swapon --show

well there is even more: We can use swap-investigations on a linux machine: I will show some more commmands on terminal

How to View a Swap File on Linux:
To view the contents of a swap file, use the strings command and save the contents to another file. For example:

sudo strings <some kind of a swap file path> > <some kind of a output file path>.txt

How can i create a new Swap File on my Linux notebook?

well i think that reating a swap file on Linux is a pretty simple process: That said i think i have to do like so: to make a new swap file on a Linux system.

1. i have to create some kind of Storage File
therefore i have to make use the dd tool to create a new storage file. For example:

like so

sudo dd if=/dev/zero of=/swapfile bs=2MB count=1024

note: we could have used every (!) other value. This is only a simple example. Yes it is.

The command contains the following parameters:

if=/dev/zero is the input file. The /dev/zero

file is a kind of a very special file that returns as many (awful many) null characters as a read operation requests.
of=/swapfile is the output swap storage file. The common practice is to place the file in the root directory.
The bs parameter is the block size.: it is damned important
The count parameter determines how many blocks to copy. I often use this command. The total data size is bs*count, which in this case is 2GB. But well, we could use every other value. Believe me. We could.

and now its pretty important to set the Swap Permissions; but how!? can i set the swap file permissions for the swap file to the user root read and write.

well can i do make use the following command:

sudo chmod 600 /swapfile

The command shows - afaik no output. Check the permissions with the following:

ls / | grep swapfile

swap file permissions - well this is pretty important. We need to do this extra step: but wait: how to do this!? well how to changing the permissions to root helps avoid accidental overwriting.

3. Set Up Swap Area

that said - we have to take care for the rest of the conditions: The swap file requires some kind of formatting the reserved file size into a so called swap area. This step is pretty important. Here we prefer to make use of the so called mkswap command to format the swap file:

sudo mkswap /swapfile

The command formats the swap file into a swap space and outputs the size, label, and UUID.

4. Enable Swap
To enable the swap area for use, run the following command:

sudo swapon /swapfile

Verify the swap is active with the following command:

swapon --show

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay