Do you use let's encrypt?





3936 votes ~ 17 comments

 

Enabling swap on debian based system

Posted by tuwid on Tue 8 Dec 2015 at 18:01

Tags:

Recently I found myself without a swap partition (having installed another OS and reworked my disk-setup) so I wanted to share a quick guide to enable swap again.

As a recap "swap" space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM.

You can check to see if you have swap partitions defined by consulting /etc/fstab: $ grep swap /etc/fstab UUID=14a0f7b9-dabb-4296-b0e7-013527a7d82d none swap sw 0 0

If one is defined you can check that it is being used by the system:

$ swapon -s
Filename Type Size Used Priority
/dev/sda6 partition 1004020 215532 -1

If one is defined but not in use the obvious thing to do is check that it is formatted as a swap partition:

$ sudo fdisk -l /dev/sda
[..snipped..]
/dev/sda5 3842 6595 22121473+ 83 Linux
/dev/sda6 6596 6720 1004031 82 Linux swap / Solaris

If it is a swap partition, ready it for use and turn it on. At this point, you might see any errors that prevented its use.

su -
mkswap /dev/sda6
swapon /dev/sda6

You can now check that the partition is being used using the "swapon -s" command we saw above.

Add an entry to /etc/fstab to have this swap partition loaded at bootup. You can replace the "UUID=xxx" part from above with "/dev/sda6"; so the entry would look like this:

/dev/sda6 none swap sw 0 0

If you didn't define a swap partition earlier, then you'd have to create one, or point swap to a regular file (which is a little less efficient, but probably sufficient).

 

 


Re: Enabling swap on debian based system
Posted by Steve (217.155.xx.xx) on Tue 8 Dec 2015 at 18:05
[ View Steve's Scratchpad | View Weblogs ]

For what it is worth I tend to find using a static partition for swap is wasteful - so I tend to configure my systems with LVM, and allocate a swap partition in that.

If I'm not using LVM then a simple file is sufficient, since a system actually using swap is probably already broken. This is how I'd set it up:

$ su -
# dd if=/dev/zero of=/swap.file bs=1024k count=1
# mkswap -f /swap.file
# echo "/swap.file none swap sw 0 0 " >> /etc/fstab
# swapon -a

--
Steve

[ Parent | Reply to this comment ]

Re: Enabling swap on debian based system
Posted by Anonymous (72.22.xx.xx) on Wed 9 Dec 2015 at 18:14
On many systems, I literally have more than enough RAM. Isn't that a nice state of being?

But just in case of... something, I might want swapspace some day.

Usually that means a 1GB swap file, as in Steve's example, but sometimes I want system uptime more than I want performance:

# apt-get install swapspace

will install a daemon that will dynamically create, mount, and remove swapspace.

[ Parent | Reply to this comment ]

Re: Enabling swap on debian based system
Posted by ntropia (137.131.xx.xx) on Wed 9 Dec 2015 at 19:36

I didn't know about this daemon, and in principle it sounds nice.

Although, the size-variable file makes feels like we're getting back to Windows and its disk-fragmented nightmare. I know that any decent filesystem available on Linux tend to have negligible fragmentation, but isn't the whole point of a partition to keep the swap separated by the other filesystems?

@Steve: kinda of a silly question, but what's the advantage of a swap partition in LVM?

[ Parent | Reply to this comment ]

Re: Enabling swap on debian based system
Posted by jchris (91.55.xx.xx) on Sat 23 Apr 2016 at 09:23
A system actually using swap is already broken? I beg to differ, because swap can be useful: swap is a safety belt on servers which might e.g. be 'slashdotted' for some reason, and, last but not least, you need swap to hibernate your system. And I prefer hibernation over "suspend to ram" because it tends to survive power outages (e.g. low battery on laptops) much better.

[ Parent | Reply to this comment ]

Re: Enabling swap on debian based system
Posted by naoliv (200.145.xx.xx) on Thu 10 Dec 2015 at 12:53
[ View Weblogs ]

[ Parent | Reply to this comment ]

Re: Enabling swap on debian based system
Posted by Anonymous (173.3.xx.xx) on Tue 29 Dec 2015 at 02:14
Thanks for the article. I found it useful. One small suggestion.

The manual of swapon says -s is deprecated in favour of --show as the latter provides better control on output data. It might be a good idea update the article to swapon --show?

[ Parent | Reply to this comment ]