Linux Swap and Swapfile: Difference between revisions

From WikiMLT
m (Spas moved page Swapfile to Linux Swap and Swapfile without leaving a redirect)
mNo edit summary
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
<noinclude><!--[[Category:Linux_Server|?]]-->{{ContentArticleHeader/Linux_Server}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Desktop|toc=off|toc-limit=2}}{{ContentArticleHeader/Linux_Server|toc-limit=2}}</noinclude>


== Swapfile Конфигуриране ==
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
 
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases when running Linux on a virtual machine (VPS) a swap partition is not present so the only option is to create a swap file.
 
== Add/Remove Swap file Short Guide ==
 
=== Swap On ===
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
sudo fallocate -l 4G /swapfile
sudo fallocate -l 4G /swapfile
Line 7: Line 13:
sudo mkswap /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon /swapfile
</syntaxhighlight><syntaxhighlight lang="shell" line="1">
sudo nano /etc/fstab
sudo nano /etc/fstab
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="bash" start="3">
/swapfile swap swap defaults 0 0
/swapfile swap swap defaults 0 0
</syntaxhighlight>Проверка:<syntaxhighlight lang="shell" line="1">
</syntaxhighlight>
If <code>fallocate</code> is not installed or you get an error message saying <code>fallocate failed: Operation not supported</code> then use the following command to create the swap file:
<syntaxhighlight lang="shell" line="1" class="mlw-shell-gray">
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
</syntaxhighlight>
=== Swap Off ===
<syntaxhighlight lang="shell" line="1">
sudo swapoff -a    # sudo swapoff /swapfile
sudo rm -f /swapfile
sudo nano /etc/fstab
</syntaxhighlight>
<syntaxhighlight lang="bash" start="3">
#/swapfile swap swap defaults 0 0
</syntaxhighlight>
 
=== Check the swap and its usage ===
<syntaxhighlight lang="shell" line="1">
sudo swapon --show
sudo free -h
</syntaxhighlight>
 
=== Swap On/Off Scripts ===
<syntaxhighlight lang="shell" class="code-continue">
swap-on.sh
</syntaxhighlight>
<syntaxhighlight lang="bash" start="1" line="1">
#!/bin/bash
 
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo sed 's_^#/swapfile_/swapfile_' /etc/fstab -i
 
echo
sudo swapon --show
echo
sudo free -h
</syntaxhighlight>
<syntaxhighlight lang="shell" class="code-continue">
swap-off.sh
</syntaxhighlight>
<syntaxhighlight lang="bash" start="1" line="1">
#!/bin/bash
 
sudo swapoff /swapfile
sudo rm -f /swapfile
sudo sed 's_^/swapfile_#/swapfile_' /etc/fstab -i
 
echo
sudo swapon --show
sudo swapon --show
echo
sudo free -h
sudo free -h
</syntaxhighlight>
</syntaxhighlight>
== Add/Remove Swap file Short Guide ==
Here is used the entire small (virtual desk) <code>/dev/sde</code> as swap, but you can use a partition...<syntaxhighlight lang="shell" class="code-continue" line="1">
sudo mkswap /dev/sde
</syntaxhighlight>
<syntaxhighlight lang="shell-session" start="1">
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=ad5aad68-f8ec-48be-8025-ccd3bd112e2e
</syntaxhighlight><syntaxhighlight lang="shell" class="code-continue" line="1">
sudo blkid /dev/sde
</syntaxhighlight>
<syntaxhighlight lang="shell-session" start="1">
/dev/sde: UUID="ad5aad68-f8ec-48be-8025-ccd3bd112e2e" TYPE="swap"
</syntaxhighlight><syntaxhighlight lang="shell" class="code-continue" line="1">
sudo swapon /etc/fstab
swapon --show
</syntaxhighlight>
<syntaxhighlight lang="shell-session" start="1">
NAME    TYPE      SIZE USED PRIO
/dev/sdb partition  4G  0B  -2
</syntaxhighlight><syntaxhighlight lang="shell" class="code-continue" line="1">
sudo nano /etc/fstab
</syntaxhighlight>
<syntaxhighlight lang="bash" start="1">
#/swapfile none swap sw 0 0
UUID=ad5aad68-f8ec-48be-8025-ccd3bd112e2e    none    swap    sw      0  0
</syntaxhighlight>
== Adjusting the Swappiness Value ==
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
<syntaxhighlight lang="shell" line="1">
cat /proc/sys/vm/swappiness
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
60
</syntaxhighlight>
While the swappiness value of 60 is OK for Desktops, for production servers you may need to set a lower value. For example, to set the swappiness value to 10, type:
<syntaxhighlight lang="shell" line="1">
sudo sysctl vm.swappiness=10
</syntaxhighlight>
To make this parameter persistent across reboots append the following line to the <code>/etc/sysctl.conf</code> file:
<syntaxhighlight lang="shell" line="1">
sudo nano /etc/sysctl.conf
</syntaxhighlight>
<syntaxhighlight lang="apacheconf" line="1" start="99">
vm.swappiness=10
</syntaxhighlight>
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
==References==
*Linuxsize: [https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-18-04/ How To Add Swap Space on Ubuntu 18.04]
*Ask Ubuntu: [https://askubuntu.com/q/969296/566421 How to delete a swap file in Ubuntu?]
*itBeginner.net: [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ How to tweak and optimize SSD for Ubuntu, Linux Mint]
*Ubuntu Docs: [https://help.ubuntu.com/community/SwapFaq Swap FAQ]


<noinclude>
<noinclude>
<div id='devStage'>
<div id='devStage'>
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Desktop
  | Стадий = 3
| Прндл1 = Linux Server
  | Фаза  = Разработване
  | Стадий = 6
  | Статус = Разработван
  | Фаза  = Утвърждаване
  | Статус = Утвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | РзбПт = Spas
| АвтПт  = Spas
| УтвПт = {{REVISIONUSER}}
  | ИдтДт  = 5.07.2022
  | ИдтДт  = 5.07.2022
  | РзбДт  = {{Today}}
  | РзбДт = 17.08.2022
| АвтДт  = 17.08.2022
| УтвДт = {{Today}}
  | ИдтРв  = [[Special:Permalink/27680|27680]]
  | ИдтРв  = [[Special:Permalink/27680|27680]]
  | РзбРв  = {{REVISIONID}}
  | РзбРв = [[Special:Permalink/30287|30287]]
| АвтРв  = [[Special:Permalink/30292|30292]]
| УтвРв = {{REVISIONID}}
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Latest revision as of 00:47, 1 May 2023

Swap is a space on a disk that is used when the amount of phys­i­cal RAM mem­o­ry is full. When a Lin­ux sys­tem runs out of RAM, in­ac­tive pages are moved from the RAM to the swap space.

Swap space can take the form of ei­ther a ded­i­cat­ed swap par­ti­tion or a swap file. In most cas­es when run­ning Lin­ux on a vir­tu­al ma­chine (VPS) a swap par­ti­tion is not present so the on­ly op­tion is to cre­ate a swap file.

Add/​​​Remove Swap file Short Guide

Swap On

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/fstab
/swapfile swap swap defaults 0 0

If fal­lo­cate is not in­stalled or you get an er­ror mes­sage say­ing fal­lo­cate failed: Op­er­a­tion not sup­port­ed then use the fol­low­ing com­mand to cre­ate the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Swap Off

sudo swapoff -a     # sudo swapoff /swapfile
sudo rm -f /swapfile
sudo nano /etc/fstab
#/swapfile swap swap defaults 0 0

Check the swap and its us­age

sudo swapon --show
sudo free -h

Swap On/​​​Off Scripts

swap-on.sh
#!/bin/bash

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo sed 's_^#/swapfile_/swapfile_' /etc/fstab -i

echo
sudo swapon --show
echo
sudo free -h
swap-off.sh
#!/bin/bash

sudo swapoff /swapfile
sudo rm -f /swapfile
sudo sed 's_^/swapfile_#/swapfile_' /etc/fstab -i

echo
sudo swapon --show
echo
sudo free -h

Add/​​​Remove Swap file Short Guide

Here is used the en­tire small (vir­tu­al desk) /​​​dev/​​​sde as swap, but you can use a par­ti­tion…

sudo mkswap /dev/sde
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=ad5aad68-f8ec-48be-8025-ccd3bd112e2e
sudo blkid /dev/sde
/dev/sde: UUID="ad5aad68-f8ec-48be-8025-ccd3bd112e2e" TYPE="swap"
sudo swapon /etc/fstab
swapon --show
NAME     TYPE      SIZE USED PRIO
/dev/sdb partition   4G   0B   -2
sudo nano /etc/fstab
#/swapfile	none	swap	sw	0	0
UUID=ad5aad68-f8ec-48be-8025-ccd3bd112e2e    none    swap    sw      0   0

Ad­just­ing the Swap­pi­ness Val­ue

Swap­pi­ness is a Lin­ux ker­nel prop­er­ty that de­fines how of­ten the sys­tem will use the swap space. Swap­pi­ness can have a val­ue be­tween 0 and 100. A low val­ue will make the ker­nel to try to avoid swap­ping when­ev­er pos­si­ble while a high­er val­ue will make the ker­nel to use the swap space more ag­gres­sive­ly.

The de­fault swap­pi­ness val­ue is 60. You can check the cur­rent swap­pi­ness val­ue by typ­ing the fol­low­ing com­mand:

cat /proc/sys/vm/swappiness
60

While the swap­pi­ness val­ue of 60 is OK for Desk­tops, for pro­duc­tion servers you may need to set a low­er val­ue. For ex­am­ple, to set the swap­pi­ness val­ue to 10, type:

sudo sysctl vm.swappiness=10

To make this pa­ra­me­ter per­sis­tent across re­boots ap­pend the fol­low­ing line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf
vm.swappiness=10

The op­ti­mal swap­pi­ness val­ue de­pends on your sys­tem work­load and how the mem­o­ry is be­ing used. You should ad­just this pa­ra­me­ter in small in­cre­ments to find an op­ti­mal val­ue.

Ref­er­ences