Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I did not opt for a swap partition during Ubuntu installation. Later I freed up some space, and made a swap partition. Now after booting up I am manually opening gparted and right-clicking the swap partition and turning it on using 'swapon' option.

How can I automatically enable the swap partition every time?

share|improve this question
    
askubuntu.com/questions/299417/… – Qasim Mar 13 '14 at 12:04
up vote 70 down vote accepted

You need to edit /etc/fstab and add the new swap partition.

sudo nano /etc/fstab

You need to add a line that looks like

UUID=735b3be3-779c-4d21-a944-b033225f3ab4 none   swap    sw      0       0

and you get the UUID using the command

sudo blkid /dev/sda3

(substitute /dev/sda3 with the appropriate device name).

Related:

share|improve this answer
    
Need Help for /etc/fstan i ma getting output :- bash: /etc/fstab: Permission denied – Ashu Aug 31 '14 at 8:20
    
Use sudo gedit /etc/fstab – The Brown One Nov 29 '14 at 22:37

To create a swap partition after installation, create an empty partition that should have no holes. You can then format this partition with:

sudo mkswap /dev/sdX

replacing /dex/sdX with your partition. Mount this partition as swap with

sudo swapon -U UUID

where UUID is that of your /dev/sdX as read from this:

blkid /dev/sdX

Bind your new swap in /etc/fstab by adding this line:

UUID=xxx    none    swap    sw      0   0

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 content RESUME=UUID=xxx. Don't forget to $ sudo update-initramfs -u.

To complete things: it would also be possible to create a swap file in case we do not have a spare partition. This answer gives you an idea on how to create such a file and enable it on boot.

share|improve this answer
    
As of right now, I have a SSD with two partitions, but I would like to make a third one. If I have a primary ext4 partition that holds the OS and my files, can I use this to peel off 2GB off it into t a new partition? This primary partition has more than enough free space so that shouldn't be an issue. – dustin Apr 4 '15 at 22:46

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.

share|improve this answer
    
This was the most helpful response, because it does not require re-partitioning the drive. Too bad Ubuntu doesn't create swap by default when you install it on a new machine! – Ernie Feb 27 '15 at 17:34

You have to have a swap filesystem defined in /etc/fstab. It should contain a line similar to

UUID=67682d1b-a1d8-4377-a3dd-67340c141619 none            swap    sw              0       0

Of course you need to substitute the value of UUID with the identifier of your device.

share|improve this answer
    
how can I get it? – user13593 Apr 5 '11 at 20:21
2  
You can use the device name instead, like /dev/sda1 (without the UUID). – Adam Byrtek Apr 5 '11 at 20:30

(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

share|improve this answer

This is a comprehensive Tutorial on how to make a Ram Drive for Ubuntu. Unforunately.. 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 parition 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: 0. Backup your system, and defrag your drive. 1. Right Click on the start bar. 2. Select Disk management 3. Right click ON THE CORRECT DRIVE 4. Select Shrink. 5. Type in the correct amount.

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.

Below are screen shots of Shrink and Gparted steps.

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 expermentation, 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.

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: If you are a pure Ubuntu and did not shrink in Windows. You will use the Resize option in gParted. If you already shrank your drive, do not do the resize step. You can type gparted in the Dash bar and get the application quite quickly. Right click on the drive you want to resize.

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 formated as Linux Swap. So I formated my new allocation that I recently shrank as Linux Swap too.
I said OK to the Primary Partion and the warning notice and it worked!

Formating the partition:

After the format and new partition:

It is a coincidence that the old swap partition on the sd was called mmblk1p5 and new one is also p5 with mmblk0 in front. Yours could be p6 or p7. Don't worry. mmblk0 is the physical disk and p is the partition on that physical disk. mmblk01 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 partion 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 existance). 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 sudo gedit \etc\fstab 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 the fstab.bak. Open the real one you had before, and then close the backup file. Double check to see you are editing the real fstab 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:

/etc/fstab: static file system information.

#

Use 'blkid' to print the universally unique identifier for a

device; this may be used with UUID= as a more robust way to name devices

that works even if disks are added and removed. See fstab(5).

#

/ was on /dev/mmcblk1p6 during installation

UUID=a611601a-6ea8-4e34-9859-42a2470cac29 / ext4 errors=remount-ro 0 1

/boot/efi was on /dev/mmcblk0p1 during installation

UUID=00F2-C0CE /boot/efi vfat umask=0077 0 1

swap was on /dev/mmcblk1p5 during installation

UUID=8fb20159-62e4-41a3-8550-791c7a66c328 none swap sw 0 0

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 wihout su rights as a scratch pad (you can use libreoffice, and geeks can use nano). This step helps you get the right text easily without the quotes so you can paste easily into the fstab file. You want to change this file as little as possible.

New UUID UUID="709ea197-e6fb-428b-9c1f-4b258452b10c" 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 none swap sw 0 0. . 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.

/etc/fstab: static file system information.

#

Use 'blkid' to print the universally unique identifier for a

device; this may be used with UUID= as a more robust way to name devices

that works even if disks are added and removed. See fstab(5).

#

/ was on /dev/mmcblk1p6 during installation

UUID=a611601a-6ea8-4e34-9859-42a2470cac29 / ext4 errors=remount-ro 0 1

/boot/efi was on /dev/mmcblk0p1 during installation

UUID=00F2-C0CE /boot/efi vfat umask=0077 0 1

swap was on /dev/mmcblk1p5 during installation NOW changed to mmcblk0p5

UUID= 709ea197-e6fb-428b-9c1f-4b258452b10c none swap sw 0 0

(I added red color.) 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) I see my new drive on mmblk0p5 which matches the UUID work I did on fstab. 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 Windows "pagefile.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. I am new to "ask ubuntu" and have only a few points. Vote me up and create a bounty for my clicking noise question or any other unanswered question I have as "thanks." I suspect is the Cherryview Graphics.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.