C++ CSS HTML Java JavaScript MySQL Oracle PERL PHP SQL Unix VBScript XHTML XML Сети
Ownership and Permissions
 

13.14. Ownership and Permissions

Earlier in this chapter, when you tried to change to root's login directory, you received the following message:

cd /root
bash: /root: Permission denied

One way to gain entry when you are denied permission is to su to root, as you learned earlier. This is because whoever knows the root password has complete access.

All files and directories are "owned" by the person who created them. You created the file sneakers.txt (see Section 13.9.1 Using Redirection) in your login directory, so sneakers.txt belongs to you.

That means you can specify who is allowed to read the file, write to the file, or (if it is an application instead of a text file) who can execute the file.

can read, write to, or execute a file.

Take a closer look at sneakers.txt with the ls command using the -l option (see Figure 13-11).

that, by default, the name of your group is the same as your login name.

Figure 13-11. Permissions for sneakers.txt

Other information to the right of the group includes file size, date and time of file creation, and file name.

of users.

For example:

-rw-rw-r--

Those three sets are the owner of the file, the group in which the file belongs, and "others," meaning other users on the system.

-    (rw-)   (rw-)   (r--) 1 sam sam 
|      |       |       | 
type  owner  group   others
      

The first item, which specifies the file type, can show one of the following:

Beyond the first item, in each of the following three sets, you will see one of the following:

When you see a dash in owner, group, or others, it means that particular permission has not been granted. Look again at the first column of sneakers.txt and identify its permissions.

ls -l sneakers.txt
-rw-rw-r--    1 sam sam     150 Mar 19 08:08 sneakers.txt

The file's owner (in this case, sam) has permission to read and write to the file. The group, sam, has permission to read and write to sneakers.txt, as well. It is not a program, so neither the owner or the group has permission to execute it.

13.14.1. The chmod Command

Use the chmod command to change permissions. This example shows how to change the permissions on sneakers.txt with the chmod command.

The original file looks like this, with its initial permissions settings:

-rw-rw-r--    1 sam sam     150 Mar 19 08:08 sneakers.txt

If you are the owner of the file or are logged into the root account you can change any permissions for the owner, group, and others.

Right now, the owner and group can read and write to the file. Anyone outside of the group can only read the file (r--).

CautionCaution
 

altered, or deleted. As a rule, you should only grant read and write permissions to those who truly need them.

of the file permissions.

Take a look at the file first. At the shell prompt, type:

ls -l sneakers.txt

The previous command displays this file information:

-rw-rw-r--    1 sam sam     150 Mar 19 08:08 sneakers.txt

Now, type the following:

chmod o+w sneakers.txt

The o+w command tells the system you want to give others write permission to the file sneakers.txt. To check the results, list the file's details again. Now, the file looks like this:

-rw-rw-rw-    1 sam sam     150 Mar 19 08:08 sneakers.txt

Now, everyone can read and write to the file.

To remove read and write permissions from sneakers.txt use the chmod command to take away both the read and write permissions.

chmod go-rw sneakers.txt

By typing go-rw, you are telling the system to remove read and write permissions for the group and for others from the file sneakers.txt.

The result will look like this:

-rw-------    1 sam sam    150 Mar 19 08:08 sneakers.txt

Think of these settings as a kind of shorthand when you want to change permissions with chmod, because all you really have to do is remember a few symbols and letters with the chmod command.

Here is a list of what the shorthand represents:

Identities

u — the user who owns the file (that is, the owner)

g — the group to which the user belongs

o — others (not the owner or the owner's group)

a — everyone or all (u, g, and o)

Permissions

r — read access

w — write access

x — execute access

Actions

+ — adds the permission

- — removes the permission

= — makes it the only permission

Want to test your permissions skills? Remove all permissions from sneakers.txt — for everyone.

chmod a-rwx sneakers.txt

Now, see if you can read the file with the command cat sneakers.txt, which should return the following:

cat: sneakers.txt: Permission denied

chmod u+rw sneakers.txt

Use the command cat sneakers.txt to verify that you, the file owner, can read the file again.

Here are some common examples of settings that can be used with chmod:

By adding the -R option, you can change permissions for entire directory trees.

permission to search through that directory.

If you do not allow others to have execute permission to tigger, it will not matter who has read or write access. No one will be able to get into the directory unless they know the exact file name.

For example, type:

chmod a-x tigger

to remove everyone's execute permissions.

Here is what happens now when you try to cd to into tigger:

bash: tigger: Permission denied

Next, restore your own and your group's access:

chmod ug+x tigger

Now, if you check your work with ls -l you will see that only others will be denied access to the tigger directory.

13.14.2. Changing Permissions With Numbers

Remember the reference to the shorthand method of chmod? Here is another way to change permissions, although it may seem a little complex at first.

Go back to the original permissions for sneakers.txt:

-rw-rw-r--    1 sam sam     150 Mar 19 08:08 sneakers.txt

Each permission setting can be represented by a numerical value:

= 6.

For sneakers.txt, here are the numerical permissions settings:

 -  (rw-)   (rw-)  (r--)
      |       |      |
    4+2+0   4+2+0  4+0+0

The total for the user is six, the total for the group is six, and the total for others is four. The permissions setting is read as 664.

If you want to change sneakers.txt so those in your group will not have write access, but can still read the file, remove the access by subtracting two (2) from that set of numbers.

The numerical values, then, would become six, four, and four (644).

To implement these new settings, type:

chmod 644 sneakers.txt

Now verify the changes by listing the file. Type:

ls -l sneakers.txt

The output should be:

-rw-r--r--    1 sam sam     150 Mar 19 08:08 sneakers.txt

Now, neither the group nor others have write permission to sneakers.txt. To return the group's write access for the file, add the value of w (2) to the second set of permissions.

chmod 664 sneakers.txt

WarningWarning
 

could allow tampering with sensitive files, so in general, it is not a good idea to use these settings.

Here is a list of some common settings, numerical values and their meanings:

Here are some common settings for directories:

Главная