Mount drive in linux and set auto-mount at boot

Before a user can access a file on a Unix-like machine, the file system on the device which contains the file needs to be mounted with the mount command. Guide to mount a drive in linux (deb/ubuntu) and set to auto-mount at boot

Guide to mount a drive in linux (deb/ubuntu) and set to auto-mount at boot.

Mount drive

Make a folder (will be mount point)

sudo mkdir /mnt/sda2
sudo mount /dev/sda2 /mnt/sda2
 

Now you can access the drive at /media/data.

Auto-mount at boot

We want the drive to auto-mount at boot.  This usually means editing /etc/fstab.

Firstly, it's always best to use the drives UUID.  To find the drive's UUID do

ls -al /dev/disk/by-uuid/​

Copy the resultant UUID (for your disk) and then open fstab for editing (note I'm using vim here but use whatever editor you prefer):

sudo nano /etc/fstab​

You want to add an entry for the UUID and mount point.  Below is an example of an fstab file with an entry added for the mount above:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-qdPuDniIfcqdrFWkuENf16BgpJobCji8kktAcFMgQBapapOgfCHocadgPOyOvzpT / ext4 defaults 0 1
# /boot was on /dev/sdb2 during curtin installation
/dev/disk/by-uuid/af21c996-833d-49cf-aefc-6815f13ca8c8 /boot ext4 defaults 0 1
# /boot/efi was on /dev/sdb1 during curtin installation
/dev/disk/by-uuid/3349-38E7 /boot/efi vfat defaults 0 1
#/swap.img      none    swap    sw      0       0
UUID=9aebc045-a965-4738-aabb-ec1044571b3d /mnt/sda1 ext4 defaults 0 0
UUID=54bd2ade-6520-44e8-9ad1-f02c357b7ad8 /mnt/sda2 ext4 defaults 0 0

Note: the entry added is the last line.

Test fstab

We always want to test the fstab before rebooting (an incorrect fstab can render a disk unbootable).  To test do:

findmnt --verify​

check the last line for errors.  Warnings can help in improving your fstab.

Unmounting drive with umount

You can unmount drives using umount.  For example, to unmount the data drive above mount at /media/data you would do:

sudo umount /mnt/sda2​


Description

Contact

VNAppMob
Vietnam
+84965235237

Social