In this comprehensive guide, we will walk you through the process of adding swap space on an Ubuntu 23.10 server. Swap space, also known as “swap,” is a reserved area on a server’s storage device that the operating system can use as virtual memory when allocated memory is fully utilized. By adding swap space, you can prevent your system from slowing down or crashing due to memory exhaustion.
Why Add Swap Space?
When your server’s memory is exhausted due to running applications and processes, the operating system moves less frequently used data from memory to the swap space. This frees up memory for more active tasks and ensures smooth system performance. Adding swap space can be particularly useful for KVM-based VPSes and dedicated servers.
Checking Swap Information
Before adding swap space, it’s essential to check if it’s already enabled on your system. To do this, open the terminal and enter the following command:
swapon --show
Alternatively, you can use the free
command:
free -m
If the output is empty, it means that swap space is not enabled yet on your system.
Adding Swap Space
To add swap space, follow these steps:
Step 1: Create a Swap File
To create a swap file, execute one of the following commands based on your preference:
fallocate -l 1G /swapfile
or
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Note that we are adding a 1GB swap file in this example. If you want to add a different size, replace “1G” with your desired size.
Step 2: Set File Permissions
By default, the permissions on the swap file only enable the root user to access it. To set the appropriate write permission, enter the following command:
chmod 600 /swapfile
Step 3: Set up the Swap Area
To set up a swap area on the file, use the following command:
mkswap /swapfile
Step 4: Activate the Swap File
To activate the swap file, run the following command:
swapon /swapfile
Step 5: Make Swap Permanent
To make the swap space permanent, open the /etc/fstab
file using a text editor such as nano
:
nano /etc/fstab
Add the following line at the end of the file:
/swapfile swap swap defaults 0 0
Save the file and exit the editor.
Step 6: Verify the Swap Size
To verify that the swap space has been successfully added, use the swapon --show
or free -m
command again. You should see the newly added swap space in the output.
Removing Swap Space
If you ever need to remove the swap space, follow these steps:
Step 1: Deactivate the Swap Space
To deactivate the swap space, execute the following command:
swapoff -v /swapfile
Step 2: Remove the Swap Entry from /etc/fstab
Open the /etc/fstab
file using a text editor:
nano /etc/fstab
Locate the line that contains the swap entry:
/swapfile swap swap defaults 0 0
Remove this line from the file, save, and exit the editor.
Step 3: Remove the Swap File
To remove the swap file, run the following command:
rm /swapfile
By following these steps, you can easily add and remove swap space on your Ubuntu 23.10 server.
Conclusion
In this article, we have provided a step-by-step guide on how to add swap space on an Ubuntu 23.10 server. By adding swap space, you can optimize your server’s memory usage and ensure smooth system performance. If you encounter any difficulties during the process, feel free to reach out to us via a support ticket. At Shape.host, we are committed to providing reliable and efficient cloud hosting solutions, including Cloud VPS services, to empower businesses with scalable and secure infrastructure.