When you plug a USB flash drive into a Windows or macOS computer, the operating system instantly recognizes it, assigns it a drive letter (like D:), and pops open a graphical window showing you the files. When you are operating a headless Linux server via the terminal, none of this automatic magic happens. The Linux kernel will recognize that a piece of hardware has been plugged into the USB port, but it will not automatically make the files on that drive accessible. To read or write data to the drive, you must manually “mount” its filesystem to a specific directory using the mount command.
Step 1: Identify the Drive
Before you can mount the drive, you must figure out what hardware name Linux assigned to it when you plugged it in. In Linux, all hardware devices are represented as files located in the /dev/ directory (usually named something like /dev/sdb1 or /dev/sdc1).
- Plug your USB drive into the computer.
- Open your terminal and type the command:
lsblk - Press Enter.
The lsblk (list block devices) command will output a tree diagram of all your drives. Look for the new drive based on its size (e.g., if you plugged in a 16GB flash drive, look for a device listed as 14.9G). Note the specific partition name, such as sdb1. Its full path will be /dev/sdb1.
Step 2: Create a Mount Point
The mount command works by grafting the USB drive’s filesystem onto an existing, empty directory on your Linux server. This empty directory is called a “mount point.”
- Create a new directory using the
mkdircommand. The standard convention is to create these inside the/mnt/or/media/directories. Type:sudo mkdir /mnt/usb_drive - Press Enter.
Step 3: Mount the Drive
Now that you know the device name (/dev/sdb1) and you have created a mount point (/mnt/usb_drive), you can attach them together.
- Type the mount command, specifying the device first, followed by the mount point:
sudo mount /dev/sdb1 /mnt/usb_drive - Press Enter.
If the command succeeds, it will execute silently without outputting any text. You can now use the cd /mnt/usb_drive command and run ls to view and interact with all the files stored on your USB stick.
Step 4: Safely Unmount the Drive
You must never physically yank a USB drive out of a Linux machine while it is mounted. Doing so guarantees data corruption because Linux heavily utilizes write-caching (holding data in RAM before writing it to the physical disk). You must explicitly tell the OS to flush the cache and disconnect the drive using the umount (unmount) command.
- Ensure you are not currently inside the
/mnt/usb_drivedirectory (typecd ~to go home). - Type the command:
sudo umount /mnt/usb_drive - Press Enter.
Once the command completes, it is completely safe to physically unplug the USB drive from the computer.