Changing File Permissions
Previous  Top  Next

There are a couple of utilities that can be used to change file permissions, but let's start with viewing the file permissions. You can see file permissions when you type ls –l (in RedHat you can just type 'll'). Let's take at look at this sample output:

drwx------
    2 Wizard   Wizard       4096 Nov 14 11:49 Wizard
d--x--x--x
    2 root     root         4096 Oct 30 07:30 bin
d--x--x--x
    3 root     root         4096 Nov 30 22:42 etc
-rw-r--r--
    1 root     root            0 Dec  1 16:52 home.txt

We have basically 7 columns here. The ones relevant for permissions are column 1,3 and 4 (conveniently marked in green ;-) )


Column 1
Effective permissions

Column 3
File owner

Column 4
File group

So let's look at the fourth line, file home.txt. The permissions for this file are
rw-r—-r--. Never mind the first letter, it only indicates what type of object it is (d = directory, - = file and so on). Let's break up the permissions into three blocks:
-rw-r--r--    1 root     root            0 Dec  1 16:52 home.txt

Owner
Group
Everyone
root
root

rw-
r--
r--

Hmm, I already revealed it. The first part are the permissions for the owner, root in this case (column 3), root has the right to read and write to the file. The group assigned to this file is also root, but only has read permissions to the file. Everybody else on the system also has read permissions on the file – obviously you should be careful with this permission. Now how do we change those permissions in traditional Unix style? Let's look at this table:

Letter
Permission
Value
r
read
4
w
write
2
x
execute
1

Highly interesting, isn't it? If you assign permissions, you simply add the values of the rights you want to assign to the particular user or group. In the above example, the owner has the right 6, since r = 4 and w = 2 and the sum is 6. Group and everyone both have ... 4 !!! Wow, not as hard as it looks like. So how do we change them? We use the program chmod (change mode). To change the permissions of the file home.txt to "
rw- --- ---" to allow only the root user to access the file, we type

         chmod 600 home.txt

I think that's enough explanation for changing the permissions. To give everybody all permissions, we would therefore type

         chmod 777 home.txt

We might want to change the owner of a file, which only root or the current owner can do. You use chown (change owner)

         chown Wizard home.txt

to change the owner to the user Wizard. If you would change permissions of a directory and want every file and subdirectory to be affected of that change, you would type

         chown -R Wizard etc

This would change the owner to Wizard in every file and subdirectory of etc. Good, one more thing to learn, that is how to change the group. It works exactly like chown, except that you use the command chgrp (change group – did you guess it?). One example

         chgrp Wizards home.txt

Here you assign the group Wizards to the file home.txt. And that's it about permissions.