MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

AWS Tutorial: Auto mount EBS Volume to ec2 Linux instance on Reboot

To make an extra Amazon EBS volume mount automatically after an EC2 Linux instance reboots, add an entry for it to /etc/fstab. Refer to the volume by its filesystem UUID and include the nofail option. The steps above show the basic idea. This guide adds the missing steps (attaching, formatting, finding the UUID), corrects the device-name example, and explains how to test the entry so a mistake can't stop the instance from booting.

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.

Follow the steps given below to automount the EBS volume to ec2 instance.

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.


Why the volume is not mounted after a reboot

EBS volumes do not unmount themselves. A volume mounted by hand with the mount command exists only in the running kernel, so the mount is gone after a restart. The volume is still attached and its data is untouched. At boot, systemd reads /etc/fstab and mounts every filesystem listed there. A volume that isn't in that file stays attached but unmounted.

Each line in /etc/fstab has six fields:

FieldExampleMeaning
DeviceUUID=1b2c...Which filesystem to mount. A UUID is safer than a /dev name.
Mount point/dataAn existing, empty directory
Typexfs or ext4Filesystem type created with mkfs
Optionsdefaults,nofailMount options. nofail lets the boot continue if the volume is missing.
Dump0Used by the old dump backup tool. Almost always 0.
Pass2fsck order at boot: 0 = never check, 1 = root, 2 = other filesystems

Prerequisites

  • A running EC2 Linux instance (Amazon Linux 2023, Amazon Linux 2, Ubuntu, RHEL and similar) and SSH access to it. On Windows, see How to Login to Linux Server using Putty?.
  • An EBS volume in the same Availability Zone as the instance, attached through the console or with aws ec2 attach-volume --volume-id vol-0123456789abcdef0 --instance-id i-0123456789abcdef0 --device /dev/sdf.
  • sudo privileges on the instance.

Note: The device name you choose in the console (for example /dev/sdf) is often not the name Linux uses. On Nitro-based instances, EBS volumes appear as NVMe devices such as /dev/nvme1n1. On older Xen instances they usually appear as /dev/xvdf. Always check with lsblk.

Complete procedure

First, find the new volume. It's the disk that has no mount point:

lsblk -f

# Example output on a Nitro instance
NAME          FSTYPE LABEL UUID                                 MOUNTPOINTS
nvme0n1
โ”œโ”€nvme0n1p1   xfs    /     2a1f...                              /
โ””โ”€nvme0n1p128 vfat         7B77-95E7                            /boot/efi
nvme1n1Code language: PHP (php)

Check whether the volume already contains a filesystem. If the output is just data, the volume is empty. If it shows a filesystem (for example a volume restored from a snapshot), skip the mkfs step, because formatting erases everything on the volume.

sudo file -s /dev/nvme1n1
# /dev/nvme1n1: data          <- empty, safe to format

# Create a filesystem (xfs is the default on Amazon Linux and RHEL)
sudo mkfs -t xfs /dev/nvme1n1
# or: sudo mkfs -t ext4 /dev/nvme1n1Code language: PHP (php)

Create the mount point, mount the volume once, and read its UUID:

sudo mkdir -p /data
sudo mount /dev/nvme1n1 /data

sudo blkid /dev/nvme1n1
# /dev/nvme1n1: UUID="1b2c3d4e-5f60-4a7b-8c9d-0e1f2a3b4c5d" BLOCK_SIZE="512" TYPE="xfs"Code language: PHP (php)

Back up /etc/fstab as shown above, then add the entry. Using tee -a appends the line and avoids the common mistake of overwriting the file with >:

sudo cp /etc/fstab /etc/fstab.bak
echo 'UUID=1b2c3d4e-5f60-4a7b-8c9d-0e1f2a3b4c5d  /data  xfs  defaults,nofail  0  2' | sudo tee -a /etc/fstabCode language: PHP (php)

Test the fstab entry properly before rebooting

sudo mount -a on its own doesn't prove anything if the volume is already mounted, because it skips filesystems that are already mounted. Unmount the volume first so that mount -a actually tests the new line:

  1. Unmount the volume: sudo umount /data
  2. Tell systemd to reread fstab: sudo systemctl daemon-reload. Without this, newer mount versions warn that systemd still uses the old version of the file.
  3. Check the syntax and the devices: sudo findmnt --verify. It reports unknown UUIDs, missing mount points and bad filesystem types.
  4. Mount everything from fstab: sudo mount -a. No output means success.
  5. Confirm the mount: df -hT /data
  6. Reboot (sudo reboot), reconnect and run df -hT /data again.
df -hT /data
# Filesystem     Type  Size  Used Avail Use% Mounted on
# /dev/nvme1n1   xfs    20G  175M   20G   1% /dataCode language: PHP (php)

Corrections to the original steps

The example above uses /dev/xvdf as the device. That name only exists on older Xen-based instance types. On Nitro instances the volume is /dev/nvme1n1 or similar, and NVMe names are assigned in the order devices are detected, so they can change between boots or when volumes are added. An fstab line that points at the wrong name will either fail or mount the wrong disk. Use the UUID instead:

# Original (fragile)
/dev/xvdf       /newvolume   ext4    defaults,nofail        0       0

# Corrected (stable across reboots and instance types)
UUID=1b2c3d4e-5f60-4a7b-8c9d-0e1f2a3b4c5d  /newvolume  ext4  defaults,nofail  0  2Code language: PHP (php)

Setting the last field to 2 lets ext4 volumes get a filesystem check at boot, after the root filesystem. For xfs the check does nothing, so 0 and 2 behave the same. The original also leaves out the steps that make the entry work: formatting the volume, creating the mount point directory and finding the UUID. These are covered in the procedure section.

Troubleshooting

SymptomLikely causeFix
Instance doesn't come back after reboot, or the console log shows emergency modeBad fstab line without nofail, or a typo in the UUIDUse the EC2 Serial Console to fix /etc/fstab. Alternatively, stop the instance, attach its root volume to a rescue instance, edit the file and reattach it.
mount: wrong fs type, bad option, bad superblockVolume was never formatted, or the type field doesn't matchRun sudo file -s /dev/nvme1n1 and set the type field to match
mount point /data does not existDirectory not createdsudo mkdir -p /data
xfs volume from a snapshot won't mount: duplicate UUIDSnapshot of a disk that is already mounted on the same instanceMount once with -o nouuid, or give it a new UUID with sudo xfs_admin -U generate /dev/nvme2n1 while it is unmounted
Volume is empty after a rebootData was written to /data before it was mounted, so it landed on the root diskCheck with findmnt /data before writing data, and keep the mount point directory empty

Note: When you mount the root volume of an instance on a rescue instance built from the same AMI, xfs may refuse because of a duplicate UUID. Use sudo mount -o nouuid /dev/nvme1n1p1 /mnt there.

Best practices

  • Always reference the volume by UUID= (or LABEL=), never by /dev/xvdf or /dev/nvme1n1.
  • Always include nofail for non-root EBS volumes so that a detached or failed volume doesn't block boot.
  • Run findmnt --verify after every edit to fstab, and keep the backup copy until the reboot test passes.
  • Set ownership after mounting, for example sudo chown ec2-user:ec2-user /data. Permissions belong to the mounted filesystem, not to the empty directory underneath it.
  • After increasing the volume size in AWS, grow the filesystem online with sudo xfs_growfs /data or sudo resize2fs /dev/nvme1n1. The fstab entry doesn't need to change because the UUID stays the same.
  • Turn on EBS encryption when you create the volume. It is transparent to the OS and needs no fstab changes.
  • For fleets or Auto Scaling groups, script these steps in user data or an AMI instead of editing each instance by hand.

Frequently asked questions

Does the fstab entry still work after stopping and starting the instance?

Yes. Stop/start is just a reboot as far as fstab is concerned, as long as the volume stays attached. Because the entry uses the UUID, it works even if the instance moves to a Nitro type and the device name changes.

What does the nofail option do in /etc/fstab?

It tells systemd to continue booting if the device is missing or fails to mount. Without it, a detached EBS volume or a wrong UUID can drop the instance into emergency mode and leave it unreachable over SSH.

Will the UUID change if I take a snapshot or resize the volume?

Resizing doesn't change the filesystem UUID. A volume created from a snapshot has the same UUID as the original, which can conflict if both are attached to the same instance. Only running mkfs again, or changing the UUID on purpose, generates a new one.

How do I find which NVMe device matches my EBS volume ID?

Run ls -l /dev/disk/by-id/. EBS NVMe devices appear as nvme-Amazon_Elastic_Block_Store_vol... links that include the volume ID. On Amazon Linux, sudo ebsnvme-id /dev/nvme1n1 also prints the volume ID and the requested device name.

Should I use xfs or ext4 for an EBS data volume?

Both work well. xfs is the default on Amazon Linux and RHEL and can be grown online but not shrunk. ext4 is the Ubuntu default and supports offline shrinking. Pick the one your team already knows how to manage.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

AWS Tutorials: EBS – Steps to Increase the Disk Size on a Running EC2 Instance:

you can increase the disk size of an Amazon EC2 instance while it is running. Amazon allows you to modify the size of an Elastic Block Store…

Read More

AWS Tutorial: How to Attach and Mount an EBS volume to EC2 Linux Instance?

Step 1 – Create a Volume Step 2 – Attach a Volume to EC2 Instance Step 3 – Verify if Volume is attached or not Verify if…

Read More

EC2 – EBS Volume Demo:

In this tutorial we are going to Explore and setup extra volume to EC2 instance Step 1 – Login to AWS console, launch EC2 launch wizard page….

Read More

Terraform Example – AWS EC2 Instance with EBS

This page shows how to create an AWS EC2 instance with an extra EBS data volume using Terraform. It uses three resources: aws_instance, aws_ebs_volume and aws_volume_attachment. Below…

Read More

EC2 Boot from EBS Explained: Architecture, Trade-offs, and an AWS CLI Workflow

Booting Amazon EC2 instances from Amazon EBS was a major shift from ephemeral root disks to persistent root volumes. This guide explains what changed, why it mattered, and how to use the model safely in modern operations.

Read More

Cloud Tools Adds Amazon EBS Support: Durable EC2 Storage for Database Workloads

This historical update covers when Cloud Tools introduced Amazon EBS support for EC2 applications, including database-backed deployments. It explains the supported storage modes, the backup/restore flow, and practical command-level equivalents.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x