• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, May 18, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How To Create a Linux Swap File

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
13
VIEWS
Share on FacebookShare on Twitter

Contents

  1. How to Create and Enable Swap in Linux

In this article, we will explain swap space, and learn how to create swap space using a swap file in Linux: this is important in case we don’t have a swap partition created on the hard disk.

Swap space/partition is space on a disk created for use by the operating system when memory has been fully utilized. It can be used as virtual memory for the system; it can either be a partition or a file on a disk.

When the kernel runs out of memory, it can move idle/inactive processes into swap creating room for active processes in the working memory. This is memory management that involves swapping sections of memory to and from virtual memory.

Suggested Read: 8 Useful Commands to Monitor Swap Space Usage in Linux

With that said, below are the steps we can follow to create a swap space using a file.

How to Create and Enable Swap in Linux

1. In this example, we will create a swap file of size 2GB using the dd command as follows. Note that bs=1024 means read and write up to 1024 bytes at a time and count = (1024 x 2048)MB size of the file.

# dd if=/dev/zero of=/mnt/swapfile bs=1024 count=2097152

Alternatively, use the fallocate command as follows.

# fallocate --length 2GiB /mnt/swapfile

And then set the appropriate permissions on the file; make it readable only by root user as follows.

# chmod 600 /mnt/swapfile

2. Now setup the file for swap space with the mkwap command.

# mkswap /mnt/swapfile

3. Next, enable the swap file and add it to the system as a swap file.

# swapon /mnt/swapfile

4. Afterwards, enable the swap file to be mounted at boot time. Edit the /etc/fstab file and add the following line in it.

/mnt/swapfile swap swap defaults 0 0

In the line above, each field means:

  • /mnt/swapfile – device/file name
  • swap – defines device mount point
  • swap – specifies the file-system type
  • defaults – describes the mount options
  • 0 – specifies the option to be used by the dump program
  • 0 – specifies the fsck command option

6. To set how often the swap file can be used by the kernel, open the /etc/sysctl.conf file and add the line below.

Note that the default value of how frequent swap space can be used is 60 (maximum value is 100). The higher the number, the more frequent swap space utilization by the kernel. When the value is set to 0, the swap file will only be used if the operating system has fully utilized memory.

vm.swappiness=10

6. Now verify the swap file was created using the swapon command.

# swapon  -s
OR
# free
OR
# cat  /proc/swaps
How To Create a Linux Swap File

Check Swap Space in Linux

We can optionally reboot the system to effect the above changes using the following command.

# reboot

Remember to also read through these useful Linux memory management guides:

  1. How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
  2. 10 ‘free’ Commands to Check Memory Usage in Linux
  3. Smem – Reports Memory Consumption Per-Process and Per-User Basis in Linux
  4. Find Top Running Processes by Highest Memory and CPU Usage in Linux

That’s It! If you have any issues, use the feedback form below to send us any questions or important additional ideas to this topic.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

3 Ways to List All Installed Packages in RHEL, CentOS and Fedora

Next Post

20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management

Next Post

20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet