Linux - How to mount and unmount a drive

  • Last updated: Nov 3, 2023
  • Views: 758
  • Author: Admin
Linux - How to mount and unmount a drive

Hello colleagues.

In today's article, the process of mounting and unmounting a disk under the control of the Linux operating system will be considered.

One of the main features of the Linux OS is that it has the ability to mount a disk (or other block device) to any of the directories, as long as the directory is empty. All steps in this article must be performed in privileged mode or as root . All disks in Linux are stored in the /dev/ directory and you can’t just work with them directly, that is, you can’t write files to them or copy files from them. To work with them, they need to be mounted, and we will use the  mount command for this. The disk is connected to the mount point, in our case it will be a directory and it will be possible to work with this directory.

In our example, I will mount the /dev/sdb1 partition that I created on the sdb drive and I will mount this partition to the /app directory that we will create.

 

Command syntax:

sudo mount /dev/sdb1 /directory_name

  •  /dev/sdb1 - Partition on disk.
  • /directory_name - The name of the directory to which we will mount the partition (disk).

 

Article content:

  1. We create a directory.
  2. Mount the partition (disk) to the directory.
  3. We allow access to the partition (disk).
  4. Automatic partition (disk) mounting on Linux boot.
  5. From mounting the partition (disk).

 

1. Create a directory.

Let's start with the fact that first we will create a directory to which the partition (disk) will be mounted.

$. sudo mkdir /app

linux mount unmount disk


 

2. Mount the partition (disk) to the directory.

$. sudo mount /dev/sdb1 /app

linux mount unmount disk

In the example, we first check all directories, then mount the partition (disk) to the directory, and then check back. After applying the command, we have the /app directory and a partition (disk) /dev/sdb1 with a size of 16 GB is mounted to it.


 

3. We allow access to the partition (disk).

After we have mounted the partition (disk), now we need to assign the necessary rights to this partition (disk) so that users can read and write to this partition (disk).

$. sudo chmod 0777 /app

In fact, you can fine-tune the permissions on a partition (disk) without full permission for all users, but in my example, I allow everything to be done with this partition (disk).


 

4. Automatic partition (disk) mounting when Linux boots.

After rebooting the server, your partition (disk) will not be mounted and everything you did will be reset, but in order for this not to happen and for your partition (disk) to be mounted automatically after the server is rebooted, we must add something to the  /etc/fstab file.

Open the file  /etc/fstab

$. vim /etc/fstab

and add the line to it:

/dev/sdb1 /app ext4 defaults 0 0

linux mount unmount disk

  • /dev/sdb1 - The directory where you mount the partition (disk).
  • /app - Directory name.
  • ext4 - File system type.
  • defaults 0 0 - Access parameters.

We save and close the file. Now when your server reboots, the partition (disk) will be automatically mounted to the directory that you specified in the file.


 

5. From mounting the drive.

If you want to mount a partition (disk) from a directory, then you need to run a simple command:

$. sudo umount /dev/sdb1

linux mount unmount disk

And do not forget to remove the line from the /etc/fstab file so that after the server is rebooted, the partition (disk) is not automatically mounted back to the directory.


 

Thank you all, I hope my article was of some help to you.

SIMILAR ARTICLES

How to check linux OS version

How to check linux OS version

lsof utility - find out what ports are listening in Linux

lsof utility - find out what ports are listening in Linux

Download and install CentOS 8

Download and install CentOS 8