Adding a swap file to Linux from command line.

Generate a file and save it in fstab



Guide to add a swap file to Linux from command line.


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

5. Verify

Verify it is activate: cat /proc/swaps

6. Save

Make it permanent, adding it to /etc/fstab echo ‘/4GiB.swap none swap sw 0 0’ | sudo tee -a /etc/fstab


Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

How to add a swap file to Ubuntu Linux from command line by generating a special file and configuring it at fstab.

Clutter-free software concepts.
Translations English Espa簽ol

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