This section contains all things related to volume management, including mounting and unmounting of external drives, partitioning, formatting and encrypting of storage media.
Usually, this is very intuitive to do from a GUI (comparable to Windows), but it might require a few more steps when it comes to doing it from the terminal only (for example, through SSH).
Whereas in a GUI environment your USB devices may auto-mount or at least be identified by some familiar name like "USB Device" or "Seagate XYZ," in the terminal all you have to work with are device files, under /dev/
and usually following the convention of sdX
for hard drives and USB drives or mmcblkX
for SD cards.
Since it's not always obvious from a first look, and one mistake from a typo could affect a completely different device, it's crucial to identify which device file points to your intended device. There are multiple ways to do this, including comparing all existing device files before and after plugging in a USB. The fastest and most straightforward way to me is the lsblk
command:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1.8T 0 disk
├─sda1 8:1 0 931.3G 0 part /media/user/ext
├─sda2 8:2 0 279.4G 0 part
sdb 8:16 1 119.3G 0 disk
└─sdb1 254:0 0 119.3G 0 disk /media/user/tmp
mmcblk0 179:0 0 28.9G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot
└─mmcblk0p2 179:2 0 28.6G 0 part /
Though not perfect, it helps you identify a few things: first, the device file under NAME
and perhaps most importantly the SIZE
of the storage medium, which is usually enough to conclude which is the device you're looking for.
In the example above, /dev/sda
is a 1.8 Terabyte medium, /dev/sdb
is a 119 GB disk, and /dev/mmcblk0
is a 28.6 GB SD card. Keep in mind that often the storage reported by lsblk
is lower than the actual capacity of the devices (for example, sdb
in this case can actually hold up to 128GB), but by approximation you can still guess correctly.
If you must be absolutely sure about which device is which, or to accurately identify all storage devices, however, your best option is fdisk
. You must use it with sudo
or root permissions:
$ sudo fdisk -l
Disk /dev/sda: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: Vendor and Model Name
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: gpt
Disk identifier: 23EA-A30D
Device Start End Sectors Size Type
/dev/sda1 4096 618495 614400 300M EFI System
/dev/sda2 618496 920446850 919828355 438.6G Linux filesys
/dev/sda3 920446851 937697984 17251134 8.2G Linux filesys
Disk /dev/mapper/luks-200-482b-8c58: 438.61 GiB, 470950020608 bytes, 919824259 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
The example above shows a single-hard drive (/dev/sda
) Linux system, where there are three partitions:
sda1
)sda2
)sda3
)Additional media will be shown if you connect it via USB.
Mounting is done via the mount
command. To mount a device, you must have the device file that you'd like to mount and a directory to act as a mountpoint. Traditionally, devices are mounted in the /media/
or /mnt/
directories, but you can mount it anywhere you have write permissions.
To mount a device (for example, /dev/sdb
) run:
sudo mount /dev/sdb /media/user/some_mount_directory
Now if you change to the /media/user/some_mount_directory
, the contents of the device will be visible.
To unmount the device, issue the umount
command and the mountpoint:
sudo umount /media/user/some_mount_directory
Mounting an encrypted drive is similar to an unencrypted one, with one difference: you have to temporarily decrypt the device and leave it "open" within the /dev/mapper/
directory while you work on it. You need the cryptsetup
package in order to accomplish this.
Plug in your USB device, and find out its dev
file using lsblk
or a similar method:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
...
sdb 8:16 1 119.3G 0 disk
...
In this example, my device is /dev/sdb
.
Open the LUKS encrypted module first via cryptsetup
. You need to specify an "identifier" that could be any string. That identifier will become a file under the /dev/mapper/
directory, and from which the actual mounting will be done:
sudo cryptsetup luksOpen /dev/sdb mysecret
# ^^^^^^^^-> this is the identifier.
You'll be prompted for a password to unlock your encrypted volume. Type it and press Enter.
You can now mount the "opened" encrypted volume under /dev/mapper
like any other device:
sudo mount /dev/mapper/mysecret /mnt/some_mountpoint
After you're done working on the encrypted drive and would like to remove it, first unmount it:
sudo umount /mnt/some_mountpoint
Then "close" the LUKS container by the identifier:
sudo cryptsetup luksClose mysecret
You can then remove it safely.
Warning: all data contained in the USB drive will be lost in the process of formatting.
The command mkfs
formats a device file to a given filesystem specification. In most systems, it is available in a few extensions to match which filesystem you'd like to use, for example mkfs.vfat
for the FAT-32 filesystem used in many USB sticks.
To format an external USB drive identified by /dev/sdb
as the Ext4 filesystem, run:
sudo mkfs.ext4 /dev/sdb
If there are other subpartitions in the device itself (ex: if the drive was used as a Linux live medium), mkfs
will complain. You can force mkfs
to format the entire medium using the -I
flag:
sudo mkfs.ext4 -I /dev/sdb
Likewise, you can only format a specific partition within the device as well:
sudo mkfs.ext4 /dev/sdb1
This command is useful to "rescue" USBs that are identified by the system (i.e. appear within the /dev/
files) but are not mountable by a file manager. Upon formatting, they appear brand-new again.