How to add swap space on Ubuntu 20.04

This article was first published in: https://www.itcoder.tech/posts/how-to-add-swap-space-on-ubuntu-20-04/

Swap space is a space on the hard disk that is urgently requisitioned when the physical RAM memory is used up. When the RAM memory of a Linux system is exhausted, inactive memory pages will be moved to swap space.

The swap space can be an independent swap partition or a swap file. Typically, when running an Ubuntu on a virtual machine, there is no swap partition, and the only option is to create a swap file.

This tutorial explains how to add a swap file on Ubuntu 20.04.

1. Before you start##

The swap partition should not be used as a substitute for physical memory. Because the swap partition is a part of the hard drive, it has a slower access time than physical memory. If your system runs out of memory frequently, you should add more memory.

Generally, the swap file size depends on how much RAM your system has:

Only root or other users with sudo privileges can activate the swap file.

Second, create a swap file##

In this example, we create a 2 GB swap file. If you want to add more swap files, replace 2G with the size of the swap space you need to set.

Complete the following steps to add swap space on Ubuntu 20.04:

  1. First, create a file to be used as a swap file:
sudo fallocate -l 2G /swapfile

If the fallocate tool is not available on your system, or you get a message: fallocate failed: Operation not supported, use the following command to create the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
  1. Set file permissions to 600 to prevent regular users from reading and writing this file:
sudo chmod 600/swapfile
  1. Create a Linux swap area on this file:
sudo mkswap /swapfile

The output is as follows:

Setting up swapspace version 1, size =2GiB(2147479552 bytes)
no label, UUID=fde7d2c8-06ea-400a-9027-fd731d8ab4c8
  1. Activate the swap area by running the following command:
sudo swapon /swapfile

To persist, open the /etc/fstab file:

sudo nano /etc/fstab

And paste the following line:

/swapfile swap swap defaults 00
  1. Use the swapon or free command to verify whether the swap area is activated, like the following:
sudo swapon --show
NAME      TYPE      SIZE  USED PRIO
/swapfile file        2G    0B   -1
sudo free -h
    total        used        free      shared  buff/cache   available
Mem:          981Mi        97Mi        68Mi       0.0Ki       814Mi       735Mi
Swap:2.0Gi        10Mi       1.9Gi

Third, adjust the swappiness value##

Swappiness is a Linux kernel attribute used to define how frequently the system will use the swap partition. It ranges from 0 to 100. A lower value can avoid swap when possible, and a higher value will make the kernel use the swap partition more frequently.

On Ubuntu, the default Swappiness value is set to 60. You can check this current value by typing the following command:

cat /proc/sys/vm/swappiness

The output is as follows:

60

Swappiness of 60 is suitable for most Linux operating systems. For production servers, you need to set this value to a lower value.

For example, to change the Swappiness value to 10, run:

sudo sysctl vm.swappiness=10

To make this parameter persistent and still work when restarting, append the following to the /etc/sysctl.conf file:

vm.swappiness=10

The optimal swappiness depends on your system and how the memory is used. You need to adjust this value in small steps to find the most suitable value.

Fourth, remove a swap file##

To deactivate and delete the swap file, follow the steps below:

  1. First, deactivate the swap space:
sudo swapoff -v /swapfile
  1. Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

  2. Finally, use the rm command to delete the actual swap file:

sudo rm /swapfile

Five, summary##

We have shown you how to create a swap file, activate, and configure swap space on your Ubuntu 20.04 system.

If you have any questions, please contact us in the following ways:

WeChat: sn0wdr1am86

WeChat group: add the above WeChat, remark the WeChat group

QQ: 3217680847

QQ Group: 82695646

Recommended Posts

How to add swap space on Ubuntu 20.04
Explain how to add swap partition on Ubuntu 16.04
How to add swap on CentOS 7
How to add swap partition in Ubuntu
How to increase swap space on CentOS 8
How to add Apt software source on Ubuntu
How to add logging module to Nginx on Ubuntu 16.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to deploy Django on Ubuntu 14.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to secure Nginx on Ubuntu 14.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to uninstall software on Ubuntu
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04