How to Mount and Unmount Disks on a Linux VPS

How to Mount and Unmount Disks on a Linux VPS

In this tutorial, we will discuss how to mount and unmount disks on a Linux VPS, along with several commands commonly used when working with additional disks.

Mounting is the process of connecting a partition or disk to a specific directory (mount point) so it can be accessed and used by the system. Conversely, unmounting is the process of releasing that connection so the disk can no longer be accessed through that directory.

When you add a new disk to a VPS, for example for backups, additional storage, or block storage from a cloud provider, the disk will not be usable automatically. You need to mount it first so the system can read and write data to that disk.

TL;DR: First, check the disk you want to use with lsblk. If the disk is still empty, create a filesystem with mkfs.ext4, if it already has content you can skip this step. Create a mount point with mkdir, then mount the partition with mount /dev/sdX /mnt/backup. To release it, use umount /mnt/backup. To have the partition mount automatically every time the VPS reboots, add the partition UUID entry to /etc/fstab.

How to Mount a Disk on a VPS

Before proceeding to the disk mounting steps, ensure you can access your VPS via SSH and have root or sudo access, or you can follow the tutorial How to Log In to a Linux VPS via SSH first.

If you already have SSH access, you can start from the first step.

1. Check the Partition and Disk List

Before mounting, you need to know the device name of the partition you want to use. Use the following command

lsblk

This command will display all disks and partitions installed on the VPS, including those that are not yet mounted.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.1-1

You can also use the following command to see the filesystem details for each partition. However, if the disk was just attached and has not been formatted, it will not be displayed.

sudo blkid

Note the device name of the partition you want to mount, for example /dev/vdb.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.1-2

2. Check and Create a Filesystem

Before formatting, you need to ensure whether the disk is truly empty or already has a filesystem and data on it, for example because the disk was previously used or is a volume that already contains data from the provider. Check with the following command

sudo lsblk -f

If the FSTYPE column on that device shows content (for example ext4, xfs, or ntfs), it means the disk already has a filesystem. You can skip this formatting step and proceed directly to step 3.

If the FSTYPE column is empty, it means the disk is truly empty and does not yet have a filesystem, so one needs to be created first. Attention, this step will erase all data on that partition, so make sure the device you are targeting is correct.

sudo mkfs.ext4 /dev/vdb

Replace /dev/vdb with the device name according to the results of lsblk in the previous step.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.2

If you are unsure whether the data on the disk is still needed or not, you can mount it temporarily as read-only to check its contents first without the risk of writing or changing data

sudo mount -o ro /dev/vdb /mnt/backup

Check the contents with ls /mnt/backup, then unmount it again with sudo umount /mnt/backup before continuing to the normal mount step.

3. Create a Mount Point

A mount point is an empty directory that will be used as the disk access location. Create a new directory with the command

sudo mkdir -p /mnt/backup

-p is used so that the command continues to run even if the parent directory does not exist yet.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.3

/mnt/backup is the name of the directory you set and can be changed as needed.

4. Mount the Disk

After the mount point is ready, you can mount the disk with the following command

sudo mount /dev/vdb /mnt/backup

This command will connect the partition /dev/vdb to the directory /mnt/backup, so you can immediately read and write data in that directory.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.4

5. Verify the Mount Was Successful

To ensure the disk is mounted correctly, use the command

df -h | grep /dev/

You will then see the partition /dev/vdb appear in the list with the mount point /mnt/backup, complete with the capacity and remaining available space.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 1.5

How to Unmount a Disk on a VPS

1. Unmount the Disk

To release the disk from the mount point, first exit that directory if you are currently inside it, then run

sudo umount /mnt/backup

You can also unmount based on the device name directly

sudo umount /dev/vdb
Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 2.1

2. Resolving the Target is Busy Error

Sometimes an error message appears target is busy when unmounting, usually because there are still processes accessing that directory. Check which processes are still using the mount point with

sudo fuser -vm /mnt/backup

After knowing which process is holding it, you can stop it forcefully with

sudo fuser -km /mnt/backup

After the process is stopped, repeat the command umount.

Automatic Mounting at Boot with fstab

Please note that mounting done with the mount command above is temporary. Once the VPS is rebooted, the disk will not be mounted automatically. To make it permanent, you need to add an entry to /etc/fstab.

1. Find the Disk UUID

Use the UUID instead of a device name like /dev/vdb, because the device name order can change after a reboot. Check the UUID with

sudo blkid /dev/vdb

Note the UUID value that appears. For example, here is what appears for /dev/vdb:

/dev/vdb: UUID="a28b4076-d743-48b4-a140-d62c9e5c0b85" BLOCK_SIZE="4096" TYPE="ext4"
Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 3.1

2. Edit the fstab File

Open the fstab file with a text editor

sudo nano /etc/fstab

Add the following line at the very bottom

#Auto MountUUID=uuid-disk-kamu /mnt/backup ext4 defaults 0 2

Replace uuid-disk-kamu with the UUID from the previous step, and adjust ext4 with your disk filesystem type.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 3.2

3. Test the fstab Configuration

Before rebooting, it is best to test the fstab configuration first so the VPS does not fail to boot due to a typo. Run

sudo mount -a

If no error appears, it means the fstab configuration is correct and the disk will be automatically mounted every time the VPS restarts.

Cara Mount dan Unmount Disk di VPS Linux - WPGan.com - 3.3

Commonly Used Mount and Unmount Commands

The following is a list of commands that are commonly used

  • lsblk View the list of disks and partitions
  • sudo blkid View UUID and filesystem type
  • sudo mount /dev/sdX /mnt/point Mount a partition
  • sudo umount /mnt/point Unmount a partition
  • df -h View currently mounted partitions
  • sudo fuser -vm /mnt/point Check processes holding a mount point
  • sudo mount -a Test fstab configuration

FAQ: How to Mount and Unmount Disks on a VPS

Is data lost when unmounting a disk?

No. As long as the unmount process runs normally, the data on the disk remains safe. Data is only at risk of being lost or corrupted if the disk is unmounted forcefully while there are still processes writing data.

Is it necessary to unmount before rebooting the VPS?

Not mandatory. The system will automatically unmount all disks safely when the shutdown or reboot process occurs normally.

Why does the disk not appear again after a reboot even though it was mounted?

Because manual mounting with the mount command is temporary. For the disk to be automatically mounted at every boot, you need to add its entry to /etc/fstab as in the steps above.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...