🦸‍♂️
KT's DevOps Notes
  • Hello World
  • Misc
    • Ubuntu Setup
    • Mac Os Setup
  • Database
    • Redis
      • How To Migrate Redis Data to a DigitalOcean Managed Database
      • How To Connect to a Managed Redis Instance over TLS with Stunnel and redis-cli
    • MariaDB
      • How to Setup MariaDB Galera Cluster on Ubuntu 18.04 with HAProxy
      • How to change innodb_thread_concurrency Read/Wirte Concurrency in MySQL
      • How to change innodb_write_io_threads in MySQL
      • How to change innodb_read_io_threads in MySQL
  • OCI Foundations 2020 Associate Certification | 1Z0–1085–20 | Preparation…
  • Security
  • Ubuntu
    • Apache Server Configuration
      • Configuring Let’s Encrypt SSL Cert for Apache on Ubuntu 18.04
      • Installing Apache, MySQL, PHP (LAMP) Stack on Ubuntu 18.04
      • Installing Apache on Ubuntu 18.04 with Multiple Domains
    • Nginx Server Configuration
      • Robots.txt disallow all with nginx
      • Installing Nginx, MySQL, PHP (LEMP) Stack on Ubuntu 18.04
      • Installing Nginx on Ubuntu 18.04 with Multiple Domains
      • Configuring Let’s Encrypt SSL Cert for Nginx on Ubuntu 18.04
    • Creating Swap Space on Ubuntu 18.04
    • Dell Inspiron 7559 Ubuntu Linux Guide
    • How to configure Postfix to use Gmail SMTP on Ubuntu 18.04 & 16.04 | LeetDev
  • Setting up SSL Certificates for HAProxy with certbot
  • PHP
  • PHP / Apache: Set Max File Upload and Post Size | LeetDev
  • Upgrade centos7 openssh & openssl to the latest version
  • Downgrading to php7.3 on Amazon Linux 2
Powered by GitBook
On this page
  • 1. Create a Swap File
  • 2. Configure Swap File
  • 3. Make it Persistent
  • 4. Some Final Tweaks

Was this helpful?

  1. Ubuntu

Creating Swap Space on Ubuntu 18.04

Of course, we do not recommend you use swap space as a replacement to physical memory, especially on cloud hosting. If you are continuously eating into swap space, you should upgrade your physical memory. Swap should really only be a fall back in case memory usage peaks abnormally.

First check if the system has any swap configured.

swapon --show

If the output is blank, there is no swap configured so we can continue with this guide.

1. Create a Swap File

We will use the fallocate program to create a swap file. Best practice is to create a swap file double the amount of your RAM. If you have 1024MB of RAM, create a 2GB swap file.

sudo fallocate -l 2G /swapfile

Now check if the file was created.

ls -lh /swapfile

If it was created correctly, you should see something like:

-rw-r--r-- 1 root root 2.0G Aug 3 18:59 /swapfile

2. Configure Swap File

Make the swap file only accessible to root.

sudo chmod 600 /swapfile

Mark the file as a swap file.

sudo mkswap /swapfile

If it was successful, you should see something like

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=00aafaee-51c9-46b3-a0fc-8240c134048e

Finally we will tell the system to start using our new swap file,

sudo swapon /swapfile

To verify that the swap is now available type:

sudo swapon --show

Result:

NAME      TYPE  SIZE USED PRIO
/swapfile file  2G   0B   -2

We can also run the following to see our new swap file alongside physical memory

free -h

Result:

              total        used        free      shared  buff/cache   available
Mem:           985M        418M        104M         26M        462M        392M
Swap:          2.0G          0B        2.0G

3. Make it Persistent

This swap will only last until next reboot. In order to make it permanent, we will add it to the /etc/fstab file.

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

4. Some Final Tweaks

For a server, you should change the swappiness value to 10.

sudo sysctl vm.swappiness=10

Now change the vfs_cache_pressure value to 50.

sudo sysctl vm.vfs_cache_pressure=50

To make these two settings persist after next reboot, edit the following file:

sudo nano /etc/sysctl.conf

Add this to the bottom.

/etc/sysctl.conf

vm.swappiness=10
vm.vfs_cache_pressure=50

Save file and exit. (Press CTRL + X, press Y and then press ENTER).

If you can, reboot the server with sudo reboot and run sudo swapon --show just to make sure the swap space was created automatically on startup.

A useful way to keep an eye on your swap usage and system resources is to run htop.

PreviousConfiguring Let’s Encrypt SSL Cert for Nginx on Ubuntu 18.04NextDell Inspiron 7559 Ubuntu Linux Guide

Last updated 5 years ago

Was this helpful?