How to mount disks in Linux

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
4,408
Reaction score
4,601
Credits
41,638
How to Mount Disks in Linux
Mounting disks in Linux can be done in several ways: by device path, by UUID, and by LABEL. Below are examples for each method, including fstab entries for persistence.
Step 1: Identify Your Disk
Code:
List all disks and partitions
fdisk -l
Or use lsblk for a cleaner view
lsblk
To get UUID or LABEL for a specific device
blkid /dev/sda2
Method 1: Mount by Device Path
Code:
sudo mount /dev/sda2 /mnt/data
fstab example:
Code:
/dev/sda2    /mnt/data    ext4    defaults    0 0
Method 2: Mount by UUID - The recommended method.
Find UUID:
Code:
blkid /dev/sda2
Example output:
Code:
/dev/sda2: UUID="123e4567-e89b-12d3-a456-426614174000" TYPE="ext4"
Mount command:
Code:
sudo mount UUID=123e4567-e89b-12d3-a456-426614174000 /mnt/data
fstab example:
Code:
UUID=123e4567-e89b-12d3-a456-426614174000    /mnt/data    ext4    defaults    0 0
Method 3: Mount by LABEL
Create a label:
Code:
For ext4
sudo e2label /dev/sda2 DATA
For XFS
sudo xfs_admin -L DATA /dev/sda2
Mount command:
Code:
sudo mount LABEL=DATA /mnt/data
fstab example:
Code:
LABEL=DATA    /mnt/data    ext4    defaults    0 0
Combined /etc/fstab Example (all three methods together):
Code:
/etc/fstab example with all three methods
Mount by device path
/dev/sda2    /mnt/data1    ext4    defaults    0 0
Mount by UUID
UUID=123e4567-e89b-12d3-a456-426614174000    /mnt/data2    ext4    defaults    0 0
Mount by LABEL
LABEL=DATA    /mnt/data3    ext4    defaults    0 0
Verify mount:
Code:
df -h
 
Last edited:


I shouldd've mentioned this part first
:)


Mount Points and Directories

When you mount a disk in Linux, you’re mounting it to a directory (called a mount point). That directory can be almost anywhere you have permission to create it. Most Linux distros use standard locations like /mnt or /media, but technically it doesn’t matter where you create it.

Important:

You cannot mount to a directory that doesn’t exist. You must create it first.
If the disk becomes unmounted, you can still write to that directory, but the data will go to your local filesystem instead of the intended disk. This can fill up your root partition quickly.

Example of creating a mount point:
Code:
sudo mkdir -p /mnt/data

Then mount your disk to that directory using any of the methods from the first post:
Code:
sudo mount /dev/sda2 /mnt/data

Tip: After editing /etc/fstab, you can apply changes without rebooting by running:
Code:
sudo mount -a
 
Just curious...

Suppose I have a directory called /tmp/jekyll_hyde with some data in it and then I mount an external filesystem containing different data on that directory. Clearly, I can only access one or the other set of data at any given time, depending upon whether or not the external filesystem is mounted. Other than the likelihood of user confusion, is there any risk to either set of data files?

Also, it seems like it might be a good idea, when creating an empty directory as a mountpoint, to add a file to it named something like "not_mounted.txt" to indicate that whatever you planned on mounting there has not yet been mounted. Once the fs has been mounted, this marker file would become invisible.
 
Suppose I have a directory called /tmp/jekyll_hyde with some data in it and then I mount an external filesystem containing different data on that directory. Clearly, I can only access one or the other set of data at any given time, depending upon whether or not the external filesystem is mounted. Other than the likelihood of user confusion, is there any risk to either set of data files?

Also, it seems like it might be a good idea, when creating an empty directory as a mountpoint, to add a file to it named something like "not_mounted.txt" to indicate that whatever you planned on mounting there has not yet been mounted. Once the fs has been mounted, this marker file would become invisible.

Typically no, the filesystems are different, on different physical disks. "other than the likelihood of confusion".
Never tried the marker file, but it sounds like a good idea. I don't see a downside.
 


Follow Linux.org

Members online


Top