Axxellance blog logoAxxellanceBlog
How to Create Swap Space on a Linux Server or Desktop
  • linux
  • server
  • ubuntu

How to Create Swap Space on a Linux Server or Desktop

Michael ChukwuemekaSat, 30 Nov 2024 17:22:36 GMT 0

Swap space is an essential feature in Linux that acts as a safety net for your system’s memory. When physical RAM runs low, swap provides additional virtual memory to keep processes running smoothly.

Whether you're using a Linux desktop or server, creating and configuring swap space is a straightforward process that can improve performance and system stability.

First thing to do is to check the available amount of swap space you have on your Linux Server or Desktop, simple type this command into the terminal free -h.

If your system is showing 0B for swap space, it means that swap is either not configured or not activated. You can create and enable swap space on your Linux server by following these steps:

Step 1: Create a Swap File

  1. Decide the Size of the Swap File: Choose the amount of swap space you want to create. For example, to create a 4GB swap file, you can use the following command. Adjust the size (4G) as needed.

  2. Create the Swap File: Run the following command to create an empty swap file. This example creates a 4GB swap file in the /swapfile location.

    sudo fallocate -l 4G /swapfile
    
    • The fallocate command is used to allocate space for the swap file. If fallocate doesn't work on your system (e.g., due to filesystem restrictions), you can use dd instead (see below).

    If fallocate doesn’t work, you can use dd as an alternative:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
    
    • This command creates a swap file with a size of 4GB (bs=1M count=4096 means 4GB).

Step 2: Set the Correct Permissions

The swap file needs to have the correct permissions so that it can be used as swap. Run the following command:

sudo chmod 600 /swapfile

Step 3: Set Up the Swap Space

Now, format the file as swap:

sudo mkswap /swapfile

This command will create the swap space in the /swapfile file.

Step 4: Enable the Swap File

To enable the swap file, run:

sudo swapon /swapfile

Now, the swap file should be active, and you should see it listed in the swap space when you run the swapon --show or free -h command.

Step 5: Make the Swap File Permanent

To ensure that the swap file is used after a system reboot, you need to add it to the /etc/fstab file:

  1. Edit the /etc/fstab file:

    sudo nano /etc/fstab
    
  2. Add the following line to the end of the file to enable the swap file automatically on boot:

    /swapfile none swap sw 0 0
    
  3. Save the file and exit (in nano, press Ctrl + X, then Y, and Enter to confirm).

Step 6: Verify the Swap Space

Finally, verify that the swap space is correctly enabled:

  • Run swapon --show to see the swap space.
  • Alternatively, use free -h to check the total, used, and free swap space.

Example output from free -h:

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.1G        1.2G        0B        4.4G        5.0G
Swap:          4.0G        0.0G        4.0G

Step 7: (Optional) Adjust Swappiness (Tuning)

The swappiness value controls how aggressively the system will use swap space. By default, the value is usually set to 60, which means the system will start using swap when memory usage reaches 60%.

To check the current swappiness value:

cat /proc/sys/vm/swappiness

To change it (for example, to 10 to make the system less likely to swap):

sudo sysctl vm.swappiness=10

To make this change persistent across reboots, add the following line to /etc/sysctl.conf:

vm.swappiness=10

Conclusion

By following these steps, you should now have successfully created a swap file on your system and enabled it for use. You can verify the swap space is being utilized and tweak the swappiness setting if necessary.