How to mount/automount USB drive on Linux Systems.
Mounting and automounting USB drives are key for managing removable storage on Linux. Whether you want to manually mount a USB drive or set up automatic mounting, understanding these processes can boost productivity and streamline your workflow.
In this guide, we will walk you through the steps to manually mount a USB drive, and then show you how to configure your system for automounting to ensure a smooth and efficient experience with removable storage on Linux.
Step 1 - Detecting USB drive
After you plug in your USB device, locate your USB drive, which might be listed as something like /dev/sdb
or /dev/sdc
. Make sure to identify the correct device to avoid data loss. However, at this stage, the device cannot be used yet, as the filesystem on the USB drive needs to be mounted before you can access or store any data.
To find out the name of your block device, run the following command:
$ sudo fdisk -l
NOTE: fdisk
command required administrative privileges to access the required information, thus from this reason, the command needs to be executed as a root user or with sudo
The output should be like this:
$ sudo fdisk -l
Disk /dev/sdb: 7.21 GiB, 7739768832 bytes, 15116736 sectors
Disk model: ImationFlashDriv
Units: sectors of 1 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc3072e18
Device Boot Start End Sectors Size Id Type
/dev/sdb1 8064 15116735 15108672 7.2G b W95 FAT32
Take note of the block device name of the partition you intend to mount, in our case that will be /dev/sdb
with FAT32 filesystem.
Partition in Linux.
In Linux, /dev/sdb
and /dev/sdb1
refer to different levels of storage devices:
1. /dev/sdb
:
• Represents the entire physical storage device (e.g., a USB drive or hard disk).
• It is the block device node for the whole disk.
• This device might contain multiple partitions.
2. /dev/sdb1
:
• Refers to a specific partition on the /dev/sdb
device.
• The number 1 indicates the first partition on the disk. Subsequent partitions would be numbered /dev/sdb2
, /dev/sdb3
, and so on.
• Partitions are used to divide the physical disk into separate sections, each of which can have its own filesystem.
When mounting, you typically use the partition /dev/sdb1
, as it contains the filesystem you want to access.
Step 2 - Create a mounting point
You need to create a mount point, which can be any new or existing directory within your system. To create a new mount point directory where you want to mount your USB device, use themkdir
command:
$ sudo mkdir /mnt/SNUB-drive
Step 3 - Mounting the Partition
We are now ready to mount our USB's partition identifier /dev/sdb1
to /mnt/SNUB-drive
mount point as below:
$ sudo mount /dev/sdb1 /mnt/SNUB-drive
To verify whether your USB drive has been mounted correctly, you can use the mount
command without any arguments and filter the output using grep
to search for your USB block device name. Here’s how you can do it:
1. Run the mount Command
$ sudo mount
2. Filter the Output
Use grep to search for your USB block device name (e.g., /dev/sdb1). Replace sdb1 with your actual device name:
$ sudo mount | grep /dev/sdb1
This command will show if /dev/sdb1
is mounted and display the mount point. If the device is mounted, you should see an output similar to:
/dev/sdb1 on /mnt/SNUB-drive type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)
This indicates that /dev/sdb1
is mounted at /mnt/SNUB-drive
with the vfat filesystem type. If there is no output, the device might not be mounted or might have been mounted at a different directory.
Step 4 - Formatting the USB Drive (if needed)
Formatting a USB drive on Linux involves preparing the drive with a specific filesystem, which will erase all existing data.
Here's a step-by-step guide:
Unmount the USB Drive
If the USB drive is already mounted, unmount it using:
$ sudo umount /dev/sdb1
Of course, replace /dev/sdb1
with the appropriate partition identifier.
Format the USB Drive
Use the mkfs
command to format the drive. Choose the filesystem you want (e.g., ext4, FAT32, NTFS).
Here are examples for different filesystems:
• For vfat
$ sudo mkfs.vfat /dev/sdb1
• For NFFS
$ sudo mkfs.ntfs /dev/sdb1
• For EXT4
$ sudo mkfs.etx4 /dev/sdb1
When you are asked to confirm your choice, just press "y" and wait as the selected partition would be wiped.
Step 5 - Accessing your USB Data
We can now access our USB data simply by navigating to our previously created mount point /mnt/SNUB-drive
:
cd /mnt/SNUB-drive
Step 6 - Automount
In order to automatically mount a drive at boot time run the the following command to see the name of your drive, its UUID (Universal Unique Identifier) and file system type. Here's how you can tackle it:
$ sudo blkid
The above command will output information about all block devices; similar to this:
/dev/mmcblk0p1: LABEL_FATBOOT="system-boot" LABEL="system-boot" UUID="B726-57E2" TYPE="vfat" PARTUUID="ab86aefd-01"
/dev/mmcblk0p2: LABEL="writable" UUID="483efb12-d682-4daf-9b34-6e2f774b56f7" TYPE="ext4" PARTUUID="ab86aefd-02"
/dev/sda: UUID="0d28feb1-b6c4-4788-a4db-22238b82f638" TYPE="ext4"
/dev/sdb1: UUID="1a5e99de-399b-4d46-b8c6-ac2e51df1fe9" TYPE="ext4"
As you can see the UUID of our USB Drive /dev/sdb1
is 1a5e99de-399b-4d46-b8c6-ac2e51df1fe9 and was formatted with the ext4 file system.
Edit /etc/fstab
File
The /etc/fstab
file lists all available disks and partitions and specifies how they should be initialized and integrated into the system’s filesystem. It provides essential information about each device, including the device identifier, mount point, filesystem type, mount options, and parameters for filesystem checks and backups. This configuration ensures that disks and partitions are automatically mounted according to the specified settings each time the system boots.
Edit the /etc/fstab
file:
$ sudo nano /etc/fstab
Add this line of code at the end of the file as shown below:
UUID=<uuid-of-your-drive> <mount-point> <file-system-type> <mount-option> <dump> <pass>
Note that you must separate these items using the Tab key.
In our case, it looks like this:
UUID=1a5e99de-399b-4d46-b8c6-ac2e51df1fe9 /mnt/SNUB-drive ext4 defaults 0 2
Save and close the file and run the following command to mount all filesystems listed in the /etc/fstab
file and see if it works.
$ sudo mount -a
Here is a full description of each line:
Device Identifier: The UUID, label, or device path (e.g., /dev/sdb1 or UUID=1a5e99de-399b-4d46-b8c6-ac2e51df1fe9).
Mount Point: The directory where the device will be mounted (e.g., /mnt/usb).
Filesystem Type: The type of filesystem used (e.g., ext4, vfat).
Mount Options:
These settings control how the filesystem is mounted. Common options include:
-defaults: Uses default mount settings.
-noatime: Disables updating of access times to improve performance.
-ro: Mounts the filesystem as read-only.
-rw: Mounts the filesystem with read and write permissions.
-user: Allows non-root users to mount the filesystem.
Dump and fsck Options:
-Dump Option: This value (typically 0 or 1) indicates whether the filesystem should be backed up by the dump utility. 0 means no backup, while 1 means it is included in backups.
-fsck Option: This value (typically 0, 1, or 2) determines the order in which filesystems are checked by the fsck utility at boot. 0 means the filesystem will not be checked, 1 is usually for the root filesystem, and 2 is for other filesystems to be checked sequentially.
Additional Tips
Label the Filesystem
Labeling a filesystem provides a human-readable name for it, especially when managing multiple disks or partitions.
• For ext4 Filesystems
Use e2label
$ sudo e2label /dev/sdb1 SNUBmonkey-drive
• For vfat Filesystems
Use dosfslabel
$ sudo dosfslabel /dev/sdX1 SNUBmonkey-drive
Of course, replace /dev/sdb1
with your device identifier as well as SNUBmonkey-drive with your desired label.
Congratulations!
We hope this was of great use!
You now know "How to mount an USB drive in a Linux System".