Add Ubuntu swap area

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:

  1. Create an empty file :
    Run the following command as root, where /swapfileis the path to the swap file and 1Gis 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
    
  2. 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
    
  3. Set the file as swap space :

    sudo mkswap /swapfile
    
  4. Enable swap file :

    sudo swapon /swapfile
    
  5. Make the swap file persistent :
    To keep the swap file active across system reboots, add it to /etc/fstabthe 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 -hthe 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 0is /etc/fstaban 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:

  1. /swapfile: This is the path to the swap file. /swapfileIs a special file created in the file system to expand the system's virtual memory.

  2. none: This field is normally used to specify a mount point, but in this case its value is nonebecause swap space is not a mountable file system.

  3. swap: This field specifies the type of file system. In this case, its value is swap, indicating that this file is a swap file.

  4. sw: This field is an optional mount option. swMeans swap space and allows administrators to activate and deactivate swap space using the swaponand swapoffcommands.

  5. 0: This field is used to specify how the system backs up the file system. 0Indicates no backup.

  6. 0: This field is used to specify the order in which the file system is checked during system startup. 0Indicates no inspection.

Add this entry to /etc/fstabthe file and the system will automatically enable the specified swap file /swapfileas 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:

  1. echo: echoCommand is used to output the specified string in the terminal. Here, the string it outputs is '/swapfile none swap sw 0 0'.

  2. '(Single quotes): Used to surround the string to be output, ensuring that the string is passed intact to the next command.

  3. |(pipe symbol): This symbol is used to pass the output of the previous command ( echo) to the next command ( sudo tee). In this way, echothe output of the command becomes sudo teethe input of the command.

  4. sudo: Execute the following commands with superuser (root) permissions tee.

  5. teeThe : teecommand is used to read standard input and write it to a file and standard output. Here, it will echowrite the string received from the command to a file /etc/fstab.

  6. -a: This option tells teethe command to append the contents to the end of the file rather than overwriting the existing contents of the file.

  7. /etc/fstab: This is the path to the file to be written. /etc/fstabIt 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 fstaba new record in the file, which tells the system /swapfilethat is a swap space file and should be automatically mounted and used as swap space when the system starts.

Guess you like

Origin blog.csdn.net/m0_57236802/article/details/133466826