Tutorial How to change swap file size on Ubuntu server from 20GB to 100GB

HelmutsHelmuts is verified member.

Domain Summit | HostMaria
Staff member
Joined
Dec 23, 2022
Messages
678
Reaction score
0
I recently stumbled upon this task, and this is both a reminder for myself how to do this and might help you as well. .. also, I believe, I might return to this post and post some updates if something was missing.

Ok, let's go >> to change the swap file size on an Ubuntu server from 20GB to 100GB, you can follow these steps:

1. Disable the current swap file: Before resizing the swap file, you need to disable the current swap file using the
Code:
swapoff
command. Run the following command to disable the current swap file:

Code:
sudo swapoff /swapfile

2. Remove the current swap file: Once the swap file is disabled, you can remove the current swap file using the
Code:
rm
command. Run the following command to remove the current swap file:

Code:
sudo rm /swapfile

3. Create a new swap file: Use the
Code:
fallocate
command to create a new swap file with the desired size of 100GB. Run the following command to create a new 100GB swap file:

Code:
sudo fallocate -l 100G /swapfile

Note that the
Code:
fallocate
command creates a file of a specific size without actually allocating disk space until data is written to it.

4. Set the correct permissions: Set the correct permissions for the new swap file (always: 600) using the
Code:
chmod
command. Run the following command to set the correct permissions:

Code:
sudo chmod 600 /swapfile

5. Set up the swap area: Set up the swap area on the new swap file using the
Code:
mkswap
command. Run the following command to set up the swap area:

Code:
sudo mkswap /swapfile

6. Enable the new swap file: Enable the new swap file using the
Code:
swapon
command. Run the following command to enable the new swap file:

Code:
sudo swapon /swapfile

7. Update the fstab file: Update the
Code:
/etc/fstab
file to ensure that the new swap file is mounted automatically at boot time. Run the following command to open the
Code:
fstab
file in a text editor:

Code:
sudo nano /etc/fstab

8. Add the following line to the end of the file:

Code:
/swapfile none swap sw 0 0

9. Save and close the file:

9.1) To save the changes and exit nano, press "Ctrl+X" on your keyboard. This will prompt you to save the changes before exiting
9.2) Press "Y" to confirm that you want to save changes
9.3) Press "Enter" to confirm the file name and location

10. Verify the new swap file: Verify that the new swap file is set up correctly by running the following command:

Code:
sudo swapon --show

This command should show the new swap file with the correct size of 100GB.

Note: It's important to ensure that you have enough free disk space on your server to create a 100GB swap file, and that your server has enough physical RAM to make use of a swap file of this size. Additionally, changing the size of the swap file may impact system performance, so it's important to monitor the system after making this change.

Voila :)

f4e2ccf3-b44b-40ee-8be6-e6f5492f3caa_text.gif
 
Back
Top