Understanding file permissions - Part 2

  • November 12, 2006
  • Avatar for peter
    Peter
    Upfold

Note: This is a straight up rip of the tutorial of the same name from the Beginner’s Linux series on my personal blog (on my personal site), but it’s content is eminently suitable for FOSSwire readers, so I’ve re-posted it here. Enjoy!

Beginner's Linux

Hello again! This is Part 2 of my tutorial on understanding file permissions on Linux. If you haven't already and don't have much of an understanding of Linux file permissions, try reading Part 1. If you've got an understanding, but don't know how to change them, you could start here!


Changing file permissions - the graphical way

If you're not too proficient when faced with a black screen and a blinking cursor, all good graphical file manager tools can help you change the permissions. It does help to understand how they work and if you've read Part 1, you should know enough to get it.

Here, I'll focus on Konqueror (KDE's default) and Nautilus (Gnome's default) but the same kind of instructions should be relevant to almost any graphical file management tool.

Konqueror

Konqueror windowHosted on Zooomr

Changing permissions is simply a case of right-clicking on a file and choosing Properties, then going to the Permissions tab. On this tab, use the combo boxes to set the permissions for user, group and others (also tick to set it executable).

Konqueror context menuHosted on Zooomr

Konqueror PermissionsHosted on Zooomr

You could also click Advanced Permissions for a tickbox system which resembles the rwxrwxrwx structure we saw in Part 1 (ignore the 'Special' for now).

Konqueror Advanced PermissionsHosted on Zooomr

Nautilus

Nautilus WindowHosted on Zooomr

In Nautilus, it's virtually the same process. First, right-click on your file/folder.

Nautilus context menu Hosted on Zooomr

Choose Properties and go to the Permissions tab.

Nautilus PermissionsHosted on Zooomr

Here it's the same deal, simply tick as necessary to set permissions. Remember it's also in the rwxrwxrwx format we explored in Part 1.

There - simple!

Changing permissions - the geeky, I mean, command line way

Of course if you're willing to dip your toes in the command line, I'll show you that as well. If you really don't want to touch the terminal, feel free to stop here. In the command line, the command you use to change permissions is chmod. There are two ways in which you can set the permissions. One of them uses a similar format to rwxrwxrwx that we learnt about earlier, and the other uses numbers. First of all, we'll look at the symbolic version.

Basically, this involves the use of a letters and symbols.

The first set of symbols consist of u (the user who owns the file) , g (the group), o (other users) and a (all three). These symbols are used for identifying who you want to give permissions. You should have seen these before.

The second set are r (read), w (write) and x (execute). Straightforward hopefully.

Finally, you use + to add permissions, - to remove them and = to set them (we'll talk about the difference later).

So, for example, if I want to give a group of users permission to write to myfile, I first need to check the permissions to see what they are at the moment.


$ ls -l myfile

This will show me the permissions in rwxrwxrwx format on the left hand side.


-rw-r--r-- 1 peter peter 0 Sep 17 12:13 myfile

So, if you remember, this set of permissions means that it's a file, the owner can read and write, the group can read and everyone else can read. OK, so what do we need to do to make the group have write permissions? We need to add (+) write permissions (w) to the group (g). So if we make that into a symbol sum, it would be g+w (add write to group). So that's exactly what we do.


$ chmod g+w myfile

Now we check and see if it's worked:


$ ls -l
-rw-rw-r-- 1 peter peter 0 Sep 17 12:13 myfile

Yes! As you can now see, the group permissions are now rw-, which is read and write. To do it the other way, it's simply g-w:


$ chmod g-w myfile

So it's simply a case now of substituting the relevant symbols for what you want to. For instance, if I wanted to give all other users read access, the command would be:


$ chmod o+r myfile

And I can string multiple additions or subtractions or both together like so:


$ chmod g+r+w myfile

Try changing permissions on a few trivial files in your home folder (remember you can always use the graphical tools if you mess something up).

Now there's also =. We can use this if we want to set multiple permissions at a time and/or we want to overwrite the current settings. Now at the moment there won't appear to be much difference in doing this and using + and -.

Here's a practical example, in code:


$ ls -l myfile
-rw-r-xr-- 1 peter peter 0 Sep 17 12:13 myfile
$ chmod g=rw myfile
$ ls -l myfile
-rw-rw-r-- 1 peter peter 0 Sep 17 12:13 myfile

Did you notice that the x for group had gone after the chmod command? We didn't explicitly remove it, but by using =, we removed the old permissions (r-x) and then set the new ones (rw-). If you still don't get it, look at it the other way as well.


$ ls -l myfile
-rw-r-xr-- 1 peter peter 0 Sep 17 12:13 myfile
$ chmod g+r+w myfile
$ ls -l myfile
-rw-rwxr-- 1 peter peter 0 Sep 17 12:13 myfile

This demonstrates that since we're adding (+) and not resetting (=), the x stays intact.

Hopefully now you see the difference. + and - add or take away permissions, and = resets them all before setting the new ones.

A quick speed tip - you can string commands together if you want to do a lot of work. This time, myfile isn't readable by anyone at all (the owner still has the ability to change permissions remember). We want to allow read and write access to the owner and read access to the group. We can string the commands together with , (a comma).


$ ls -l myfile
---------- 1 peter peter 0 Sep 17 12:13 myfile
$ chmod u+r+w,g+r myfile
$ ls -l myfile
-rw-r----- 1 peter peter 0 Sep 17 12:13 myfile

Right OK. So if you're very new to this kind of thing you might want to take a quick break now to get this into your head. Perhaps create some dummy files and practise changing permissions on them. Any questions about my explanation or why something isn't working? Drop a comment at the end of this post and I'll help out!

There's only one more thing I'm going to delve into for this part, and that's recursive chmodding.

This time I've got myfolder, which contains six files. Here they are (this is the result of an ls -l from within myfolder.


total 24
-rwx------ 1 peter peter 0 Sep 18 14:25 file1
-rwx------ 1 peter peter 0 Sep 18 14:25 file2
-rwx------ 1 peter peter 0 Sep 18 14:25 file3
-rwx------ 1 peter peter 0 Sep 18 14:25 file4
-rwx------ 1 peter peter 0 Sep 18 14:25 file5
-rwx------ 1 peter peter 0 Sep 18 14:25 file6

I want to change the permissions of all the files in this folder so that they are rwxrw-r--. Instead of doing each one manually, I can use chmod's -R (recursive) feature to apply the permissions to the whole folder. From within myfolder, I run:


$ chmod -R u=rwx,g=rw,o=r .

The -R tells chmod to apply this to the whole folder, then I have my permissions I'm setting, then I have a dot (.). The dot tells chmod to affect this folder - as dot means 'the current folder'. Lo and behold:


total 24
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file1
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file2
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file3
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file4
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file5
-rwxrw-r-- 1 peter peter 0 Sep 18 14:25 file6

It worked!

Well, as it turns out, writing this part of the tutorial has taken longer (and ended up longer) than I thought it was going to. So, in Part 3, I'm going to explain the other command line way of chmodding, using the number system.

When Part 3 debuts, this text will contain a link to it. Until then, you can also brush up on Part 1.

When you're ready, move on to Part 3 [Coming soon to FOSSwire], or revisit Part 1.

And as with all my tutorials, if you’ve got any suggestions, had a few problems or you genuinely found this useful, I’d love it if you’d drop a comment on this post, it makes writing these so worthwhile!

If you liked this tutorial, look out for more coming on FOSSwire. So why not subscribe via email, a feed reader of your choice, or via IM now to get the latest tutorials delivered to you!

Avatar for peter Peter Upfold

Home » Articles »