5

I have a t2.micro Ubuntu Server 1GB RAM and 30GB Hard Drive (HD). I am in need to take 2GB out of the 30GB HD in order to create a Swap partition. Could someone give me a help on how to that?

I have a doubt if whether I should create the swap partition as I asked above or should I create a swap file as shown bellow in item 1 and 2:

1 -

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo mkswap /swapfile
sudo swapon /swapfile

To enable it by default after reboot, add this line to /etc/fstab:

/swapfile swap swap defaults 0 0

2 -

sudo dd if=/dev/zero of=/var/swapfile bs=1M count=2048 &&
sudo chmod 600 /var/swapfile &&
sudo mkswap /var/swapfile &&
echo /var/swapfile none swap defaults 0 0 | sudo tee -a /etc/fstab &&
sudo swapon -a

That said, which one is best? swap partition or swap file?

Thank you very much in advance for your time.

HelpOverFlow
  • 315
  • 1
  • 2
  • 10
  • 1
    note that point 1 and 2 are basically the same thing, the only difference is the location of the swapfile (`/` vs `/var`). Assuming you're already `root`, you'll need the `chmod 600` also on point 1, or the system will refuse to mount the file. – Cavaz Sep 30 '16 at 23:22

2 Answers2

1

Have you considered just adding a new volume to your instance and dedicate it for swap space?

It might be easier and safer to do, with less risk of disrupting you running instance:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/add-instance-store-volumes.html

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
0

Actually in terms of performances it doesn't make much of a difference with modern kernels. See the following question for a more detailed explanation:

https://serverfault.com/questions/25653/swap-partition-vs-file-for-performance

I'd say go for the procedure you feel more comfortable with. But if the system is already installed and you don't want to add another disk then using the swapfile seems to be the more straightforward solution.

Community
  • 1
  • 1
Cavaz
  • 2,996
  • 24
  • 38