|
|
Understanding UNIX permissions and chmodIntroductionThis is a topic that has been beaten to death both in books and on-line. For some reason, it seems that it is one of the most common misunderstandings that people have to face when learning how to write and/or configure their first cgi programs. This tutorial aims to clarify the concepts involved. Note that we will be referring to UNIX in a generic sense in this article. Most of what we are going to discuss here applies to all UNIX flavours. (such as Linux, SVR4, BSD etc.) It is also a good idea to type man chmod to check for the specific details on your system, too.UsersA UNIX system serves many users. Users are an abstraction that denotes a logical entity for assignment of ownership and operation privileges over the system. A user may correspond to a real-world person, but also a type of system operation. So, in my system, I have user 'nick' that corresponds to me, but I also have user 'www' which corresponds to the privileges necessary to operate the local webserver. UNIX doesn't care about what the user means for me. It just knows what belongs to any given user and what each user is allowed to do with any given thing (file, program, device, etc) on the system. UNIX identifies each user by a User ID (UID) and the username (or login) such as 'nick' and 'www' is just an alias to the UID that makes humans more comfortable.GroupsUsers can be organized in groups. A user may belong to one or more groups of users. The concept of groups serves the purpose of assigning sets of privileges for a given resource and sharing them among many users that need to have them. (perhaps because they are all members of a project working team and they all need access to some common project files) So, on my system user 'nick' and user 'www' both belong to the group 'perlfect'. This way, they can have some shared privileges over the files for this site. User 'nick' needs them to edit the site, and user 'www' needs them to manage the webserver that will be publishing the site.OwnershipEvery file in UNIX has an owner user and an owner group. So, for any file in the system, user 'nick' may have one of the following ownership relations:
PermissionsEvery file on the system has associated with it a set of permissions. Permissions tell UNIX what can be done with that file and by whom. There are three things you can (or can't) do with a given file:
[nick@thekla src]$ ls -l
-rwxr-xr-x 1 nick users 382 Jan 19 11:49 bscoped.pl
drwxr-xr-x 3 nick users 1024 Jan 19 11:19 lib/
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl
The first column here shows the permission bit pattern for each file. The third column shows the owner,
and the fourth column shows the owner group. By the time, the information provided by ls -l
should be enough for you to figure out what each user of the system can do with any of the files in the
directory.
DirectoriesAnother interesting thing to note is that lib/ which is a directory has permissions, too. Permissions take a different meaning for directories. Here's what they mean:
chmodTo set/modify a file's permissions you need to use the chmod program. Of course, only the owner of a file may use chmod to alter a file's permissions. chmod has the following syntax: chmod [options] mode file(s) The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A mode specifies which user's permissions should be changed, and afterwards which access types should be changed. Let's say for example:chmod a-x socktest.pl
This means that the execute bit should be cleared (-) for all users. (owner, group and the rest
of the world) The permissions start with a letter specifying what users should be affected by the change, this
might be any of the following:
$ ls -l socktest.pl
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod a-x socktest.pl
$ ls -l socktest.pl
-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod g+w socktest.pl
$ ls -l socktest.pl
-rw-rw-r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod ug+x socktest.pl
$ ls -l socktest.pl
-rwxrwxr-- 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod ug-wx socktest.pl
$ ls -l socktest.pl
-r--r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
Strange numbers...You might have encountered things like chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode. Every one of the three digits on the mode number corresponds to one of the three permission triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 4 for r, 2 for w, 1 for x. If the permission bit you add this value to the number of the permission triplet. If it is cleared, then you add nothing. (Some of you might notice that in fact, the number for a triplet is the octal value corresponding to the three-bit pattern - if you don't know what an octal value is, it doesn't really matter, just follow the intstructions) So if a file has rwxr-xr-x permissions we do the following calculation: Triplet for u: rwx => 4 + 2 + 1 = 7Triplet for g: r-x => 4 + 0 + 1 = 5 Tripler for o: r-x => 4 + 0 + 1 = 5 Which makes : 755 So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means 'everyone has full access to this file' Further reading...
CommentsWarning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'mysql303.secureserver.net' (4) in /home/content/42/6167842/html/comments/comments_include.php on line 6 Connection Error: Can't connect to MySQL server on 'mysql303.secureserver.net' (4) |
Like it? Share it!
|
|||||||||||||||
| Copyright © Perlfect Solutions 1998-2008 - Privacy Policy |