swap file create linux

How To Create Swap file On Linux

Swap is a space on disk that would be used when the psychical amount of memory is full. Let’s see, how to create swap file centos and you can also use free -m command to check swap size in Linux.

This is slower than physical ram because it’s created from the disk. The size of swap must be almost double or equal to the physical ram. You can either create a swap partition or swap file in Linux.

There’re two ways to create a file.

  1. Use fallocate command
  2. Use DD command

Check Out: How To Install Memcached Memory Caching In Linux

Create a file Using fallocate

fallocate -l 1G /swfile

or

dd if=/dev/zero of=/swfile bs=1024 count=10048576

how to create swap file

count = size of a file

Check Out: How To Display Timestamp In History Command On Linux

Now change the permission the only root should be accessible the file.

chmod 600 /swfile

Run the below command to create a swap.

mkswap /swfile

linux swap size

Make an entry in /etc/fstab and save the file.

/swfile     swap    swap      defaults    0    0

command to check swap

Check Out: How To Set Date and Time In Linux Using Command Line

Now run mount -a to check whether the entry is correct or not.

create swap file centos

you can check swap partition size using the below commands and also use cat /proc/swaps

swapon -s

or

swapon –show

Check Out: Launch EC2 Instance On Amazon Cloud Using Private subnets

Now you have to change the value of vm.swapiness in /etc/sysctl.conf.

sysctl vm.swappiness=30

Command is to check swap cat /proc/sys/vm/swappiness

echo ‘vm.swappiness=30’ >> /etc/sysctl.conf

The default value is 30 or 60 and you can put these vm.swapiness =20 
line in sysctl.conf file and run sysctl -p

Check Out: How To Delete Backup AMI EC2 On Amazon Cloud

The values are between 0 to 100 and if you set is 0 means swap file will only be used when the system has fully utilized the psychical memory.

You’re done Linux swap file size command to check centos linux create framework 4.7

LEAVE A REPLY

Please enter your comment!
Please enter your name here