Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add permanent swap on your ubuntu 14.04 Digital Ocean Droplet and vps
- What is swap area?
- A swap is space allocated in hard drive where your OS can store files in temporary basis when RAM is full. Creating a swap area in your VPS can boost speed to a certain extent. If your VPS or droplet (as digital ocean calls it) is suffering memory limit, in this case, adding swap could boost performance of your system. (system=droplet=vps)
- How to add swap area in your VPS?
- Adding swap is not that hard. Follow the instructions below to add swap area permanently in your digital ocean droplet or any other vps with ubuntu 14.04. You will neet root access ofcourse. My current configuration has 20 Gb HDD and 512 MB RAM and I'm planning to add 1GB swap. Get dirt cheap vps for yourself.
- Step one: Check is swap has already been allocated or not. To run the check follow the command below:
- free -m
- The command should give the result below if swap has not been allocated in your droplet.
- total used free shared buffers cached
- Mem: 490 286 204 0 11 234
- -/+ buffers/cache: 40 449
- Swap: 0 0 0
- Here we can clearly see that the total swap allocated is 0, which means, no swap allocated. Now we can proceed to next step.
- Step two: Since we are working on a VPS, its not always possible to create a separate partition dedicated for swap. In this case, we can create a swap file in existing drive. Before that, lets get information on our current disk usage with the commands below.
- df -h
- This command should give us the information on our current disk usage, which should look like below.(this will be different in each setup)
- Filesystem Size Used Avail Use% Mounted on
- /dev/vda1 20G 1.6G 18G 9% /
- none 4.0K 0 4.0K 0% /sys/fs/cgroup
- udev 235M 4.0K 235M 1% /dev
- tmpfs 50M 328K 49M 1% /run
- none 5.0M 0 5.0M 0% /run/lock
- none 246M 0 246M 0% /run/shm
- none 100M 0 100M 0% /run/user
- From the information above, we now know that our main partition /dev/vda1 has 20G of total space and 18G is available and we will create swap in here.
- Step 3: Now we are ready to create swap file. There are few methods to create a swap file, in our case we will use the fastast method. Follow the command below to create swap file.
- sudo fallocate -l 1G /swapfile
- In above command we can see that, we have allocated a file of 1GB as "swapfile" where we will let ubuntu 14.04 store required information as required. This will prompt immediately and we can confirm this with following command:
- ls -lh /swapfile
- This will return us the details on swap file that we just created as below:
- -rw-r--r-- 1 root root 1.0G Aug 12 00:06 /swapfile
- Now, that we have created and confirmed our swap file. Its time for us to enable it.
- Step 4: In order to enable swap file, we have to first configure permissions on the file. We can to that with the command below:
- sudo chmod 600 /swapfile
- and again to confirm if the permissions have been configured properly follow the commands below.
- ls -lh /swapfile
- and this should return the updated information on swap file as follows:
- -rw------- 1 root root 1.0G Aug 12 00:06 /swapfile
- Now that our swap file has been created and permission has been updated to make it secure, we can tell system to set it up as swap file with the commands below:
- sudo mkswap /swapfile
- and this should give us information as below:
- Setting up swapspace version 1, size = 1048572 KiB
- no label, UUID=1118d955-c1cb-4106-82bf-8dfc46ea3b58
- Now, we have everything ready and its time to enable the swap file. For this, use the command below:
- sudo swapon /swapfile
- We have successfully created, configured and enabled our swap file. To confirm this we can run the command below and see the updated information with command below:
- free -m
- result:
- total used free shared buffers cached
- Mem: 490 286 203 0 11 234
- -/+ buffers/cache: 40 449
- Swap: 1023 0 1023
- Here we can see that our swap file has been created and is being recognized by our system. But it is still not permanent. If we restart our Droplet, our system will loose its swap configuration. In order to make it permanent we have to tweak few things. We will cover this in our next step
- Step 5: In order to make our swap file permanent, we need to add few information to "fstab" file. In order to edit fstab file, follow command:
- sudo nano /etc/fstab
- here, add the line below at the bottom of the fstab file.
- /swapfile none swap sw 0 0
- and update the file. This will make sure that Ubuntu 14.04 will use swapfile every time it boots.
- Step 6: Now since everything is properly configured, its time to make most out of the swap file. In order to do that, we need to configure swappiness. Swappiness is a parameter that tells OS how often it can use swap to store data out of RAM. The value can be anything between 0 to 100. when used 0, swap file will be used only when absolutely necessary and when used 100, just the opposite. To check the current swappiness value you can run cat /proc/sys/vm/swappiness and it will return the current swappiness value. Since we are using a VPS, we will use value of 10 as swappiness. To do that, we can follow the command below.
- sudo sysctl vm.swappiness=10
- This will update the swappiness value to 10 and to make it permanent, we can follow the commands below:
- sudo nano /etc/sysctl.conf
- And add the line vm.swappiness=10 at the bottom of the page and save and close the file when done.
- Step 7: Now, the last step is to configure vfs_cache_pressur. Basically vfs_cache_pressure is a parameter that allows system to choose how much it can store inode and dentry information over other data. To check the current value, you can use cat /proc/sys/vm/vfs_cache_pressure. To assign your value(in our case 50), follow the command below:
- sudo sysctl vm.vfs_cache_pressure=50
- and to make it permament, follow the command below:
- sudo nano /etc/sysctl.conf
- and add the line vm.vfs_cache_pressure = 50 at the bottom and save and close.
- Now, we have configured everything correctly and we have a swap on our system that is permanent. You can reboot once and check it with the command below to see if swap is being registered or not.
- sudo swapon -s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement