Best way to mount NTFS drive on login

I have an NTFS drive I mount with this command:

sudo mount -t ntfs /dev/nvme0n1p2 /mnt/ntfs

What is the best way to automate this so it runs automatically at login?

Probably by adding a line in /etc/fstab. However, be sure to include nofail or else your system might boot to a rescue prompt in the event that it is unable to mount the filesystem.

man fstab explains the different fields that go in the /etc/fstab file. For your example, that would probably be the following.

/dev/nvme0n1p2 /mnt/ntfs ntfs nofail 0 0

The fields are space-separated. The last two numbers indicate if the dump command (a legacy backup utility for ext filesystems) should include this filesystem and whether (and in what order) to run fsck on the filesystem during boot (this is also a legacy parameter). The last two numbers are optional and you will almost always want them set to zero. I still include them but it is mostly just out of habit.

If you want to test it before you reboot your PC, run systemctl daemon-reload to tell systemd to re-parse the config file, then run sudo mount -a to tell the OS to mount all filesystems (it will skip anything that is already mounted). Also, I think you can add user to the options list to allow the filesystem to be mounted by normal (not super) users (i.e. change the fourth field to user,nofail). If that works, then you should be able to run mount /mnt/ntfs (without sudo) if the drive wasn’t available at system startup or otherwise failed to mount. (Also, umount /mnt/ntfs should work.)

Edit: Oh, you said “at login”. In that case, make the fourth field user,noauto,nofail and write a systemd service to execute mount /mnt/ntfs. Put the systemd service in the ~/.config/systemd/user directory and enable it so it will run whenever you sign in. I’d add ConditionPathIsMountPoint=!/mnt/ntfs in the [Unit] section so the service won’t repeatedly mount the filesystem every time you sign in.

3 Likes

Just to add on to the previous answer, there are a few GUI options where you can add the fstab automatically from a GUI. KDE has Partition Manager and I believe it’s called Gnome Disk under Gnome.

In Partition Manger you can select the drive / partition, right click on it and there is an option to auto mount the filesystem, and then gives you a few options that are equivalent to the options above.

4 Likes

Can you explain this last part? I don’t have a directory named systemd under ~/.config.

I was able to find the “Edit mount point” menu from KDE Partition manager but I can’t find any GUI option to automatically mount the drive at login.

If that directory doesn’t already exist, you can just create it. Here is an example file I have in my ~/.config/systemd/user that runs on login.

$ cat ~/.config/systemd/user/fedora-sso.service
[Unit]
Description=Fedora Project SSO Service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=%h/bin/fedora-sso
Restart=on-failure
RestartSec=1

[Install]
WantedBy=default.target

It calls a Bash script that I have in my ~/bin directory to initialize my Fedora credentials (so I can auto-sign-in on several of Fedora’s services like Fedora Magazine and GitLab). The same concept should work for mounting a filesystem, but you need to make sure your user account can do it (without sudo) first (the service will run as your user account).

Added ntfs

I was having problems initially because the NTFS partition of interest was sometimes showing up as /dev/nvme0n1p2 and other times it would show up as /dev/nvme1n1p2 Thus, identitying the partition by device node was giving inconsistent results and I was ending up with multiple lines in /etc/fstab with /mnt/ntfs as the mount point. Therefore, I deleted all existing lines in /etc/fstab relating to the ntfs partition and then did systemctl daemon-reload.

Next I opened KDE Partition Manager and right-clicked on the ntfs partition and selected “Edit Mount Point.”

I made sure to choose “UUID” under “Identify by:” because of the aforementioned problem with identifying by device node, set “Path” to /mnt/ntfsand checked the box “Users can mount and unmount.”

I ended up with this line in /etc/fstab:

UUID=D274609474607D5B                       /mnt/ntfs   ntfs    nofail,users                                             0 0 

This seems to be all that needs to be done to make the NTFS partition mount automatically at login. It works fine without any additional steps. I believe that as long as the noauto option is excluded the partition is automatically mounted.

1 Like