sodcalculator

10 Tech How-To

Format and Mount a Disk on a Linux VPS

Guide 10-29002 · Updated · Step by step

This guide walks you through attaching a new or existing disk to your Linux VPS, formatting it with a filesystem, and mounting it so you can store data on it. You’ll learn how to identify the correct device, create a partition, format it, and make the mount permanent across reboots.

What You’ll Need

  • Root or sudo access to your Linux VPS
  • An attached disk (in addition to your system disk) – often added via your hosting control panel
  • Basic comfort with the command line

Step-by-Step

  1. Identify the new disk
    Run lsblk to list all block devices. Look for a device that is not mounted and has no partitions (e.g., sdb or vdb). Also run sudo fdisk -l to confirm its size and path. Be absolutely sure you have the right device – formatting the wrong disk will destroy data.
  2. Create a partition (optional but recommended)
    Use sudo fdisk /dev/sdb (replace sdb with your device). Inside fdisk:
    Type n to create a new partition, press Enter to accept defaults for partition number and first/last sector, then type w to write the partition table. This creates /dev/sdb1.
  3. Format the partition
    Format with ext4 (widely compatible): sudo mkfs.ext4 /dev/sdb1. If you prefer XFS, use sudo mkfs.xfs /dev/sdb1. Confirm the operation when prompted.
  4. Create a mount point
    Choose a directory where the disk will be accessible, e.g., /mnt/data. Create it with sudo mkdir -p /mnt/data.
  5. Mount the disk temporarily
    Run sudo mount /dev/sdb1 /mnt/data. Verify with df -h – you should see the new filesystem mounted.
  6. Make the mount permanent
    Get the UUID of the partition: sudo blkid /dev/sdb1. Edit /etc/fstab with sudo nano /etc/fstab and add a line like:
    UUID=your-uuid-here /mnt/data ext4 defaults 0 2
    Replace the UUID and adjust the filesystem type if you used XFS. Save and exit.
  7. Test the fstab entry
    Run sudo umount /mnt/data then sudo mount -a. If there are no errors, the mount is configured correctly. Reboot or run sudo mount -a again to confirm it persists.

Common Mistakes

  • Formatting the wrong device – Always double-check with lsblk and fdisk -l. A typo like sda instead of sdb can wipe your OS.
  • Forgetting the partition – Some guides skip partitioning, but most VPS disks benefit from a partition table. If you format the whole disk without a partition, some tools won’t recognize it.
  • Using the wrong filesystem type in fstab – If you formatted with XFS but put ext4 in fstab, the mount will fail at boot.
  • Not testing fstab – A bad fstab entry can prevent your VPS from booting. Always test with mount -a after editing.

Tips

  • Use a dedicated mount point like /mnt/data or /srv/data rather than mounting at / or /home to avoid conflicts.
  • Consider using LVM if you plan to resize the disk later – it’s more flexible but more complex.
  • For SSDs, you can add discard to the fstab options to enable TRIM, but this may reduce performance on some systems.
  • Always back up important data before formatting, even if the disk is new.
  • If the disk is larger than 2TB, use GPT partitioning instead of MBR (fdisk handles this automatically on modern systems).

Troubleshooting

  • Mount fails with “wrong fs type” – Check that the filesystem type in fstab matches what you formatted with. Run sudo blkid to confirm.
  • Device not showing up – Rescan the SCSI bus with echo 1 > /sys/class/scsi_disk/0:0:0:0/device/rescan (replace the identifier) or reboot the VPS.
  • Can’t write to the mounted disk – Check permissions: sudo chown -R $USER:$USER /mnt/data or set appropriate permissions.
  • fstab entry causes boot failure – Boot into recovery mode or use a rescue console, comment out the line, and fix the entry.
  • UUID not found – If you formatted the whole disk without a partition, the device might be /dev/sdb instead of /dev/sdb1. Adjust accordingly.

Frequently Asked Questions

Can I mount a disk without partitioning?

Yes, you can format the whole disk (e.g., mkfs.ext4 /dev/sdb) and mount it directly. However, partitioning is recommended for flexibility and to avoid issues with some tools.

What filesystem should I use?

Ext4 is the safest default for most VPS workloads. XFS is better for very large files or high throughput. Btrfs offers snapshots but is more complex.

How do I unmount a disk?

Use sudo umount /mnt/data. If it says “target is busy”, close any open files or processes using that mount, or use sudo lsof +f -- /mnt/data to find them.

What does the ‘0 2’ at the end of fstab mean?

The first number is the dump option (0 disables), the second is the fsck order (2 means check after root filesystem). For non-root filesystems, 2 is standard.

Can I resize the disk later?

If you used a partition, you can extend it with growpart and resize2fs after increasing the disk size in your hosting panel. With LVM it’s even easier.

Is it safe to mount a disk at /home?

Yes, but be careful: if you mount over an existing /home, the old files become hidden until unmounted. Better to mount at a separate path and symlink if needed.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →