Situation: increase swap size (/dev/sda3) greater than Ram (8 GB) when HD 128 GB
Motivation: 8 GB RAM is too little; 30 GB free space in my SSD; I want to turn 20 GB to SSD swap
Characteristics of system

  • Swap non-immutable/changeable. I cannot find any evidence why /mnt/.swapfile should be immutable so you do not need the change the file attributes of the swapfile

    sudo lsattr /mnt/.swapfile 
    -------------e-- /mnt/.swapfile
  • Command sudo fdisk -lu /dev/sda gives

    Disk /dev/sda: 113 GiB, 121332826112 bytes, 236978176 sectors 
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 082F85CA-EE3E-479C-8244-858B196FA5BA
    Device         Start       End   Sectors   Size Type
    /dev/sda1       2048      4095      2048     1M BIOS boot
    /dev/sda2       4096 220323839 220319744 105.1G Linux filesystem
    /dev/sda3  220323840 236976127  16652288     8G Linux swap
  • Command df -h gives

    Filesystem      Size  Used Avail Use% Mounted on
    udev            3.9G     0  3.9G   0% /dev
    tmpfs           793M  9.4M  784M   2% /run
    /dev/sda2       104G   74G   25G  75% /
    tmpfs           3.9G   54M  3.9G   2% /dev/shm
    tmpfs           5.0M  4.0K  5.0M   1% /run/lock
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    tmpfs           793M   64K  793M   1% /run/user/1000
  • Allocate more disk space for Swap in /dev/sda3.

My unsuccessful workflow for the task when HD and Swap on the same partition, /dev/sda3

masi@masi:~$ sudo -i

root@masi:~# swapoff /dev/sda3

root@masi:~# swapon

root@masi:~# dd if=/dev/zero of=/dev/sda3 bs=20480 count=1M
dd: error writing '/dev/sda3': No space left on device
416308+0 records in
416307+0 records out
8525971456 bytes (8.5 GB, 7.9 GiB) copied, 18.7633 s, 454 MB/s

root@masi:~# mkswap /dev/sda3
Setting up swapspace version 1, size = 8 GiB (8525967360 bytes)
no label, UUID=245cb42c-1d4e-4e21-b544-16b64af962d6

root@masi:~# swapon -p 99 /dev/sda3

root@masi:~# swapon
/dev/sda3  partition   8G   0B   99

root@masi:~# vi /etc/fstab 

HD and Swap on same Partition - Current Workflow [Ijaz, cas, FarazX]

Merging. Use fallocate at the beginning instead dd because no need to put zeros

masi@masi:~$ sudo fallocate -l 20G /mnt/.swapfile

masi@masi:~$ sudo mkswap /mnt/.swapfile 
Setting up swapspace version 1, size = 20 GiB (21474832384 bytes)
no label, UUID=45df9e48-1760-47e8-84d7-7a14f56bbd72

masi@masi:~$ sudo swapon /mnt/.swapfile
swapon: /mnt/.swapfile: insecure permissions 0644, 0600 suggested.

masi@masi:~$ sudo chmod 600 /mnt/.swapfile

masi@masi:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7925        1494         175         196        6255        5892
Swap:         28610           0       28610

Add the following line in your /etc/fstab which is better than adding the thing to your runlevels (/etc/rc.local), where I put the swapfile to the /mnt/.swapfile to maintain Linux/Unix philosophy and maintain the integrity of my system backup scripts; If swapping to an SSD, use the discard option so that the blocks are trimmed on every reboot, so not sw

# http://unix.stackexchange.com/a/298212/16920
# http://unix.stackexchange.com/a/298543/16920

# If swap is on SSD, trim blocks each time at startup.
/mnt/.swapfile  none    swap    defaults,discard      0        0

# If swap on External HDD, just use sw.
#/media/masi/SamiWeek/.swapfile  none    swap    sw      0        0


  1. How to increase swap space? https://askubuntu.com/a/178726/25388 General discussion about increasing swap space for beginners.
  2. Linux Partition HOWTO for HDDs, not SSDs: 4. Partitioning requirements. http://www.tldp.org/HOWTO/Partition/requirements.html So do not put your swap to outer tracks on SSDs but use defaults,discard options to trim your blocks as proposed by @cas.

System: Linux Ubuntu 16.04 64 bit
Linux kernel: 4.6
Linux modules: wl
Hardware: Macbook Air 2013-mid
Ram: 8 GB
SSD: 128 GB

  • Output of fdisk -lu /dev/sda? Jul 25, 2016 at 15:02
  • Options are shrinking fs on /dev/sda2 and repartition (but that can't be done from the live system as you can't unmount /dev/sda2 as that's the root filesystem: boot on some livecd is the easiest), or use a swap file as opposed to block device, or use compressed swap or compressed ram Jul 25, 2016 at 15:07
  • 1
    Few of those placement considerations apply to SSD:s, as they don't have heads or tracks like spinning disks. Striping over multiple drives might apply, if you have a huge I/O load.
    – ilkkachu
    Jul 25, 2016 at 20:40
  • 1
    @Masi Hey mate, I was working with my FreeBSD system and something sprang to my mind, we have a command called chflags on BSD and OS X, and we have a command on linux called chattr, you can make your files and directories more and more secure and immutable by setting some attributes, here is a guide on tecmint.com, I hope this can help.
    – user172564
    Jul 28, 2016 at 9:18

3 Answers 3


You just want to increase the swap size on your system using the space from sda2. Your sda2

/dev/sda2       104G   74G   25G  75%  / 

You can add additional swap space to your system by using swap file created on / that will utilize your sda2. Just do:

dd if=/dev/zero of=/swapfile bs=20480 count=1M

and then do:

sudo mkswap /swapfile  
sudo swapon /swapfile 

and check, you swap space will increase by that amount using free -m

and yes , to enable it at boot time add the entry in /etc/fstab

 /swapfile     none     swap     sw     0     0
  • 3
    Yep, you should be able to compute. Swapper should see a large virtual memory.
    – Lmwangi
    Jul 25, 2016 at 18:02
  • 3
    @masi if you add an entry in /etc/fstab you can avoid the entries in rc.local. Put the swap file somewhere like /mnt/.swap and you'll not need to worry about it too much any more Jul 25, 2016 at 18:50
  1. Memory management

    • To display swap usage summary by your device:

      $ swapon -s
    • To displays the amount of your free and used physical and swap memory:

      $ free -h
    • To preallocate space to /swapfile, you can use the line below:

      $ fallocate -l 20G /swapfile
      % OR
      $ dd if=/dev/zero of=/swapfile bs=20480 count=1M
  2. Change permission and create/activate swap

    $ chmod 600 /swapfile
    $ mkswap /swapfile
    $ swapon /swapfile

    You can also improve your file security by changing your file attributes using chattr. I recommend you read its manual page, or read this guide on Tecmint website.

  3. Verify it is enabled by viewing the output of the command cat /proc/swaps, use free command, or

    $ swapon -s
  4. To enable it at boot time, edit /etc/fstab (static information about the file system) to include the following where the fields are fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq and fs_passno, about which you can read by man 5 fstab:

    $ vi /etc/fstab
    /swapfile     none     swap     defaults,discard     0     0
    • In the fourt parameter fs_mntops, do not use just sw when swapping to an SSD but defaults,discard such that the memory blocks are trimmed each time at startup, see the answer here about How to Do Error-trapping and Swapoff if Error/Warning? The discard option definitely works on partitions.
  • 2
    @Masi no, fallocate is just easire, no need to put zeros and set count via dd since you might do it wrong if you don't know what you are doing, dd is a super-powerfull command. And AFAIK Swap space in Linux is used when the amount of physical memory (RAM) is full, and be aware that is has no mount point. So I think it doesn't matter where it is, just chmod it to 600.
    – user172564
    Jul 25, 2016 at 18:53
  • 1
    @Masi my pleasure. BTW, you can read about fields in fstab by running man 5 fstab. Good Luck mate.
    – user172564
    Jul 25, 2016 at 19:08
  • @Masi yes of course. You can list file attributes by lsattr, and as I mentioned you can set them by chattr. For instance, if you wanna make it immutable, you can use chattr +i /mnt/.swapfile, or in BSD or OS X you can use chflags uchg yourFile. You have many attribute options, for instance, when a file with s attribute set is deleted, its blocks will be zeroed and written back to the disk. But I recommend you read its manual page and surf the net about. It's not as easy as it seems to use chattr, and you should be aware of what you are really going to do!
    – user172564
    Jul 29, 2016 at 11:29
  • 1
    @Masi AFAIK setting attribute is up to your needs, so the answer is no for the majority systems. But you can find some Unix-Like operating systems having some files or directories with immutable or restricted attributes set by default, for Instance, MacOS X.
    – user172564
    Aug 1, 2016 at 16:00
  • 1
    @Masi in fact, there is no need of doing it by default, so I think the answer is no, since Swapfiles are most likely used temporarily not permanently. If you are using LVM partitions it's better to increase your swap partition, instead of making a swapfile. But if you cannot do that and swapfiles are the only option, there is no problem, but it will affect your system and does slow its performance. There is no rule-book, everything depends on your needs, and what you can do for it, and how secure you want your system to be.
    – user172564
    Aug 2, 2016 at 7:59

You question, "How to Allocate More Space to Swap and Increase its Size Greater than Ram?", does not say anything about changing the way your system is set up.

Your fdisk and free output tells us:

  • You have a a partition (/dev/sda3) dedicated to swap. If you just resize that partition you should not need to change anything in your system to use the extra space. (/etc/fstab).
  • You have a partition (/dev/sda2) that has the OS on it. This partition has 26G free space. If you shrink this partition by 20G the OS will have 5G left to grow and use.

The swap partition is not a extended partition, this makes it a little bit easier to increase the size.

Tools required:

  • Bootable OS with gparted

Look here for one location of the last free version of Parted Magic

Instruction for resizing partitions with gparted

  1. boot into another OS that has gparted tool
  2. In gparted; resize OS partition (/dev/sda2). Shrink by amount to donate to swap space. This could take a lot of time because all of the data that is in the space being freed up will need to be moved by gparted.
  3. In gparted; resize swap partition (/dev/sda3). Move and extend to include all free space.
  4. reboot into sda2 OS.
  5. verify swap partition (/dev/sda3) is being used.

Remember that you are changing you disk and partitions on a low level and that any errors or crashes could damage you data.

Backup you data first.

  • I think you do not need gparted here because you can put swab in the same disk where your OS is. Jul 28, 2016 at 13:37
  • True, but then you would have 2 swap. File on /sda2 and partition on /sda3. This answers the part of the question that asked "Allocate more disk space for Swap in /dev/sda3. How to take it from /dev/sda2?".
    – jc__
    Jul 28, 2016 at 13:42
  • In reference to the statement: "However, not sure because I think /dev/sda2 and /dev/sad3 are on the same disk partition". /dev/sda2 and /dev/sda3 are separate partitions.
    – jc__
    Jul 28, 2016 at 14:28
  • So you are adding another device that has space on it you want to use for swap? If yes. You could create a new partition of type swap on it and when plugged in and ready to use do a swapon or similar. On device removal do a swapoff or similar. Device swap status will not be remembered on OS reboot.
    – jc__
    Jul 28, 2016 at 15:03
  • 1
    mkfs will format a partition with a file system. (vfat, ext2, ext4, ntfs). chattr will change the file attributes of a file on an ext file system.
    – jc__
    Jul 28, 2016 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .