Make Swap File in Ubuntu

1. Check for swap

root@px6:~# sudo swapon -s
Filename                Type        Size    Used    Priority

2. Check File System

root@px6:/etc/kernel# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       20511356 2134860  17311540  11% /
none                   4       0         4   0% /sys/fs/cgroup
udev              240060       4    240056   1% /dev
tmpfs              50184     332     49852   1% /run
none                5120       0      5120   0% /run/lock
none              250904       0    250904   0% /run/shm
none              102400       0    102400   0% /run/user

4. Create and enable swap file

(i) create swap file
root@px6:/etc/kernel# sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
262144+0 records in
262144+0 records out
268435456 bytes (268 MB) copied, 0.981026 s, 274 MB/s

(ii) prepare swap file
root@px6:/etc/kernel# sudo mkswap /swapfile
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=40db0b75-8ff2-456e-9763-a879601eb9eb

(iii) enable swap file

root@px6:/etc/kernel# sudo swapon /swapfile

5. Check again for swap
root@px6:/etc/kernel# swapon -s
Filename                Type        Size    Used    Priority
/swapfile                               file        262140    0    -1

6.This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.

Open up the file:

sudo vi /etc/fstab

Paste in the following line:

  /swapfile       none    swap    sw      0       0 

7.Swappiness in the file should be set to 10.

root@px6:/etc/kernel# echo 10 | sudo tee /proc/sys/vm/swappiness

10

root@px6:/etc/kernel# echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
vm.swappiness = 10

8.give root permission to swap file

root@px6:/etc/kernel# sudo chown root:root /swapfile
root@px6:/etc/kernel# sudo chmod 0600 /swapfile

 

Comments