I have encountered a very strange behaviour I do not know how to solve: the action permissions depend on where the command is executed.
My situation: I set up a Nextcloud server on a Raspberry Pi 4. The system works off of an SD card and the Nextcloud instance is on an SSD, run via Docker (using IOTstack). As far as Nextcloud is concerned, everything works as expected.
When I want to list the contents of the Nextcloud instance, I may use the root account as such:
sudo ls -lh /media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files
total 4.0K
drwxr-xr-x 3 www-data www-data 4K Nov 3 11:09 Documents
Of course, this works because of root. When I try the normal account pi then I get the following:
ls -lh /media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files
ls: cannot access '/media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files': Permission denied
That's because everything belongs to www-data, so I try as www-data and I get:
sudo -u www-data ls -lh /media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files
ls: cannot access '/media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files': Permission denied
This is not normal... I should be able to see the files of the user they belong to.
I tried to cd to the mounted SSD (storage) and execute the command again:
cd /media/pi/storage/
sudo -u www-data ls -lh ./IOTstack/volumes/nextcloud/html/data/solenoid/files
total 4.0K
drwxr-xr-x 3 www-data www-data 4K Nov 3 11:09 Documents
Why? How come the permissions depend on from where the ls command is executed? Why can I list the files from /media/pi/storage and not from /home/pi, as the www-data user? Should www-data be in the pi group?
Previously I had the data stored on the SD card, under /home/pi/IOTstack..., but I wanted to store my files in a more safe location, while keeping the option of swapping the hardware. I did not encounter this issue.
The reason for accessing files in this "raw" way is to back them up over the network to another storage media.
Edit
Answering @kamil-maciorowski
namei -nom /media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files
f: /media/pi/storage/IOTstack/volumes/nextcloud/html/data/solenoid/files
drwxr-xr-x root root /
drwxr-xr-x root root media
drwxr-x--- pi pi pi
drwxr-xr-x pi pi storage
drwxr-xr-x pi pi IOTstack
drwxr-xr-x pi pi volumes
drwxr-xr-x pi pi nextcloud
drwxrwx--- www-data root html
data - Permission denied