how to increase swap space in linux
TRANSCRIPT
-
8/12/2019 How to Increase Swap Space in Linux
1/3
Question:I would like to add more swap space to my Linux system. Can you explain
with clear examples on how to increase the swap space?
Answer:You can either use a dedicated hard drive partition to add new swap space,
or create a swap file on an existing filesystem and use it as swap space.
How much swap space is currently used by the system?
Free command displays the swap space. free k shows the output in !".
# free -k
total used free shared buffers
cached
Mem: 3082356 2043700 1038656 0 50976
1646268
-/ buffers/cache: 346456 2735900
!"a: 4192956 0 4192956
#wapon command with option s, displays the current swap space in !".
# s"ao$ -s
%&le$ame '(e !&)e *sed
+r&or&t(
/de,/sda2 art&t&o$ 4192956 0 -1
#wapon s, is same as the following.
# cat /roc/s"as
%&le$ame '(e !&)e *sed
+r&or&t(
/de,/sda2 art&t&o$ 4192956 0 -1
Method 1: Use a Hard Drive Partition for Additional wap pace
If you have an additional hard disk, $or space availa%le in an existing disk&, create a
partition using fdisk command. Let us assume that this partition is called 'dev'sdc(
)ow setup this newly created partition as swap area using the mkswap command as
shown %elow.
# mks"a /de,/sdc1
*na%le the swap partition for usage using swapon command as shown %elow.
# s"ao$ /de,/sdc1
+o make this swap space partition availa%le even after the re%oot, add the following
line to the 'etc'fsta% file.
# cat /etc/fstab
/de,/sdc1 s"a s"a defaults0 0
-
8/12/2019 How to Increase Swap Space in Linux
2/3
erify whether the newly created swap area is availa%le for your use.
# s"ao$ -s
%&le$ame '(e !&)e *sed
+r&or&t(
/de,/sda2 art&t&o$ 4192956 0 -1
/de,/sdc1 art&t&o$ 1048568 0 -2
# free -k
total used free shared buffers
cached
Mem: 3082356 3022364 59992 0 52056
2646472
-/ buffers/cache: 323836 2758520
!"a: 5241524 0 5241524
!ote:In the output of swapon s command, the +ype column will say -partition if
the swap space is created from a disk partition.
Method ": Use a #ile for Additional wap pace
If you don/t have any additional disks, you can create a file somewhere on your
filesystem, and use that file for swap space.
+he following dd command example creates a swap file with the name -myswapfile
under 'root directory with a si0e of (1234" $(5"&.
# dd &f/de,/)ero of/root/m(s"af&le bs1M cou$t1024
10240 records &$
10240 records out
# ls -l /root/m(s"af&le
-r"-r--r-- 1 root root 1073741824 .u 14 23:47
/root/m(s"af&le
Change the permission of the swap file so that only root can access it.
# chmod 600 /root/m(s"af&le
4ake this file as a swap file using mkswap command.
# mks"a /root/m(s"af&le!ett&$ u s"asace ,ers&o$ 1 s&)e 1073737 k
*na%le the newly created swapfile.
# s"ao$ /root/m(s"af&le
+o make this swap file availa%le as a swap area even after the re%oot, add the
following line to the 'etc'fsta% file.
# cat /etc/fstab
/root/m(s"af&le s"a s"adefaults 0 0
-
8/12/2019 How to Increase Swap Space in Linux
3/3
erify whether the newly created swap area is availa%le for your use.
# s"ao$ -s
%&le$ame '(e !&)e *sed
+r&or&t(
/de,/sda2 art&t&o$ 4192956 0 -1
/root/m(s"af&le f&le 1048568 0 -2
# free -k
total used free shared buffers
cached
Mem: 3082356 3022364 59992 0 52056
2646472
-/ buffers/cache: 323836 2758520
!"a: 5241524 0 5241524
!ote:In the output of swapon s command, the +ype column will say -file if the
swap space is created from a swap file.
If you don/t want to re%oot to verify whether the system takes all the swap space
mentioned in the 'etc'fsta%, you can do the following, which will disa%le and ena%le
all the swap partition mentioned in the 'etc'fsta%
# s"aoff -a
# s"ao$ -a