To mount a partition into a directory in Linux, you can use the mount command. Here are the steps:
1. Identify the Partition :
First, you need to identify the partition that you want to mount. You should know the device file for the partition, such as /dev/sdXN, where X is the drive identifier (e.g., sda) and N is the partition number (e.g., 1,2,3 etc.,).
Use the below command to identify the partition which is not mounted to any directories.
sudo lsblk
From the below command, you can figure out more details about the partitioned disks.
sudo parted /dev/sdX print
As you can see from the above figure, the logical partitions /dev/sda5, /dev/sda6, /dev/sd7 are created under the extended partition /dev/sda1.
2. Create a Mount Point :
Next, you will need to create a directory (folder) that will serve as the mount point for the partition. You can create this directory anywhere you like, but it’s common to create it in the /mnt directory or under the /opt directory.
For example, to create a mount point named “software” in the /opt directory, you can use the following command:
sudo mkdir /opt/software
3. Mount the Partition :
Now, you can use the mount command to mount the partition. The basic syntax is:
sudo mount /dev/sdXN /mount_point
Replace /dev/sdXN with the actual device file of your partition and /mount_point with the path to the directory you created in step 2. If you are getting message as shown in above figure, execute below command and then mount the directory.
For formatting the filesystem in ext4 format :
sudo mkfs.ext4 /dev/sdXN
For formatting the filesystem in xfs format :
sudo mkfs.xfs /dev/sdXN
If you want to mount /dev/sda5 into the /opt/software directory, you would run:
sudo mount /dev/sda5 /opt/software
To verify the mounted filesystem, use lsblk command.
4. Access the Mounted Partition :
Once the partition is mounted, you can access its contents by navigating to the mount point directory (/opt/software in this example). You can use standard file commands (e.g., ls, cp, mv, rm, touch) to work with the files and directories on the mounted partition.
5. Automount on Boot (Optional) :
If you want the partition to be automatically mounted each time you boot your system, you can add an entry to the /etc/fstab file. This file contains information about partitions and their mount points. Here’s an example entry:
/dev/sdXN /mount_point filesystem defaults 0 0
Replace /dev/sdXN and /mount_point with the appropriate values for your partition and mount point. The filesystem field should be the file system type (e.g., ext4, ntfs, vfat). The defaults option usually works for most cases. The last two fields (0 and 0) are for filesystem checks and backups and can typically be left as-is.
After adding the entry to /etc/fstab, the partition will be automatically mounted during system boot.
Remember to replace placeholders in the commands and file paths with the actual values corresponding to your setup.
6. Unmounting the Partition :
When you’re done working with the partition and want to safely disconnect it, you can unmount it using the umount command:
sudo umount /mount_point
For example:
sudo umount /opt/software
Make sure you’re not currently using any files or directories within the mounted partition before unmounting it.
Click here!! to know how to create a logical partions in linux.
How do you feel about this post? Drop your comments below..