How to change mounted drive partition owner
This is a swift tutorial on how to change the mounted partition point owner user, access and permissions.
Leia esse artigo em português aqui.
This tutorial is meant for those who use a shared storage in a remote SSH-enabled Linux machine. It was tested for Ubuntu, but it will be useful on most Linux distros.
By default, when in a desktop with a GUI (graphical user interface), the mounted partition point is locally accessible by all users. However, when using a server-like machine and mounting the storage manually at least once, it will be needed to configure the permissions to other users besides root.
Before executing the commands I suggest in this tutorial, I tried to change the folder’s owner with the command sudo chown -R <user> <folder>
, mode with sudo chmod a+rwx <folder>
. These had no result, so I kept searching and tried to change the folder's attribute with sudo chattr -i <folder>
, which returned the following error:
chattr: Inappropriate ioctl for device while reading flags on <folder>
That said, lets start the tutorial.
Step 1
If mounted, unmount the storage. Type the following command to see a simplified list of disks and partitions:
lsblk -o name,mountpoint,label,size,fstype,uuid | egrep -v "^loop"
It’s return looks like this:
NAME MOUNTPOINT LABEL SIZE FSTYPE UUID
sda 2.7T
├─sda1 /data/part1 part1 1.2T vfat 5633-529C
└─sda2 /data/part2 part2 1.2T vfat 9792-26A7
It is also possible to use the simpler command lsblk -f
which has a similar output.
To unmount the storage using the example, run the command sudo unmount /data/part1
or sudo unmount /dev/sda1
Step 2
Create the mountpoint folder:
example mkdir -p /data/part1
The folder /mnt/sda1 is also very common.
Step 3
Check your userID’s uid
number (usually it's 1000, sometimes 1001 or 1002):
grep ^"$USER" /etc/group
And use that number if you want to grab ownership.
sudo mount -o rw,user,uid=1000,umask=007,exec /dev/sda1 /data/part1
# with execute permissionsudo mount -o rw,user,uid=1000,dmask=007,fmask=117 /dev/sda1 /data/part1
# without execute permissionsudo mount -o rw,users,umask=000,exec /dev/sda1 /data.part1
# with permission to everyone - convenient but unsafe
Note: it is also possible to run a similar command using the user group’s gid
.
Et voilà
$ls -l data/
total 512
drwxrwx--- 4 home root 262144 Dec 31 1969 part1
It’s now remotely accessible with read and write permissions, in this example by the home
user.
Check here how to configure a Remote SSH/SFTP Storage Drive access on Windows 10.
Notes:
- This tutorial is based on this answer from Sudodus.
- It was patiently reviewed by Heitor Brandão at 2020–08–25
Originally published at GitHub.