HOW TO ATTACH AND USE EBS VOLUME ON EC2 LINUX INSTANCE- A SIMPLE GUIDE
Tags: aws, ec2, beginners
Published: 2023-08-17 18:28:43

To create new EBS volume navigate to EC2, Elastic Block Store, Volumes then click Create volume.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f3e4okt3sp13y612z5xt.jpg)

-choose the desired volume type and size, **make sure the EBS volume is in the same AZ as the EC2 instance it will be attached to**. Then click Create.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z25pyp676rsktxitc42r.jpg)

-choose the EBS volume you created, Actions then Attach volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xoj5uh87qfac0dpxi228.jpg)

-select the EC2 instance you want to attach the EBS volume to it. EBS volume name will be /dev/sdf or /dev/xvdf by default

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/363l87wsj141pl3eiu96.jpg)

-EBS volume is successfully attached

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dscd2jyj66zzzx9l0iq4.jpg)

-connect to your EC2 instance (to use EC2 instance connect you must allow SSH in the inbound rules of attached security group).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18h7dyo9bl7wzhlq75ii.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akd6ayk9xml5unz87x5s.jpg)

sudo lsblk
view your available disk devices and their mount points.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miu9z5yzhy24pexa7abe.jpg)
the EBS volume we attached is xvdf (1G) and it has no mount point.

sudo lsblk -f 
get information about all of the devices attached to the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbo9rxnc0jvoc29gs2zq.jpg)

sudo file -s /dev/xvdf
show file system for disk xvdf

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilyt45ja0cy38qs0acig.jpg)

sudo mkfs -t xfs /dev/xvdf
create a file system xfs on the volume xdvf
**If you get an error that mkfs.xfs is not found, use the following command to install the XFS tools and then repeat the previous command: sudo yum install xfsprogs**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4gnl23dqi46ko2ujado.jpg)

sudo mkdir /ebs
create directory ebs, we will use this directory to mount our EBS volume
sudo mount /dev/xvdf /ebs
mount the volume at the directory you created

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13qvihy44x16xdogy30r.jpg)

**To mount an attached EBS volume on every system reboot, add an entry for the device to the /etc/fstab file.

You can use the device name, such as /dev/xvdf, in /etc/fstab, but it is recommended to use the device's 128-bit universally unique identifier (UUID) instead. Device names can change, but the UUID persists throughout the life of the partition.**

sudo cp /etc/fstab /etc/fstab.orig
Create a backup of your /etc/fstab to restore it in case of any problems

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfo3avkun5t0s02xox3h.jpg)

sudo blkid
find the UUID of the device

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gvpphjrnma7mv8jgzk2.jpg)
EBS volume UUID is 0bce0506-dd1d-4ee7-9aef-642eac6501bb

sudo vim /etc/fstab
edit the fstab file
press i to enter insert mode
Add the following entry to /etc/fstab
UUID=0bce0506-dd1d-4ee7-9aef-642eac6501bb  /ebs  xfs  defaults,nofail  0  2
0 to prevent the file system from being dumped, and we specify 2 to indicate that it is a non-root device.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/li6to7100579dufnt6hd.jpg)

esc
:wq
w stands for write (save) and q stands for quit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbj50k9sbu55rzsbq947.jpg)

to check unmout the device then mount all  file systems in /etc/fstab
sudo umount /ebs
sudo mount -a

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bvl4xl4dvenf93oe8xn.jpg)

**Errors in the /etc/fstab file can render a system unbootable. Do not shut down a system that has errors in the /etc/fstab file**

restore fstab backup in case of error
sudo mv /etc/fstab.orig /etc/fstab