Estimated reading time: 2 minute(s).
Setting Ubuntu Swap File
To increase the swap file size on an Ubuntu system using
fallocate
, follow these updated instructions. This method is efficient and quick, suitable for most modern filesystems.
Step 1: Turn Off Existing Swap File
Disable the currently active swap file to modify or replace it:
sudo swapoff -a
This command turns off all swap spaces in use.
Step 2: Create or Resize the Swap File with
fallocate
Use
fallocate
to allocate space for the swap file instantly:
sudo fallocate -l 4G /swapfile
-
-l 4G
specifies the desired size of the swap file, 4GB in this example. -
/swapfile
is the file to be used or created for swap.
Adjust the size (
4G
in the example) according to your needs.
Step 3: Secure the Swap File
Set the swap file's permissions to ensure it is only accessible by the root user:
sudo chmod 600 /swapfile
This command restricts access to the swap file, enhancing security.
Step 4: Make the Swap File
Format the newly created file for use as swap:
sudo mkswap /swapfile
This prepares the swap file for use by setting it up as a swap space.
Step 5: Activate the Swap File
Enable the swap file, adding it to your system's swap space:
sudo swapon /swapfile
This step makes the swap file active, allowing the system to use it.
Step 6: Make the Swap File Permanent
Edit
/etc/fstab
to include the swap file for automatic activation at boot time:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Appending this line to
/etc/fstab
ensures the swap file is recognized and activated at every startup.
Step 7: Verify the Swap File
Check that the swap is active and verify its size:
sudo swapon --show
To see the total available swap space, including the newly added swap:
free -h
This concludes the steps to increase the swap file size on your Ubuntu system using
fallocate
. Adjust the swap file size in step 2 as necessary to suit your system's requirements.