How to Create and Enable a Swap File on Linux: A Step-by-Step Guide
HardReset.InfoJanuary 2, 20266 min112 views
6 connectionsΒ·8 entities in this videoβChecking Existing Swap
- π Before creating a swap file, it's essential to check if swap is already active on your Linux system.
- π‘ Use the commands
swapon --showandfree -hto verify if any swap is currently configured. Ifswapon --showreturns nothing andfree -hshows 0 swap, you need to create one.
Creating the Swap File
- π To create a swap file, use the
sudo fallocate -l <size> /swapfilecommand. For example,sudo fallocate -l 2G /swapfilecreates a 2GB swap file. - π Crucially, set the correct permissions for the swap file using
sudo chmod 600 /swapfile. This ensures only the root user can read and write to it, which is vital for security.
Formatting and Enabling Swap
- π οΈ Format the newly created file as a swap area with the command
sudo mkswap /swapfile. - β
Activate the swap file immediately by running
sudo swapon /swapfile. - π Verify that the swap file is active and shows the correct size using
swapon --showandfree -h.
Making Swap Permanent
- π To ensure the swap file is enabled after every reboot, you must add an entry to the
/etc/fstabfile. - π Edit the file using
sudo nano /etc/fstaband add the line:/swapfile none swap sw 0 0. - π§ͺ Test the
fstabentry by runningsudo swapon -a. If no errors appear, the configuration is likely correct.
Final Verification
- π» After restarting your Linux system, open the terminal again and run
swapon --showandfree -hto confirm that the swap file is still active and recognized.
Knowledge graph8 entities Β· 6 connections
How they connect
An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.
Hover Β· drag to explore
8 entities
Chapters2 moments
Key Moments
Transcript21 segments
Full Transcript
Topics12 themes
Whatβs Discussed
Swap FileLinuxVirtual MemoryTerminal CommandsSystem AdministrationfstabPermissionsfallocatemkswapswaponfreeReboot
Smart Objects8 Β· 6 links
ProductΒ· 1
ConceptsΒ· 7