Concepts of File Permissions
Concepts of File Permissions
LAB # 5
File permissions
Linux also lets files be shared between users and groups of users. If xyz(user) desired,
he could cut off access to his files so that no other user could access them. However,
on most systems the default is to allow other users to read your files but not modify or
delete them in any way.
Every file is owned by a particular user. However, files are also owned by a
particular group, which is a defined group of users of the system. Every user is placed
into at least one group when that user's account is created. However, the system
administrator may grant the user access to more than one group.
Groups are usually defined by the type of users who access the machine. For
example, on a university Linux system users may be placed into the groups student,
staff, faculty or guest.
Permissions fall into three main divisions: read, write, and execute. These permissions
may be granted to three classes of users: the owner of the file, the group to which the
file belongs, and to all users, regardless of group.
1. Read permission lets a user read the contents of the file, or in the case of
directories, list the contents of the directory (using ls).
2. Write permission lets the user write to and modify the file. For directories,
write permission lets the user create new files or delete files within that
directory.
3. Execute permission lets the user run the file as a program or shell script (if the
file is a program or shell script). For directories, having execute permission
lets the user cd into the directory in question.
The first field in the listing represents the file type. The second field is the owner of
the file (danny), the third field is the group to which the file belongs ( users).
Obviously, the last field is the name of the file (story).
This file is owned by danny, and belongs to the group users. The string –rw-r--r--
lists, in order, the permissions granted to the file's owner, the file's group, and
everybody else.
The first character of the permissions string (“-“) represents the type of file. The next
three characters (“rw-“) represent the permissions granted to the file's owner, danny.
The “r” stands for “read” and the “w” stands for “write”. Thus, danny has read and
write permission to the file story.
As mentioned, besides read and write permission, there is also “Execute” permission
represented by an “x”. However, a “-“ is listed here in place of an “x”, so danny
doesn't have execute permission on this file. This is fine, as the file story isn't a
program of any kind. Of course, because danny owns the file, he may grant himself
execute permission for the file if he so desires.
The next three characters, (“r--“), represent the group's permissions on the file. The
group that owns this file is users. Because only an “r” appears here, any user who
belongs to the group users may read this file.
The last three characters, also (“r--“), represent the permissions granted to every other
user on the system (other than the owner of the file and those in the group users).
Again, because only an (“r--“) is present, other users may read the file, but not write
to it or execute it.
Permissions Dependencies
The permissions granted to a file also depend on the permissions of the directory
in which the file is located. For example, even if a file is set to -rwxrwxrwx, other
users cannot access the file unless they have read and execute access to the directory
in which the file is located. For example, if Danny wanted to restrict access to all of
his files, he could set the permissions to his home directory /home/danny to -rwx---.
In this way, no other user has access to his directory, and all files and directories
within it. Danny doesn't need to worry about the individual permissions on each of
his files.
In other words, to access a file at all, you must have execute access to all directories
along the file's pathname, and read (or execute) access to the file itself.
Typically, users on a Linux system are very open with their files. The usual set of
permissions given to files is -rw-r-r-, which lets other users read the file but not
change it in any way. The usual set of permissions given to directories is -rwxr-xr-x,
which lets other users look through your directories, but not create or delete files
within them.
However, many users wish to keep other users out of their files. Setting the
permissions of a file to -rw---- will prevent any other user from accessing the file.
Likewise, setting the permissions of a directory to -rwx--- keeps other users out of
the directory in question.
File Permissions
The file permission system allows you to define the level of access to
individual files and directories for three different types of users.
There are three kinds of access or permissions each different type of user can be
given.
Abb. Permission
r read permission
w write permission
x execute permission
Changing Permissions.
Change the access mode (i.e. file permissions) of one or more files and directories.
Mode can be specified using symbolic or octal format. To execute chmod on a file or
directory you must be its owner or a super user.
u (user)
g (group)
o (other)
a (all) (i.e. ugo)
r (read)
w (write)
x (execute)
nnn
Sets the permissions based on three octal numbers. The first number defines access
for the user, the second for the group and the last for others. The octal number, n, is
a number between 0 and 7. Each type of permission has an octal number associated
with it.
The file permission is determined by adding the octal numbers of each desired
permission. This provides seven unique numbers for every possible combination of
read, write and execute permission. If a 0 is used then no access is granted.
Looking at the numbers for story we see permissions of 751: 7 for owner (hundreds
positions), 5 for group (ten positions), and 1 for others (one position). See the table
below for number positions in detail.
Examples:
Set the permissions on file1 so that the user has both read and write access, the
group has read access and others have no access.
2. chmod go-wr *
Remove read and write permission for the group and others for all files in the
current directory.
For the user and group allow read and write access to the files chap1 and
chap2. The octal number specifying read and write permission, 6, is
determined by adding 4 to give read permission and 2 to give write
permission. Others have no access to the files as specified by the octal number
0.
Set permissions on the directory dir1 so that the user has complete access
(read + write + execute = 2 + 4 + 1 = 7). The group and others have no
access to the directory.
Set permissions on the file file1so that the user and group has complete access
and others have no access to the file
1. Read Permission
Read permission grants privileges to use files and directories. Read permission
allows a file to be viewed, copied and accessed by commands such as cat and
grep.
Read permission can be granted to three different classes of people: the file's user
(or owner), the file's group and others.
Examples:
Changes the permissions on the file afile so that the user has write permission but
not read permission. The user can no longer view or copy the file.
$ cat afile
The user can still rename, overwrite or delete the file. For example,
$ mv afile bfile
$ rm afile
$ cp afile ./labs
Changes the permissions on the directory adir so that the user has write and
execute permission but not read permission. The user can still cd into adir, create
files and subdirectories within adir and delete adir, but he cannot list all files
within adir.
$ cd adir
$ ls
ls: .: Permission denied
$ Cat > file1
$ mkdir sdir
The user can list a file or directory within adir by its exact name, but cannot use
wildcards. For example,
$ ls file1
file1
$ ls file*
ls: No match.
2. Write permission
Write permission grants privileges to use files and directories. Write
permission allows a file to be edited, deleted and overwritten. Many commands
such as cp, rm, mv, and vi will override write permissions when used with the
correct command line option or after querying the user
Write permission is required to create, edit, rename and delete files and
subdirectories within a directory. Note that execute permission is also required for
these tasks.
Write permission can be granted to three different classes of people: the file's user
(or owner), the file's group and others.
Examples:
Changes the permissions on the directory adir so that the user has read and
execute permission but not write permission. The user can still cd into adir and
list files in the directory, but cannot create, rename or delete files or subdirectories
within adir.
$ cd adir
$ ls
file1 file2 sdir
$ rm file1
rm: file1: Permission denied
$ cp file1 newfile
cp: newfile: Permission denied
$ mv file1 newname
mv: rename file1 to newname: Permission denied
$ mkdir sdir2
mkdir: sdir2: Permission denied
3. Execute Permission
Execute permission on a directory is required to cd into the directory, list the contents
of the directory with the ls command as well as create, edit, rename, access and delete
files or subdirectories within the directory. It is also required to execute a program or
shell script within a directory.
Execute permission can be granted separately to three different classes of people: the
file's user (or owner), the file's group and others.
Examples:
Lab Task
Example: chmod u+rwx, g-rwx` ,u-rwx file 1 above example would grant’file 1’
permission for users to read, write and execute the file; for group to read , write and
execute the file andtake permission away from other to read,write and execute.