Ubuntu Linux Create and Add Swap File Tutorial

Ubuntu Linux Create and Add Swap File Tutorial

To create and add a swap file on an Ubuntu Linux system, you can follow these steps:

  1. Determine the size of the swap file that you want to create. A good rule of thumb is to make the swap file twice the size of the available RAM on the system. For example, if the system has 4 GB of RAM, you can create an 8 GB swap file.

  2. Create a file with the desired size for the swap file. For example, to create an 8 GB swap file, you can use the following command:

sudo fallocate -l 8G /swapfile
Source:ww‮.w‬lautturi.com

This will create an empty file with a size of 8 GB in the root directory, called /swapfile.

  1. Set the appropriate permissions on the swap file. The swap file should be owned by the root user and the sys group, and have permissions set to 0600 (readable and writable by the owner only):
sudo chmod 600 /swapfile
sudo chown root:sys /swapfile
  1. Format the swap file as a swap space. You can use the mkswap command to do this:
sudo mkswap /swapfile
  1. Enable the swap file. You can use the swapon command to enable the swap file:
sudo swapon /swapfile

The swap file should now be enabled on the system. You can verify the configuration by running the swapon -s command, which should display the swap file in the output.

  1. To make the swap file persistent across system reboots, you will need to add it to the /etc/fstab file. Open the /etc/fstab file in a text editor (e.g. nano) and add the following line to the end of the file:
/swapfile swap swap defaults 0 0

Save the /etc/fstab file and close the text editor. The swap file should now be persistent across system reboots.

Keep in mind that the above steps are just a basic guide for creating and adding a swap file on Ubuntu Linux. The specific steps and options may vary depending on your system and configuration.

For more information on creating and managing swap space on Ubuntu Linux, you can refer to the Ubuntu documentation or the documentation for the swapon, mkswap, and fstab utilities.

Created Time:2017-10-30 14:27:25  Author:lautturi