Adding a swap memory to Linux from command line in 6 steps
Generate a file and save it in fstab
Overview
This is a quick guide to add a swap file to Linux from command line.
But first, what is swap memory?
Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.
We will add a special formatted file that works as a swap file and
then add it to the /etc/fstab
to have it also after the system reboots.
Steps
1. Create File
Create a file of 4GB
sudo fallocate -l 4g /4GiB.swap
or if fallocate
isn’t present:
sudo dd if=/dev/zero of=/4GiB.swap bs=1024 count=4000
2. Readonly
Make it readable only for root sudo chmod 600 /4GiB.swap
3. Swap format
Format the file as swap: sudo mkswap /4GiB.swap
4. Activate it
Enable use of Swap File sudo swapon /4GiB.swap
4.1 Verify
Verify it is active.
$ cat /proc/swaps
Filename Type Size Used Priority
/4GiB.swap file 4194300 0 -2
5. Save
Make the file present after rebooting the system. To make it
permanent, add it to /etc/fstab
.
echo ‘/4GiB.swap none swap sw 0 0’ | sudo tee -a /etc/fstab
5.1 Verify
$ cat /etc/fstab
LABEL=cloudimg-rootfs / ext4 defaults 0 0
LABEL=UEFI /boot/efi vfat defaults 0 0
/4GiB.swap none swap sw 0 0
Conclusion
Now we have our RAM memory and can also paginate into swap memory.
$ free -m
total used free shared buff/cache available
Mem: 981 819 70 3 91 40
Swap: 4095 3 4092
References
- https://help.ubuntu.com/community/SwapFaq
- https://en.wikipedia.org/wiki/Paging#Swap_files_and_partitions
- Find out IP addresses from MACs in a Local Area NetworkMay 10, 2023
- Choose any key as the modifier in i3wm in 6 stepsJanuary 20, 2021
- Adding a swap memory to Linux from command line in 6 steps
- Free up space in Linux (Ubuntu)March 27, 2020
- Switch between languages in Linux. Write in multiple languages with same keyboard.March 21, 2020
- How to make Ubuntu display emojisFebruary 12, 2020
- Detect and mount USB devices in Linux from consoleJanuary 24, 2019
- How to make screencasts in Ubuntu LinuxJanuary 21, 2019
- Using i3 window manager in LinuxJanuary 7, 2019
- Setting Up A Fresh Linux ServerAugust 25, 2018
- How To Download A Website With Wget The Right WayJune 30, 2017
- Replicate Installed Package Selections From One Ubuntu System To AnotherApril 24, 2017
- Using Clamav Antivirus In UbuntuJanuary 25, 2017
- How to Type Spanish Characters, Accents and Symbols in LinuxJune 6, 2016
Ubuntu
- How to activate tap to click touchpad's feature in Ubuntu in 4 stepsMarch 4, 2021
- Difference between suspend and hibernate in Ubuntu and how to execute them from command lineApril 12, 2020
- Solving Google Chrome's gpu-process error message in Ubuntu LinuxJanuary 7, 2019
- Solving Google Chrome's secret service operation error message in Ubuntu LinuxJanuary 7, 2019
- Start Emacs In Ubuntu The Right WayJune 10, 2017
Unix Shell
- Connect to a Bluetooth device from command line in Ubuntu LinuxJune 23, 2020
- Add Infolinks Script To An Existing Website From Console With Sed CommandApril 4, 2017
- How to change all files permissions to 644 and directories to 755January 10, 2017
- Shell Redirect Output And Errors To The Null Device In BashDecember 9, 2016
- Prevent Running Of Duplicate Cron JobsDecember 8, 2016
- Delete All Backup Files Recursively In BashNovember 28, 2016
- Bash Script to Find Out If MySQL Is Running Or NotNovember 9, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·