Skip to main content

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 --show and free -h to verify if any swap is currently configured. If swapon --show returns nothing and free -h shows 0 swap, you need to create one.

Creating the Swap File

  • πŸš€ To create a swap file, use the sudo fallocate -l <size> /swapfile command. For example, sudo fallocate -l 2G /swapfile creates 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 --show and free -h.

Making Swap Permanent

  • πŸ“Œ To ensure the swap file is enabled after every reboot, you must add an entry to the /etc/fstab file.
  • πŸ“ Edit the file using sudo nano /etc/fstab and add the line: /swapfile none swap sw 0 0.
  • πŸ§ͺ Test the fstab entry by running sudo 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 --show and free -h to 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