How to create a ext4 partition for all users?

30,055

Solution 1

I finally solved the problem. I found out that I had a typo in my UUID. So my final entry in fstab was:

UUID=... /media/data ext4 defaults 0 0

or even better:

/dev/sdb1 /media/data ext4 defaults 0 0

I checked it before rebooting by simply calling:

sudo umount /media/data
sudo mount -a

If the fstab entry is correct everything will mount ok and every user of the computer will have access to the partition (At least to the folders he created himself which is the correct behaviour).

One cat do

sudo chown -R UserNameOfSudo:users /media/data 
sudo chmod -R g+rw /media/data 

while the partition is still mounted to ensure everybody can access the partition.

If you do the ´chown´ ensure the specific user is part of the group "users" by typing:

sudo adduser yourSpecificUsername users

and logout and login again!

Edit
The partition after mounting by default is owned by root. This seems to prevent anyone from writing to the partition. Changing the owner to "UserNameOfSudo" after mounting as shown above yields the desired behaviour.

Edit2
In the case of sharing the computer between local users and LDAP users a solution is to give all rights to everyone:

sudo chmod 777 /media/data

and then to set a 'sticky bit', which means only the user that created a folder / file is allowed to delete it. Which adds some sensible security:

sudo chmod o+t /media/data/

Solution 2

Maybe you're doing the things in the wrong order. When you create a file system with mkfs.ext4, everything inside it is owned by user root and group root with your system default permissions set.

When you mount that file system on a directory, you see file system permissions and owner, regardless of the original owner and permissions on that directory.

So doing something like this won't work:

sudo mkfs.ext4 /dev/some/data
sudo mkdir /media/data
sudo chown -R :users /media/data
sudo chmod -R g+rw /media/data
sudo mount /dev/some/data /media/data

The right thing to do is create the file system, mount it, and then change permissions and ownership on it. It doesn't matter what you do in /etc/fstab.

The right way to do it is this:

sudo mkfs.ext4 /dev/some/data
sudo mkdir /media/data
sudo mount /dev/some/data /media/data
sudo chown -R :users /media/data
sudo chmod -R g+rw /media/data

This should answer your question. If you need more details, read on.


To better understand what happens, let's experiment a little with an image file

Create an empty file to format and mount using fallocate -l 100MB /tmp/filesystem.img. Then format it as an ext4 file system with sudo mkfs.ext4 /tmp/filesystem.img (it's not a block device, but if you answer yes you can put a working ext4 file system on it anyway) and create a directory to use as mount point mkdir /tmp/experiment.

Now try to change the owner and permissions on that directoy with sudo chown -R :users /tmp/experiment and sudo chmod -R g+rw /tmp/experiment, and check permissions with ls -la /tmp/experiment. You'll get something like this:

 ls -la /tmp/experiment/
 total 0
 drwxrwx--x 2 gerlos users  40 feb 19 10:37 .
 drwxrwxrwt 8 root   root  180 feb 19 10:38 ..

This tells you that /tmp/experiment is owned by user gerlos and group users, and group members can read, write and execute on it. You can put files in it, for example with touch /tmp/experiment/somefile.

Now mount the file system on that directory with sudo mount /mnt/filesystem.img /tmp/experiment, and look again at ls output:

$ ls -la /tmp/experiment/
total 13
drwxr-xr-x 3 root root  1024 feb 19 10:41 .
drwxrwxrwt 8 root root   180 feb 19 10:41 ..
drwx------ 2 root root 12288 feb 19 10:41 lost+found

As you can see, now /tmp/experiment seems owned by root, with different permissions! Why? Because we are not looking at /tmp/experiment itself, but at the root directory of the file system contained in /mnt/filesystem.img, mounted on /mnt/experiment.

Additionally, your normal user won't be able to put files there with touch /tmp/experiment/anotherfile.

If you now try again to run chown and chmod as above, you will change owner and permissions not on the mount point, but on the mounted file system, and your users will be able to use the file system. To confirm this look at ls output one last time:

$ ls -la /tmp/experiment/
total 13
drwxrwxr-x 3 root users  1024 feb 19 10:41 .
drwxrwxrwt 8 root root    180 feb 19 10:45 ..
drwxrw---- 2 root users 12288 feb 19 10:41 lost+found

As you can see, now members of users group can put files on the file system! In fact, nothing prevents your normal user from creating a new file there with touch /tmp/experiment/myfile:

$ ls -la /tmp/experiment/
total 13
drwxrwxr-x 3 root   users   1024 feb 19 11:05 .
drwxrwxrwt 8 root   root     180 feb 19 11:02 ..
drwxrw---- 2 root   users  12288 feb 19 10:41 lost+found
-rw-rw---- 1 gerlos gerlos     0 feb 19 11:02 myfile

Mission accomplished! :-)

Share:
30,055

Related videos on Youtube

mcExchange
Author by

mcExchange

Updated on September 18, 2022

Comments

  • mcExchange
    mcExchange over 1 year

    I know it sound like a dublicate questions but I tried the following and it didn't help:

    My drive is mounted under

    /media/data
    

    so I entered

    sudo chown -R :users /media/data
    

    However I still cannot write to that partition as a user. I also tried to make an entry in the /etc/fstab

    UUID=... /media/data ext4 rw,suid,dev,exec,auto,user,async 0 0
    

    but after that my computer could not boot anymore because he could not find the disk.

    I have Ubuntu 14.04 Server installed. The partition is listed under /dev/nvmeOn1p1 and does not appear in the /etc/fstab (see pics below).

    (the device is a "Intel DC P3700" 800GB SSD)

    Additional Remark: Since the partition is empty i could also format it and recreate it using gparted for example. Is there a way in gparted or other gui programs to specify that the partition should be usable by all users?

    • mcExchange
      mcExchange about 9 years
      sorry, I did specify the filesystem ext4. I will edit that
    • muru
      muru about 9 years
      Before you add pics, please: NO screenshots of text. Copy-paste the text, instead.
    • Jos
      Jos about 9 years
      When you mount manually, is there anything useful in dmesg?
    • mcExchange
      mcExchange about 9 years
      how should i use dmesg here?
    • mcExchange
      mcExchange about 9 years
      These are the last 3 lines of dmesg:32739.410465] EXT4-fs (nvme0n1p1): mounted filesystem with ordered data mode. Opts: (null) [33403.695995] systemd-hostnamed[11061]: Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname! [33449.756403] EXT4-fs (nvme0n1p1): mounted filesystem with ordered data mode. Opts: (null)
    • Takkat
      Takkat about 9 years
      You may want to become familiar with Access Control Lists (ACL): askubuntu.com/questions/52584/shared-folders-for-all-users
    • gerlos
      gerlos about 9 years
      A clarification about language. In GNU/Linux you have disks that contain partitions, that may contain volumes (volumes may span different partitions, for example in LVM) that may contain file systems, that contain files and directories with ownership and permissions metadata (there are als ACLs, but let's keep it simple). If something doesn't work as you expect, go up that chain, check the easy things first, like permissions and ownership ("can I write that file?"), and then up, for example to file systems ("is it mounted read-only?").
  • mcExchange
    mcExchange about 9 years
    As far as I know the "defaults" were the options where only root has access to the partition and not all users, isn't it?
  • Petro Pikh
    Petro Pikh about 9 years
    No, defaults - I think access for all. Now I use in my PC Ubuntu 14.04 similar config in fstab for mount external drive and in me all worked fine. All users in PC can have access to drive.
  • gerlos
    gerlos about 9 years
    Acter you create a file system on your device, everything on it will be owned by root, no matter how you mount it. To make it usable you need to mount it and change ownership and permissions to let your normal user read/write on it - see my answer above.
  • mcExchange
    mcExchange about 9 years
    Thanks for the evolved answer. Step 3 in your "right way to do it" is redundant. It seems to work now, thanks :). But is this solution persistent or do I have to do it every time I reboot?
  • gerlos
    gerlos about 9 years
    You're right, there was an error! mkdir was needed BEFORE mount. Now I corrected it, thanks. Actually, also chmod command could be redundant, but it depends on your system umask settings (if not needed it doesn't harm). Answering to your last question: changes in permissions and ownership are done in the file system, and are permanent just like your files. To always mount on boot the file system stored in /dev/some/data you just need to add a line like /dev/some/data /media/data ext4 defaults 0 1 in your /etc/fstab.