4

I have made 3 users with adduser.
How can I give each user different permissions to one file (example.txt) without setfacl?

Example:

  • First user to have only read permissions to the file
  • Second to have read and write permissions
  • Third to have all permissions for the file.
Zanna
  • 70,465

1 Answers1

4

The permissions tag wiki is a nice short reference on classic Linux file permissions.

As a brief demonstration of the simplest usage of these permissions on files only, let's create a file, which I am going to call shiny

touch shiny

let's take a look at the permissions of the file we just created

$ ls -l shiny
-rw-rw-r-- 1 zanna zanna 0 Feb 26 21:54 shiny

The string at the start shows the permissions in sets of three. After the permissions string, there's a number that isn't interesting to us, then the word zanna repeated twice. zanna is my username. Since I created the file, I am the owner, and it belongs to my group too.

enter image description here

r = read w = write, x = execute - = no permission

Looking at shiny now we can see it is a regular file (- at the start) and the owner, zanna, may read and write to the file. Members of the group zanna may also read and write to the file, and any other user may read, but not write. No user has execute permission for this file.

The stat command gives much more detail about file metadata, including permissions, and we can format its output in various ways. For example, my favourite way:

$ stat -c "%n %a" shiny
shiny 664

This shows us that the file has octal permissions 664 - read-write for owner and group, and read-only for others.

On my system I have created three user accounts, zanna, pixie and mermaid

Let's change the permissions of shiny to grant different levels of access to each user.

First, we'll create a new group (we could just add a user to the zanna group, but I would rather not do that, as that would give them access to lots of my personal files)

sudo addgroup unicorns

And now we'll use the unicorns group to control access, by making this group own shiny:

chown :unicorns shiny

Please do not forget the colon : in this command, as this allows us to change only the group, and always type carefully when using chown (change owner) or chmod (change mode), especially with sudo.

Now make one user a member of this group:

sudo adduser pixie unicorns

Now pixie is a unicorn a member of the unicorns group.

Now we just need to change the file mode. There are two ways to do this, the octal way, which I prefer:

chmod 764 shiny

and the symbolic way:

chmod u+x shiny

I trust you to read the wiki to learn more about these. After running one of these commands, you can check again with stat or ls -l.

$ ls -l shiny
-rwxrw-r-- 1 zanna unicorns 0 Feb 26 21:54 shiny

Now:

  • zanna has read, write and execute permission because she is the owner
  • pixie has read and write permission because she is in the group unicorns
  • mermaid has read permission because shiny is readable to all.
Zanna
  • 70,465