person github
If you want to increase swap space to improve system stability or to cope with low memory conditions, you can create a swap file or allocate a swap partition. Creating a swap file is an easy and fast way, here are the basic steps to create a swap file:
-
Create an empty file :
Run the following command as root, where/swapfile
is the path to the swap file and1G
is the size of the swap file (for example, 1GB). You can choose different sizes and paths according to your needs.sudo fallocate -l 1G /swapfile
-
Set the correct permissions :
To ensure that only the root user can read and write the swap file, set the correct permissions.sudo chmod 600 /swapfile
-
Set the file as swap space :
sudo mkswap /swapfile
-
Enable swap file :
sudo swapon /swapfile
-
Make the swap file persistent :
To keep the swap file active across system reboots, add it to/etc/fstab
the file.echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
After completing the above steps, your system should have new swap space and you can run free -h
the command again to check the status of the swap space.
Code explanation
/swapfile none swap sw 0 0
This line /swapfile none swap sw 0 0
is /etc/fstab
an entry in the file whose purpose is to specify how to mount and manage the system's swap space. Below is an explanation of each field:
-
/swapfile
: This is the path to the swap file./swapfile
Is a special file created in the file system to expand the system's virtual memory. -
none
: This field is normally used to specify a mount point, but in this case its value isnone
because swap space is not a mountable file system. -
swap
: This field specifies the type of file system. In this case, its value isswap
, indicating that this file is a swap file. -
sw
: This field is an optional mount option.sw
Means swap space and allows administrators to activate and deactivate swap space using theswapon
andswapoff
commands. -
0
: This field is used to specify how the system backs up the file system.0
Indicates no backup. -
0
: This field is used to specify the order in which the file system is checked during system startup.0
Indicates no inspection.
Add this entry to /etc/fstab
the file and the system will automatically enable the specified swap file /swapfile
as swap space at startup. This is a way to ensure that the system maintains the swap space configuration across reboots.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
This command is used to add the newly created swap file ( /swapfile
) to the system's file system table ( /etc/fstab
) so that the system can be started every time The swap file can be automatically mounted and used as swap space. Below is an explanation of each part of the command:
-
echo
:echo
Command is used to output the specified string in the terminal. Here, the string it outputs is'/swapfile none swap sw 0 0'
. -
'
(Single quotes): Used to surround the string to be output, ensuring that the string is passed intact to the next command. -
|
(pipe symbol): This symbol is used to pass the output of the previous command (echo
) to the next command (sudo tee
). In this way,echo
the output of the command becomessudo tee
the input of the command. -
sudo
: Execute the following commands with superuser (root) permissionstee
. -
tee
The :tee
command is used to read standard input and write it to a file and standard output. Here, it willecho
write the string received from the command to a file/etc/fstab
. -
-a
: This option tellstee
the command to append the contents to the end of the file rather than overwriting the existing contents of the file. -
/etc/fstab
: This is the path to the file to be written./etc/fstab
It is the system's file system table, which contains information about how the system should mount and use various file systems and devices.
Specifically, '/swapfile none swap sw 0 0'
this string is fstab
a new record in the file, which tells the system /swapfile
that is a swap space file and should be automatically mounted and used as swap space when the system starts.