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 withmkfs.ext4, if it already has content you can skip this step. Create a mount point withmkdir, then mount the partition withmount /dev/sdX /mnt/backup. To release it, useumount /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
lsblkThis command will display all disks and partitions installed on the VPS, including those that are not yet mounted.
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 blkidNote the device name of the partition you want to mount, for example /dev/vdb.
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 -fIf 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/vdbReplace /dev/vdb with the device name according to the results of lsblk in the previous step.
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/backupCheck 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.
/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/backupThis command will connect the partition /dev/vdb to the directory /mnt/backup, so you can immediately read and write data in that directory.
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.
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/backupYou can also unmount based on the device name directly
sudo umount /dev/vdb2. 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/backupAfter knowing which process is holding it, you can stop it forcefully with
sudo fuser -km /mnt/backupAfter 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/vdbNote 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"2. Edit the fstab File
Open the fstab file with a text editor
sudo nano /etc/fstabAdd the following line at the very bottom
#Auto MountUUID=uuid-disk-kamu /mnt/backup ext4 defaults 0 2Replace uuid-disk-kamu with the UUID from the previous step, and adjust ext4 with your disk filesystem type.
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 -aIf no error appears, it means the fstab configuration is correct and the disk will be automatically mounted every time the VPS restarts.
Commonly Used Mount and Unmount Commands
The following is a list of commands that are commonly used
lsblkView the list of disks and partitionssudo blkidView UUID and filesystem typesudo mount /dev/sdX /mnt/pointMount a partitionsudo umount /mnt/pointUnmount a partitiondf -hView currently mounted partitionssudo fuser -vm /mnt/pointCheck processes holding a mount pointsudo mount -aTest 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.
COMMENTS
Loading...