Swap space is an area on a disk that is used as virtual memory. When your system runs low on physical memory (RAM), inactive pages in memory are moved to the swap space, allowing the system to continue running smoothly. This is known as “swapping”.
In this article, we will show you how to add swap space on Debian 10 (also known as “Buster”). We will be using the command line for this tutorial, so you will need to have a basic understanding of how to use the Linux command line.
Before we begin, there are a few prerequisites that you will need to have in place in order to add swap space on your system:
- A server running Debian 10
- A user account with sudo privileges
- Enough free space on the disk to create a swap file
To check if you have enough free space on the disk to create a swap file, run the following command:
df -h
This will display the available space on each mounted filesystem on your system.
If you do not have enough free space on the disk, you will need to free up some space before you can create a swap file.
- Create a swap file
To create a swap file, you will need to use the “fallocate
” command. This command allows you to create a file of a specified size on your filesystem.
To create a swap file, run the following command:
sudo fallocate -l <swap-size> /swapfile
Replace <swap-size
> with the size of the swap file that you want to create. For example, to create a 1GB swap file, you would use the following command:
sudo fallocate -l 1G /swapfile
This will create a 1GB swap file named “/swapfile
” on your system.
- Set the correct permissions on the swap file
To ensure that only the root user can access the swap file, you will need to set the correct permissions on the file.
To set the correct permissions on the swap file, run the following command:
sudo chmod 600 /swapfile
This will set the permissions on the swap file so that only the root user can read and write to the file.
- Set up the swap file
To set up the swap file and enable it on your system, you will need to use the “mkswap
” command. This command initializes the swap file and sets it up for use as swap space on your system.
To set up the swap file and enable it on your system, you will need to use the “mkswap
” command. This command initializes the swap file and sets it up for use as swap space on your system.
To set up the swap file, run the following command:
sudo mkswap /swapfile
This will initialize the swap file and set it up for use as swap space on your system.
- Enable the swap file
To enable the swap file and start using it as swap space on your system, you will need to use the “swapon
” command. This command enables a swap file or partition and adds it to the system’s swap space.
To enable the swap file, run the following command:
sudo swapon /swapfile
This will enable the swap file and add it to the system’s swap space.
- Make the swap file persistent
To make the swap file persistent across system reboots, you will need to add it to the /etc/fstab
file. This file defines the available filesystems and their mount options.
To add the swap file to the /etc/fstab
file, run the following command:
sudo nano /etc/fstab
This will open the /etc/fstab
file in the nano text editor.
Add the following line to the file, replacing <swap-size
> with the size of your swap file:
/swapfile none swap sw 0 0
This line defines the swap file and its mount options. The “none
” value for the “file system
” field specifies that the file is not a regular filesystem, and the “swap
” value for the “type
” field specifies that it should be used as swap space.
Save the file and exit the editor.
To apply the changes, you will need to reboot the system. To do this, run the following command:
sudo reboot
This will reboot the system and apply the changes that you made to the /etc/fstab
file.
After the system has rebooted, you can verify that the swap file is being used by running the “free” command. This command displays the amount of free and used memory on the system.
To display the amount of free and used memory on the system, run the following command:
free -h
This will display the amount of free and used memory on the system, including the amount of swap space being used.
Conclusion
In this article, we showed you how to add swap space on Debian 10. We covered all of the necessary steps, including creating a swap file, setting the correct permissions on the file, setting up the file for use as swap space, enabling the file, and making it persistent across system reboots. With swap space added to your system, you can ensure that your system has enough virtual memory to continue running smoothly, even when physical memory is low.