How to Attach and Mount an EBS volume to EC2 Linux Instance
- Last Updated On: March 24, 2020
- By: devopscube
AWS allows you to create new EBS volumes and you can attach it to instances for extra storage. However, to make it usable as storage inside the instance, you need to mount it to a specific folder.
Mount an EBS volume to EC2 Linux
In this tutorial, we will teach you how to attach and mount an EBS volume to ec2 Linux instances.
Follow the steps given below carefully for the setup.
Step 1: Head over to EC2 –> Volumes and create a new volume of your preferred size and type.
Note: Make sure the EBS volume and the instance are in the same zone.
Step 2: Select the created volume, right-click and select the “attach volume” option.
Step 3: Select the instance from the instance text box as shown below.
Step 4: Now, login to your ec2 instance and list the available disks using the following command.
lsblk
The above command will list the disk you attached to your instance.
Step 5: Check if the volume has any data using the following command.
sudo file -s /dev/xvdf
If the above command output shows “/dev/xvdf: data
“, it means your volume is empty.
Step 6: Format the volume to the ext4
filesystem using the following command.
sudo mkfs -t ext4 /dev/xvdf
Step 7: Create a directory of your choice to mount our new ext4 volume. I am using the name “newvolume”
sudo mkdir /newvolume
Step 8: Mount the volume to “newvolume” directory using the following command.
sudo mount /dev/xvdf /newvolume/
Step 9: cd into newvolume directory and check the disk space for confirming the volume mount.
cd /newvolume df -h .
The above command would show the free space in the newvolume
directory.
To unmount the volume, you have to use the following command.
umount /dev/xvdf
Get 95% Off Today: Best AWS Certified Solutions Architect – Associate Course
Automount EBS Volume on Reboot
By default on every reboot, the EBS volumes other than root volume will get unmounted. To enable automount, you need to make an entry in the /etc/fstab
file.
Step 1: Back up the /etc/fstab file.
sudo cp /etc/fstab /etc/fstab.bak
Step 2: Open /etc/fstab file and make an entry in the following format.
device_name mount_point file_system_type fs_mntops fs_freq fs_passno
For example,
/dev/xvdf /newvolume ext4 defaults,nofail 0 0
Step 3: Execute the following command to check id the fstab file has any error.
sudo mount -a
If the above command shows no error, it means your fstab entry is good.
Now, on every reboot, the extra EBS volumes will get mounted automatically.
Add Multiple EBS Volumes To EC2 Instance
You can add more than one EBS volumes to an ec2 instance as an additional disk. You can follow the same steps explained above and the only difference will be the disk name that you get from the lsblk
command.
Here is an example output having two EBS volumes attached to an instance.
How Many EBS volumes can I attach to an Instance?
You can attach a maximum of 20 EBS volumes to the instance. However, limit the maximum volumes to have optimal performance. Also, plan your instance capacity based on the workload you are trying to run.
For example, databases require high IOPS for high read-write rates. IOPS depends on the disk size. Higher the size, the higher the IOPS.
Also, it is recommended to take snapshots of EBS volumes for high data high availability and restore option. Check out EBS snapshot automation article for strategies and restore options.
That’s how you mount and unmount EBS volumes in your ec2 instances. If you get any error during the setup, please feel free to contact us in the comment section.
devopscube
Other Interesting Blogs
How To Setup a Zookeeper Cluster – Beginners Guide
Zookeeper is a distributed coordination tool which can be used for various distributed workloads. In this article, we have explained the necessary
How To Setup Kube State Metrics on Kubernetes
In this blog we will look at what is Kube State Metrics and its setup on Kubernetes. What is Kube State Metrics?
Kubernetes Ingress Tutorial For Beginners
Kubernetes Ingress is a resource to add rules for routing traffic from external sources to the services in the kubernetes cluster. In
Comments
Awesome post, Thanks it works
Thanks for the post. But, I’m facing one error
I couldn’t able to unmount the volument, I’m facing a error as “””umount: /newvolume: umount failed: Operation not permitted”””
Please sort this out ASAP
I don’t know if you’ve already figured it out, but maybe you could try taking ownership of the volume:
sudo chown `whoami` /newvolume
Hope it helps
Thank you. This was exactly what I was looking for. I appreciate your post!
not able to mount /dev/nvme2n1,getting error mount: /dev/nvme2n1 is write-protected, mounting read-only mount: unknown filesystem type ‘(null)’
Hi Dhanush,
In which step are you facing error? did you format the disk as per step 6?
Hi! I did exactly that. But when I try to create files in the mounted directory, it says I have no permission. Any idea what I doing wrong?
Try it with Sudo user
Great! Simple simple simple
simple and easy, but is it good practice.
Yes of-course !! It’s a good practice . .
thank you so munch for this tuto
v good
Tks!
Thank you!
IT worked like a charm!
Really Helpful !! Thank you
Awesome
Awesome Bro !! I Thanks for the step by step
Very helpful, thank you.
Good one – works like a charm. Thanks 🙂
Perfect and very simple
Perfect and very simple. Thanks for this.
This is super helpful, thank you! Only one issue: once I got to the end, “sudo mount -a” gave me this error:
-bash: $’sudo\302\240mount’: command not found
However, “sudo mount” works without the -a option. Do you know why “sudo mount -a” has an issue with the non-breaking space (\302\240)? Thanks!
You copied from the Web page. There’s a in the markup. Just type the command in using your keyboard instead of using your clipboard.
Thanks guys!
Perfect! Thanks a lot! 🙂
Great. Thanks.
Thanks
Thanks ! Works like a charm