Introduction
In this lab, you will learn the essential administrative task of creating and activating a swap file in Linux. This process allows you to increase your system's virtual memory, which is crucial when physical RAM is insufficient. You will gain hands-on experience with fundamental command-line utilities to manage system resources effectively, providing a flexible alternative to creating a dedicated swap partition.
You will walk through the complete workflow, starting with how to review the current swap space using the swapon command. Next, you will create a file for the swap area with dd, format it for use as swap with mkswap, and then activate it. To conclude the lab, you will verify that the new swap file is successfully configured and recognized by the system.
Review the Current Swap Space with swapon
In this step, you will learn how to check the current swap space configuration on your Linux system. Swap space is a portion of a hard disk that is used as virtual memory when the amount of physical memory (RAM) is full. It allows the system to run more applications than it could fit into physical RAM alone.
To begin, you will use the swapon command, a utility for specifying devices and files for paging and swapping. We will use it with the --show option to display a summary of the active swap devices. Since managing swap space is an administrative task, you need to use sudo to execute the command with root privileges.
In your terminal, which is already in the ~/project directory, run the following command:
sudo swapon --show
You will see an output similar to the following. The details, such as the NAME and SIZE, may vary depending on the initial configuration of your virtual machine. It's also possible that no swap space is configured, in which case this command will produce no output.
NAME TYPE SIZE USED PRIO
/dev/loop12 partition 2G 0B -2
Let's break down the output:
- NAME: The name of the swap device or file. In this example, it's a loop device (
/dev/loop12). - TYPE: The type of swap space, which can be a
partitionor afile. - SIZE: The total size of the swap space.
- USED: The amount of swap space currently in use.
- PRIO: The priority of the swap space. If you have multiple swap spaces, the one with the higher priority will be used first.
By executing this command, you have successfully reviewed the system's current swap configuration. In the following steps, you will learn how to create and activate a new swap file, which is a flexible way to add swap space without repartitioning a disk.
Create a File for Swap Space with dd
In the previous step, you learned how to view the existing swap configuration. Now, you will create a dedicated file that will be used as new swap space. Using a file for swap is a flexible method because it doesn't require repartitioning your hard drive.
We will use the dd command, a powerful utility for copying and converting data. It's ideal for this task because it can create a file of a precise size by reading from a special system device called /dev/zero, which is an endless stream of null characters (zeros).
To create a 250MB file named swapfile in your current directory (~/project), execute the following command. You need sudo because the file will eventually be managed by the system.
sudo dd if=/dev/zero of=swapfile bs=1M count=250
Let's break down this command:
sudo: Executes the command with root privileges.dd: The command-line utility we are using.if=/dev/zero: Specifies the input file./dev/zerois a special file that produces a stream of null characters.of=swapfile: Specifies the output file. This creates a file namedswapfilein the current directory.bs=1M: Sets the block size to 1 Megabyte.ddwill read and write data in 1MB chunks.count=250: Copies 250 blocks. Since our block size is 1M, this results in a 250 * 1M = 250MB file.
After running the command, you will see output confirming that the operation was successful:
250+0 records in
250+0 records out
262144000 bytes (262 MB, 250 MiB) copied, 0.270222 s, 970 MB/s
You have now successfully created a 250MB file. In the next step, you will format this file to be recognizable as swap space and set the appropriate permissions for it.
Format the File as Swap with mkswap
In the previous step, you created a file named swapfile filled with zeros. However, the operating system doesn't yet recognize it as a valid swap area. You need to format it specifically for this purpose. This is where the mkswap command comes in.
The mkswap command sets up a Linux swap area on a device or in a file. It writes a special signature to the file that the kernel can identify.
First, let's format the swapfile you created. In your terminal, run the following command:
sudo mkswap swapfile
After executing the command, you will see output indicating that the swap space has been set up. The system will assign a unique identifier (UUID) to your new swap area. You may also see a warning about insecure permissions, which we'll address next.
mkswap: swapfile: insecure permissions 0644, fix with: chmod 0600 swapfile
Setting up swapspace version 1, size = 250 MiB (262139904 bytes)
no label, UUID=aabf0226-9f7b-47f5-9ad3-10248552795c
Important Security Note: A swap file can potentially contain sensitive data from your computer's memory. For security reasons, it's crucial that only the root user has permission to read from and write to it. Other users on the system should not have any access.
To set the correct permissions, we will use the chmod command. We'll set the permissions to 600, which gives the owner (root) read and write access, and revokes all access for everyone else.
Run the following command to secure your swapfile:
sudo chmod 600 swapfile
This command will not produce any output if it succeeds. You have now successfully formatted the swapfile and secured its permissions. It is now ready to be activated and used by the system, which you will do in the next step.
Activate the New Swap File with swapon
You have successfully created and prepared a swapfile. It is now formatted and has the correct permissions, but the Linux kernel is not yet using it as virtual memory. In this step, you will activate the swap file, making it available to the system.
To do this, you will use the swapon command again. In the first step, you used it with the --show option to view the current configuration. This time, you will provide the path to your swapfile as an argument to enable it.
In your terminal, located at ~/project, execute the following command to activate your new swap space:
sudo swapon swapfile
Let's review the command:
sudo: This is necessary because activating swap space is a system-level operation that requires root privileges.swapon: The command to enable devices and files for swapping.swapfile: The name of the file you want to activate. Since you are in the~/projectdirectory where the file is located, you don't need to specify the full path.
If the command is successful, it will not produce any output. This is standard behavior for many Linux commands—no news is good news.
Your swapfile is now active and being used by the system as additional virtual memory. In the final step, you will verify that the new swap space has been successfully added to the system's total memory resources.
Verify the New Swap Space Configuration
Congratulations! You have successfully created, formatted, and activated a new swap file. In this final step, you will verify that the system recognizes and is ready to use this new resource. This confirms that all previous steps were completed correctly.
Let's start by using the same command from the beginning of this lab, swapon --show. Running it now should display an updated list of active swap spaces, including the swapfile you just added.
In your terminal, execute the following command:
sudo swapon --show
The output will now show two entries: the original swap partition (if one existed) and your new swap file. Notice the new line for /home/labex/project/swapfile.
NAME TYPE SIZE USED PRIO
/dev/loop12 partition 2G 0B -2
/home/labex/project/swapfile file 250M 0B -3
As you can see, the system now recognizes a file-type swap space of 250M. It has also been assigned a priority (PRIO). The kernel will use the swap space with the higher priority first.
Another useful command for checking memory and swap usage is free. Using the -h (--human) option makes the output easier to read by displaying sizes in megabytes (M) or gigabytes (G).
Run the free -h command:
free -h
You will see a summary of the system's memory. Pay close attention to the Swap line.
total used free shared buff/cache available
Mem: 3.5Gi 608Mi 732Mi 16Mi 2.2Gi 2.7Gi
Swap: 2.2Gi 0B 2.2Gi
The total value on the Swap line should now reflect the combined size of the original swap space and your new 250MB file. For example, if you started with 2G of swap, the new total is approximately 2.2G.
You have successfully added and verified a new swap file on your Linux system, giving it more virtual memory to work with.
Summary
In this lab, you learned how to create and activate a swap file to add virtual memory to a Linux system. You began by reviewing the existing swap space configuration using the sudo swapon --show command to understand the current state of the system's virtual memory and interpret its output.
Following the initial review, you proceeded to create a new swap file using the dd command to allocate the desired disk space. You then formatted this file for swap usage with mkswap and activated it with the swapon command, making it immediately available to the system. Finally, you verified that the new swap file was successfully added and active by running swapon --show again, confirming the updated configuration.



