I did not create a swap partition during Ubuntu installation. Later, I freed up some space and made a swap partition. Now after each boot up, I am manually opening gparted
to right-click the swap partition to turn on the 'swapon' option.
How can I automatically enable the swap partition at boot?
Also, if I had not created the swap partition, what are my other options?
You need to edit
/etc/fstab
and add the new swap partition.You need to add a line that looks like
and you get the UUID using the command
(substitute
/dev/sda3
with the appropriate device name; in order to see the appropriate device name, we can uselsblk
- as said here).Related:
To create a swap partition after installation, create an empty partition. It should have no holes. You can then format this partition with:
replacing
/dev/sdX
with your partition. Mount this partition as swap withwhere UUID is that of your
/dev/sdX
as read from this:Bind your new swap in
/etc/fstab
by adding this line:If you want to use your swap for hibernating then you need to update the UUID in
/etc/initramfs-tools/conf.d/resume
with this contentRESUME=UUID=xxx
. Don't forget to$ sudo update-initramfs -u
.To complete things: it is also possible to create a swap file if you do not have a spare partition. This answer gives you an idea of how to create a swap file and enable it on boot.
In case you don't want or you're not sure how to create a swap partition, you can create a swap file which will work in the same way as partition. Here are the steps (using terminal):
Create an empty file (1K * 4M = 4 GiB).
Convert newly created file into a swap space file.
Enable file for paging and swapping.
Verify by:
swapon -s
ortop
:Should display line like:
KiB Swap: 4194300 total, 4194300 free
To disable, use
sudo swapoff swapfile
command.Add it into
fstab
file to make it persistent on the next system boot.Re-test swap file on startup by:
Note: Above commands re-checks the syntax of
fstab
file, otherwise your Linux could not boot up properly.There's a graphical way to do this using gparted.
Run Gparted with:
If you don't have it install, you can install it using
Once gparted is running:
new
linux-swap
swapon
There should be a key icon next to your partition name once you're done
You have to have a swap filesystem defined in /etc/fstab. It should contain a line similar to
Of course you need to substitute the value of UUID with the identifier of your device.
from linux.com just create swap file as that is more flexible and you can dynamically adjust how many and how large swap storages you have. That also allows you to move swap partition away from your system disk, thus making system faster. Also, if you use several swap spaces, adjust their priority of use to same value, so round-robin is used to write in them, allowing you to get even more speed improvements.
p.s. i would use UUID instead of device name, because it is easily possible to change device order if by accident you leave USB storage inside USB slot and simply removing and plugging back sata cables - you do not need to worry much about what drive is connected where.
(replace the "x" in sdax with swap partition number eg: sda5)
replace "xxx" with UUID number eg: 67682d1b-a1d8-4377-a3dd-67340c141619)
su (give password to get root permission)
blkid /dev/sdax (get UUID) mkswap /dev/sdax (format swap partition) swapon -U xxx
nano /etc/fstab (edit fstab) UUID=xxx none swap sw 0 0 (enter and save this text to make swap permanant) (then close fsantab)
nano /etc/initramfs-tools/conf.d/resume (create "resume" file and save this text to use hibernate/resume) RESUME=UUID=xxx (ctrl+x to close nano)->(y to save)
restart machine
login and now you can test hibernate / resume
This is a comprehensive Tutorial on how to make a Ram Drive for Ubuntu. Unfortunately.. the screenshots do not show up in this via cut and paste.. Later I will add them, but they are not needed.
The first thing you need to do is create space for your ram drive. I have a dual boot system with only a 32gb hard drive. My Ubuntu is actually installed on an SD card (yes, a real install and not a Live Casper thing). My swap was also on the SD and might get worn out quickly. I looked up how to change the swap and found this article and did not like the answers so I made a tutorial while I made the change.
If you simply wish to increase or decrease your Swap partition, you only need to increase or decrease the partition through your favorite partition manager. Ubuntu already knows where to go and it is size independent. If you want to change the location or make a new Swap, then read on. The first thing you will need to do is Shrink your hard drive to provide space for a swap. If you are shrinking a disk with the Windows system on it, then you'd better use Windows to shrink the disk (It knows its own stuff). If you are on a pure ubuntu, try resizing using a live Ubuntu USB.
Windows Resize Instructions:
If you are in Ubuntu. Make sure you use Ubuntu tools. To finish the allocation, I will use a bootable Live USB of Ubuntu which includes GParted. A Live USB is the best way to use Gparted for any reason, and you should have a USB from your first install or make one to keep handy.
Typing in 2000 was not allowed because there was not enough room to do that even though I had “defragmented” the drive (I do not believe Windows really defragged it). With some experimentation, I was allowed 1000. Then I tried 1000 again and it worked. I should thank my primary school math teacher for that one more than the MS Windows programmers. That left me with 1.95gb which is close enough for my primary school math.
Format the partition
Now I am ready for gparted to format that space as a proper swap drive. If you are in Windows and switching back and forth between Ubuntu and Windows, MAKE SURE YOU HOLD THE SHIFT KEY when clicking "Power" and "Shut down." This is very important, especially when you are messing with the "C" drive particulars in another operating system. If you are dual booting, you should know already that Windows does a fake shut down under normal circumstances. Some people have disabled "Fast Boot". I do not have that choice on my system (Windows 10) and must use the shift key thanks to my friend Google. If you boot with a live USB and you cannot mount the "C" drive (by clicking on it), then you did not fully shut down properly. Reload windows and shut down again using the shift key.
Now you are in Ubuntu Live Mode and read below:
Now we will format the unallocated space as a partitioned drive:
Before:
I did not know what to do, so I looked at my previous Ram Drive on the SD. It is formatted as Linux Swap. So I formatted my new allocation that I recently shrank as Linux Swap too.
I said OK to the Primary Partition and the warning notice and it worked!
After the format and new partition
It is a coincidence that the old swap partition on the
sd
was calledmmblk1p5
and new one is alsop5
withmmblk0
in front. Yours could be p6 or p7. Don't worry.The
mmblk0
is the physical disk and p is the partition on that physical disk. Andmmblk01
is another physical disk, etc. (That is my SD) Before I did any more work, I opened Windows again to make sure Windows still booted. You do not need to do this step if you are lazy or on a pure ubuntu machine. If it broke Windows, I would likely wipe Windows do a fresh install of Ubuntu on that disk (with a swap too). If I broke my system, I would not need to follow any more steps because I can allocate the swap on the new install. All went well and Windows booted. Now you can see the new partition in the Windows disk manager: (Right click on the start bar to get disk management)Now attach the Swap to the boot routine
Previous answers to this question actually had swapon/off commands which are not needed and can be complex. All one needs to do is find the right file, and point the swap to the new partition. Again, if you only resized the partition, you do not need to do anything. It is already setup to find the partition. The size gets done automatically.
If you changed the partition or are making a first ever swap, then read below:
Let me tell you that, this is the first time I am doing this. I am not a UNIX geek and I prefer GUI tools. I am learning and crossing my fingers as I make this tutorial. All has gone well so far. At the root of your system is a directory called etc. Inside that directory is a file called
fstab
(no extension). You will need to edit this file with super user "su" rights. Before we do that, we need the UUID of the new Swap Partition you made with the Live USB. Type sudo su Now you are in Super User mode. You can do anything (including wipe your own existence).Type
blkid
and look for the uuid of the new swap partition. I saw mine below and highlighted it:Pay no attention to PartUID's.
Now, the other people will say type:
However, I tried that and it did not find the right file (comments please).
So I just typed gedit and open it the whimpy GUI way (Success!). Since, I was already in Super User mode (sudo su) I have root permission to do ANYTHING in Gedit, including saving the file after I change it. Wonderful! Before you change or corrupt anything, save the fstab as backup called...
fstab.bak
.Make the backup by using the whimpy GUI way and choose the familiar “Save As” and add "bak" to the file
fstab.bak
. Check to see it is there with a .bak extension. Geeks will tell you to use the terminal cp command, but we novices cannot find the file in terminal to get this far in the first place. Just like any word processor, after the “save as” command, your current file is now thefstab.bak
. Open the real one you had before, and then close the backup file. Double check to see you are editing the realfstab
with no extension. If you edit the backup file, nothing will happen when you reboot, plus your backup copy will not be a real backup of the original.My original
fstab
file looks like this:The last line (I made red for you), has the original swap info that is used probably on boot up time. From the previous blkid command, I could see my new drive UUID. Copy that phrase by highlighting the text in the terminal and then right clicking and choosing copy. Paste that phrase into another separate editor without
su
rights as a scratch pad (you can use Libre Office, and geeks can usenano
). This step helps you get the right text easily without the quotes so you can paste easily into thefstab
file. You want to change this file as little as possible.So, my new UUID is listed in red above. All I need to do is change the uuid for the swap. If you have no swap drive, then copy my line in the above fstab file and then add the proper uuid from the previous blkid command. (choose the right uuid please!)
If this is your first Swap line, make sure you includes the complete line with the extra stuff
Do not worry about the exact spacing so much.
My new file looks like this: Notice I added a comment above my change too The
#
character means it is a comment.Do not paste the quotes. Do not have spaces after the
=
sign.Save your file.
Open it again, just to be sure your changes saved. Close the gedit and terminal with dangerous
su
rights that geeks will scold you for doing (because it is easy work with... and easy to destroy things too).REBOOT and and cross your fingers!
I rebooted and then I checked to see if my swap was working by looking at system performance app. I still had a Swap drive! But was it the new one or the old one? I could not verify my new drive was actually loaded because the new one is the same size as the old drive. Therefore, I typed:
swapon
(DO NOT TYPE ANYTHING MORE THAN SWAPON and it will only list the swap for you: safe). Now I see my new drive onmmblk0p5
which matches the UUID work I did onfstab
. My slim 32gb main drive took effect:This concludes this tutorial from a novice user who has never done this before. Half of the reason I made this tutorial was so the Geeks who help us (and thank you) can see how to help novice users. I suspected the previous answers were misleading and decided to make a tutorial as I did my work... By guessing from the other answers which were useful, I concluded that all I needed to do was edit the
fstab
flle and reboot. All worked well and I still have windows 10 and have not wiped my system yet. If anyone knows how to share a Windowspagefile.sys
as a Linux swap file, I would be interested since 2 gb is taken for that in Windows and hd space is very pricy on my 32 GB machine.If you installed new empty disk then need to create swap partition on it.
Show partitions:
$ sudo fdisk -l
Disk /dev/sdb: 4.1 GiB, 4420796416 bytes, 8634368 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
Create swap partition:
$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xbf65f113.
Command (m for help): m
Help:
DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag
Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition
Misc m print this menu u change display/entry units x extra functionality (experts only)
Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file
Save & Exit w write table to disk and exit q quit without saving changes
Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-8634367, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-8634367, default 8634367):
Created a new partition 1 of type 'Linux' and of size 4.1 GiB.
Command (m for help): t Selected partition 1 Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/ 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto 1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): m
Help:
DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag
Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition
Misc m print this menu u change display/entry units x extra functionality (experts only)
Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file
Save & Exit w write table to disk and exit q quit without saving changes
Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Make partition swap:
$ sudo mkswap /dev/sdb1 Setting up swapspace version 1, size = 4.1 GiB (4419743744 bytes) no label, UUID=e2c53e87-c32c-4230-8405-25b471af6524
Use turn on swap on created partition:
sudo swapon -U e2c53e87-c32c-4230-8405-25b471af6524
Make swap permanently:
sudo nano /etc/fstab
Add line below and save file.
Check swap
sudo free -m total used free shared buff/cache available Mem: 1993 203 1271 6 518 1635 Swap: 4214 0 4214
Done.