Support >
  About independent server >
  Method of configuring Swap (to swap partitions or files)
Method of configuring Swap (to swap partitions or files)
Time : 2024-10-30 14:51:39
Edit : Jtti

Swap is a dedicated space on the disk that is used when the physical RAM is used up. The Linux system runs out of RAM, and inactive pages are moved from RAM to swap space. Switching usage can be delayed, so it is typical for a swap configuration to be about 50% of the available memory. In a restricted environment, you are advised to configure 1GB of Swap space. Perform the following operations to configure SWAP:

Use the 'dd' command to create a file of the specified size. To create a Swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 # Create a Swap file of 1GB

` ` `

Ensure that the permissions on the Swap file are correct. Set permissions:

   sudo chmod 600 /swapfile

` ` `

Format using the 'mkswap' command. Format the Swap file:

   sudo mkswap /swapfile

` ` `

Use the 'swapon' command to enable the Swap file. To enable Swap files:

   sudo swapon /swapfile

` ` `

Edit the '/etc/fstab' file and add the Swap file configuration. To set up startup:

   echo '/swapfile none swap defaults 0 0' >> /etc/fstab

` ` `

Create a Swap partition: Use the 'fdisk' or 'parted' command to create a new partition on the disk.

   

sudo fdisk /dev/sdb # Assume that /dev/sdb is the target disk

` ` `

Create a new partition at the 'fdisk' prompt and set the partition type to 'Linux swap/Solaris' (type code 82).

Use the mkswap command to format the new partition:

sudo mkswap /dev/sdb1 # Assume that the new partition is /dev/sdb1

` ` `

Use the 'swapon' command to enable the Swap partition.

   sudo swapon /dev/sdb1

` ` `

Edit the '/etc/fstab' file, add Swap partition configuration, and set the startup:

   echo '/dev/sdb1 none swap defaults 0 0' >> /etc/fstab

` ` `

Adjust the Swap usage policy, such as viewing and setting the swappiness value: The swappiness value determines the tendency of the system to use Swap. Check the current 'swappiness' value:

     cat /proc/sys/vm/swappiness

` ` `

Temporarily set the 'swappiness' value:

     echo 10 > /proc/sys/vm/swappiness

` ` `

Set 'swappiness' permanently: edit '/etc/sysctl.conf' file and add 'vm.swappiness=10'.

     

     sudo sysctl -p

` ` `

After the Swap space is configured, the swap space can be configured to improve the stability and response speed of the linux system when the physical memory is insufficient.

JTTI-Defl
JTTI-COCO
JTTI-Selina
JTTI-Ellis
JTTI-Eom