Create a Linux Swap File

Create a Linux Swap File

  • Last updated on: 2015-08-19
  •  
  • Authored by: Trey Hoehne

Swap is space on a disk that is reserved to be used as virtual memory. When a Linux server runs out of memory, the kernel can move inactive processes into swap to make room for active processes in the working memory.

A swap partition is not present on Cloud Servers by default, but swap can be added to a server by allocating a swap file. The performance of a swap file is similar to that of a swap partition while making it easier to control the swap size without repartitioning a volume. How aggressively the server will use this swap space can be controlled by modifying the system’s swappiness value.

The steps below discuss the creation of a swap file on Linux and modifying the system swappiness value.

How do I add swap?

To add 1GB of swap to your server, for example, follow these steps:

  1. Create the file to be used for swap.

    sudo fallocate -l 1G /mnt/1GB.swap
    

    If fallocate fails or is not installed, run the following command.

    sudo dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
    
  2. Format the file for swap.

    sudo mkswap /mnt/1GB.swap
    
  3. Add the file to the system as a swap file.

    sudo swapon /mnt/1GB.swap
    
  4. Add this line to the end of /etc/fstab to make the change permanent.

    /mnt/1GB.swap  none  swap  sw 0  0
    
  5. To change the swappiness value edit /etc/sysctl.conf and add the following line.

    vm.swappiness=10
    

    Start with a value of 10 and increase if needed. A typical default value for swappiness is 60. The higher the number (up to 100), the more often swap is utilized.

    How much swappiness affects performance depends on how your memory is being used, so experiment to find an optimal value. At 0 the swap file will only be used when the system runs completely out of memory. Higher values let the system swap idle processes out to allow the system to free memory for disk caching, potentially improving overall system performance.

  6. Check that the swap file was created.

    sudo swapon -s
    
  7. Reboot the server to ensure that the changes go into effect.

Note: By following these instructions on a newly created Rackspace server, the resulting swap file is world-readable. To prevent the file from being world-readable, you should set up the correct permissions on the swap partition/file by running the following command to change the permission:

chmod 600 /mnt/1GB.swap

In most cases, the only user that needs access to the swap partition is the root user.

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
logrotate - rotates, compresses, and mails system logs.
Viewed 905 times since Fri, Nov 30, 2018
Use inotify-tools on CentOS 7 or RHEL 7 to watch files and directories for events
Viewed 11786 times since Fri, Jul 27, 2018
A tcpdump Tutorial and Primer with Examples
Viewed 3041 times since Sun, Jun 17, 2018
20 Linux Command Tips and Tricks That Will Save You A Lot of Time linux
Viewed 3042 times since Thu, Apr 18, 2019
How to disable SSH cipher/ MAC algorithms for Linux and Unix
Viewed 21253 times since Fri, Aug 21, 2020
LVM: Managing snapshots
Viewed 5548 times since Sat, Jun 2, 2018
Linux File Systems (mkfs, mount, fstab) ext4
Viewed 1721 times since Sat, Jun 2, 2018
How To: Linux Hard Disk Encryption With LUKS [ cryptsetup Command ]
Viewed 5768 times since Fri, Jul 13, 2018
SSL HowTo: Decode CSR
Viewed 1789 times since Mon, Feb 18, 2019
Jak ustawić LVM, jak robić snapshoty oraz automatycznie powiększać LV, czyli małe howto
Viewed 3160 times since Sun, May 20, 2018