Make Swap on Linux Machines
Posted By : Prakhar Budholiya | 19-Jun-2015
How to make swap on Linux?
There are two types of Swap on linux machines
- Swap Partition
- Swap file
- Swap Partition-
We can make swap as a partition during installation or after installation as follows-
Firstly check your hard drive by
# cat /proc/partitions
Results will be like sda/sdb or etc
Then go to partition table (only SuperUser can access this)
# parted /dev/sda
*print free (check the free space on disk)
*mkpart
(make a new Partition of your choice, it can be both primary or logical )
*File system type = linux-swap
*quit
# partprobe or reboot (update the partition table)
# mkswap /dev/sdax (x= partition no. , ex.- /dev/sda3)
Now mount it permanently on fstab
# vim /etc /fstab
Here Give
File system- ex. /dev/sdax, mount point- swap, type- swap, options- sw, dump- 0, pass- -1 or 2
# swapon -a (mount the Swap)
# swapon -s (check the swap)
This whole partition is used by swap only no user can access it or place their stuff in it.
- Swap file-
When there is no space left for partitioning on hard drive we will extract a dump file from any partition and will make that as a swap.
First check all the partitions with most free space by
# df -h
Now create a Dump Directory from that partition of your choice.
# dd if=/dev/zero of=/newswap bs=10M count=200
(Here we are extracting a dumb file of 2000Mb from / , bs stands for block size.)
# du -sh /newswap (to check the size of new swap)
# mkswap /newswap (formate the swap file in swap format)
# blkid /newswap (verify that swap file is formatted in swap format or not)
Now go to fstab and mount it the same way, we have mounted earlier in /etc/fstab.
File system- ex. /newswap, mount point- swap, type- swap, options- sw, dump- 0, pass- -1 or 2
#swapon -a
#swapon -s
* Now verify your swap with # free -m
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prakhar Budholiya
Prakhar is a linux administrator and well versed with linux and networking. he loves horse riding and is an athlete.