Mounting external drive on Linux
Give permissions to specific user for writing into an external drive on Linux
Assumptions
These are steps I have tried when installing Nextcloud on my raspberry pi with external drives attached to it.
The uid and gid for www-data is 33. You will need to check it before you go ahead with the steps below.
Steps
Install
exfat-fuse
andexfat-utils
sudo apt update sudo apt install exfat-fuse exfatprogs -y
Check existing mounts
df -h #this will list all the mount points mount | grep /dev/sdX1
Unmount the mounted drive
sudo umount /dev/sdX1
Attempt mounting by adding the uid and gid of the user or program you need to add the permission for
sudo mount -t exfat /dev/sdX1 /mnt/external-storage -o uid=33,gid=33,fmask=0022,dmask=0002
If there is any error like below
fuse: mount failed: Device or resource busy
Check if there was an auto-mount in the media folder.
sudo lsof | grep /mnt/external-storage sudo kill -9 <PID>
Attempt mounting
sudo mount -t exfat /dev/sdX1 /mnt/external-storage -o uid=33,gid=33,fmask=0022,dmask=0002
Test write permissions
sudo -u www-data touch /mnt/external-storage/testfile
Hope this helps. I was literally struggling to do this for a few days.