Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active August 25, 2024 23:11
Show Gist options
  • Save fernandoaleman/6aad581d5b2dbcc7c3b8fe38f619a80a to your computer and use it in GitHub Desktop.
Save fernandoaleman/6aad581d5b2dbcc7c3b8fe38f619a80a to your computer and use it in GitHub Desktop.
How to check, view, create and remove a swap file on Linux

How to Check Swap Space

There are several ways to check swap space in Linux.

  • The free command with the -h tag shows the total, available, and used swap space in a human-readable format.
$ free -h
  • The /proc/meminfo file shows the total, free, and used swap space in kilobytes.
$ cat /proc/meminfo | grep Swap
  • The /proc/swaps file shows active swap devices.
$ cat /proc/swaps
  • The swapon command with the --show tag displays swap information. The output shows the active swap devices, including the path, file type, size, and usage.
$ swapon --show

To monitor swap space, use a monitoring tool such as the vmstat command.

How to View a Swap File on Linux

To view the contents of a swap file, use the strings command and save the contents to another file.

$ sudo strings /swapfile > output.txt

How to Create a New Swap File on Linux

Creating a swap file on Linux is a simple process.

Create Storage File

Use the dd tool to create a new storage file.

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

The command contains the following parameters:

  • if=/dev/zero is the input file. The /dev/zero file is a special file that returns as many null characters as a read operation requests.
  • of=/swapfile is the output swap storage file. The common practice is to place the file in the root directory.
  • The bs parameter is the block size.
  • The count parameter determines how many blocks to copy.

The total data size is bs*count, which in this case is 2GB.

Set Swap Permissions

Set the swap file permissions to root read and write.

$ sudo chmod 600 /swapfile

The command shows no output. Check the permissions with the following:

$ ls / | grep swapfile

Set Up Swap Area

The swap file requires formatting the reserved file size into a swap area.

$ sudo mkswap /swapfile

The command formats the swap file into a swap space and outputs the size, label, and UUID.

Enable Swap

Enable the swap area for use.

$ sudo swapon /swapfile

Verify the swap is active.

$ swapon --show

The output shows a list of swapping devices and files, including the newly created swap file.

Persist Swap

The swap file does not persist after a restart by default. To make the changes permanent:

  1. Open the /etc/fstab file with a text editor.
$ sudo vim /etc/fstab
  1. Append the swap information in the following format.
/swapfile swap swap defaults 0 0
  1. Save the file and close the editor.

How to Adjust Swappiness on Linux

Swappiness determines how often a Linux system uses the swap space. The swappiness value is a number between 0 and 200.

  1. To check the current system swappiness value, run:
$ cat /proc/sys/vm/swappiness

The command prints a number between 0 and 200. A low value avoids swapping, while a high value increases swap usage.

  1. To change the swappiness value to 10, run:
$ sudo sysctl vm.swappiness=10

The optimal value depends on the system, workload, and memory usage. Production servers typically require lower values.

  1. To persist the change after a restart, open the /etc/sysctl.conf file:
$ sudo vim /etc/sysctl.conf
  1. Append the following information to the file:
vm.swappiness = 10
  1. Save the file and close vim.

How to Remove a Swap File on Linux

Removing a swap file requires deactivating the swap space before removal.

  1. To deactivate the swap, run:
$ sudo swapoff -v /swapfile
  1. Use the rm command to delete the swap file:
$ sudo rm /swapfile
  1. Open the /etc/fstab file:
$ sudo vim /etc/fstab
  1. Remove the following swap information.
/swapfile swap swap defaults 0 0
  1. Save the file and close vim.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment