For Linux users, seamless interaction with external drives is essential, especially when dealing with NTFS-formatted USB drives. However, mounting these drives often comes with limitations in terms of file permissions and performance. By implementing custom configurations to automate the mounting process, you can enhance your file access experience, streamline workflows, and eliminate the need for manual remounting every time you connect a drive. In this article, we will explore how to set up your Linux system to automatically mount NTFS USB drives with optimized options using systemd, udev rules, and the fstab file.

Method 1: Using Udev Rules and Systemd Mount Units

Step 1: Identify Your NTFS Partition’s UUID

First, plug in your NTFS USB drive. To retrieve the UUID of your partitions, execute the following command:

lsblk -o NAME,UUID,FSTYPE,MOUNTPOINT

This command will display device names, UUIDs, file system types, and mount points. Find and note the UUID of the NTFS partition you wish to configure.

Step 2: Create a Mount Point Directory

Choose a location to mount your NTFS drive. A common choice would be /media/usb-ntfs. If this directory doesn’t already exist, you can create it with:

sudo mkdir -p /media/usb-ntfs

Step 3: Create a Systemd Mount Unit File

Next, you will create a systemd mount unit file to define how the NTFS partition is to be handled. Create a file named /etc/systemd/system/media-usb\x2dntfs.mount. Ensure that the filename accurately reflects the mount point, replacing slashes with dashes.

READ 👉  Can You Use Linux Without the Terminal? Yes—Here’s How

In this file, insert the following content, making sure to replace your-ntfs-uuid-here with the UUID you noted earlier:

[Unit]
Description=Mount NTFS USB drive automatically

[Mount]
What=UUID=your-ntfs-uuid-here
Where=/media/usb-ntfs
Type=ntfs-3g
Options=uid=1000,gid=1000,umask=022,windows_names

[Install]
WantedBy=multi-user.target

The Options line specifies owner IDs, permissions, and filename compatibility with Windows systems.

Step 4: Reload Systemd Daemon and Enable the Mount Unit

To apply your changes, reload the systemd daemon and enable the newly created mount unit so it activates at boot or whenever the drive is connected:

sudo systemctl daemon-reload
sudo systemctl enable --now media-usb\x2dntfs.mount

Now, your NTFS partition will mount automatically with specified options every time it is connected.

Method 2: Using Udev Rules with Custom Mount Scripts

Step 1: Create a Custom Mount Script

For a more dynamic approach, create a custom mount script that specifies how your NTFS USB should be mounted. Create a script named /usr/local/bin/auto-mount-ntfs.sh and edit it to include:

#!/bin/bash
mount -t ntfs-3g -o uid=1000,gid=1000,umask=022,windows_names $1 /media/usb-ntfs

Make the script executable with the following command:

sudo chmod +x /usr/local/bin/auto-mount-ntfs.sh

Step 2: Create a Udev Rule

Now, you need to set up a udev rule to execute your script when an NTFS USB partition is connected. Create a new rules file at /etc/udev/rules.d/99-ntfs-automount.rules and add this line:

KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", ENV{ID_FS_TYPE}=="ntfs", ACTION=="add", RUN+="/usr/local/bin/auto-mount-ntfs.sh %N"

This rule ensures your script runs every time an NTFS drive is plugged in.

Step 3: Reload Udev Rules

To make your changes effective, reload the udev rules:

sudo udevadm control --reload-rules
sudo udevadm trigger

Now whenever you connect an NTFS USB drive, it will mount automatically using your specified options.

Method 3: Using fstab for Static Devices

If you consistently use the same external drive, adding an entry in /etc/fstab simplifies the mounting process further.

READ 👉  Linux df Command: Check Disk Space and Mounted Filesystems

Step 1: Edit fstab

Open the fstab file with root privileges:

sudo nano /etc/fstab

Add the following line, again replacing your-ntfs-uuid-here with the UUID of your device:

UUID=your-ntfs-uuid-here   /media/usb-ntfs   ntfs-3g   uid=1000,gid=1000,umask=022,windows_names   0   0

Save and close the file, then mount all filesystems to apply the configuration:

sudo mount -a

Conclusion

By automating the mounting process of NTFS USB drives, Linux users can significantly enhance their file access and management experience. Whether using systemd, udev rules, or the fstab approach, these methods streamline the integration of external drives into your workflow, ensuring consistency and efficiency. Experiment with these techniques to find the best fit for your specific use case, and enjoy the improved performance and usability on your Linux system.

Did you enjoy this article? Feel free to share it on social media and subscribe to our newsletter so you never miss a post!

And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
Buy Me a Coffee

Categorized in: