7.controlling Access To Files
7.controlling Access To Files
TO
FILES
• GOAL
• Set Linux file-system permissions on files and to interpret the security effects
of different permission settings.
• OBJECTIVES
• Change the permissions and ownership of files using command-line tools.
• Control the default permissions of new files created by users, explain the
effect of special permissions, and use special permissions and default
permissions to set the group owner of files created in a particular directory.
• SECTIONS
• Managing File System Permissions from the Command Line (and Guided
Exercise)
• Managing Default Permissions and File Access (and Guided Exercise)
• LAB
• Controlling Access to Files
Linux file-system permissions
• File permissions control access to files.
• The Linux file permissions system is simple but flexible, which makes it
easy to understand and apply, yet still able to handle most normal
permission cases easily
• Files have three categories of user to which permissions apply.
• The file is owned by a user, normally the one who created the file.
• The file is also owned by a single group, usually the primary group of the user who
created the file, but this can be changed. Different permissions can be set for the
owning user, the owning group,
• and for all other users on the system that are not the user or a member of the
owning group.
Effects of Permissions on Files and
Directories
• Three categories of permissions apply: read, write, and execute. The
following table explains how these permissions affect access to files
and directories.
Viewing file and directory permissions and
ownership
• The -l option of the ls command shows more detailed information
about file permissions and ownership:
[user@host~]$ ls –l /var/log/lastlog
-rw-rw-r-- 1 root utmp 292292 Jul 24 11:16 /var/log/lastlog
User(owner)
group(owner)
Type Link Count filename
File size
User permission Last Modification
group permission Date and time
Other users permission Type:
- File
d Directory
l Symbolic link
b Block device
c Character device
• The first character of the long listing is the file type. You interpret it
like this:
• - is a regular file.
• d is a directory.
• l is a soft link.
• Other characters represent hardware devices (b and c) or other special-
purpose files (p and s).
• The next nine characters are the file permissions. These are in three
sets of three characters:
• permissions that apply to the user that owns the file,
• the group that owns the file, and
• all other users.
• If the set shows rwx, that category has all three permissions, read,
write, and execute. If a letter has been replaced by -, then that
category does not have that permission.
• You can use the -d option to to show detailed information about a
directory itself, and not its contents.
EXAMPLES OF PERMISSION
EFFECTS
• Given the below four users information and list of files
• Each digit represents permissions for an access level: user, group, other.
• The digit is calculated by adding together numbers for each permission you want to
add, 4 for read, 2 for write, and 1 for execute.
• For example to set the permissions -rwxr-x---
• For the user, rwx is calculated as 4+2+1=7.
• For the group,r-x is calculated as 4+0+1=5,
• and for other users, --- is represented with 0.
• Putting these three together, the numeric representation of those permissions is 750.
• This calculation can also be performed in the opposite direction. Look
at the permissions 640.
• For the user permissions, 6 represents read (4) and write (2), which displays
as rw-
• For the group part, 4 only includes read (4) and displays as r--.
• The 0 for other provides no permissions (---)
• Hence the final set of symbolic permissions for this file is -rw-r-----.
Examples
• Set read and write permissions for user, read permission for group
and other, on samplefile:
• Set read, write, and execute permissions for user, read and execute
permissions for group, and no permission for other on sampledir:
Changing file and directory user or group
ownership
• A newly created file is owned by the user who creates that file. By
default, new files have a group ownership that is the primary group of
the user creating the file.
• Only root can change the user that owns a file. Group ownership,
however, can be set by root or by the file's owner. root can grant file
ownership to any group, but regular users can make a group the
owner of a file only if they are a member of that group.
• File ownership can be changed with the chown (change owner)
command. For example, to grant ownership of the test_file file to the
student user, use the following command:
• chown can be used with the -R option to recursively change the
ownership of an entire directory tree. The following command grants
ownership of test_dir and all files and subdirectories within it to user
student:
• Set the setgid bit and add read/write/execute permissions for user
and group, with no access for others, on directory:
Default file permissions
• When you create a new file or directory, it is assigned initial
permissions. There are two things that affect these initial permissions.
The first is whether you are creating a regular file or a directory. The
second is the current umask.
• If you create a new directory, the operating system starts by assigning
it octal permissions 0777 (drwxrwxrwx).
• If you create a new regular file, the operating system assignes it octal
permissions 0666 (-rw-rw-rw-). You always have to explicitly add
execute permission to a regular file. This
Default Permisions:- umask
• However, the shell session will also set a umask to further restrict the
permissions that are initially set. This is an octal bitmask used to clear
the permissions of new files and directories created by a process. If a bit
is set in the umask, then the corresponding permission is cleared on
new files.
• For example, the umask 0002 clears the write bit for other users. The
leading zeros indicate the special, user, and group permissions are not
cleared. A umask of 0077 clears all the group and other permissions of
newly created files.
• The umask command without arguments will display the current value
of the shell's umask:
umask Example
• The following example explains how the umask affects the
permissions of files and directories.
• By setting the umask value to 0, the file permissions for other change
from read to read and write. The directory permissions for other
changes from read and execute to read, write, and execute.
• To mask all file and directory permissions for other, set the umask
value to 007.
• A umask of 027 ensures that new files have read and write
permissions for user and read permission for group. New directories
have read and write access for group and no permissions for other.
• The default umask for users is set by the shell startup scripts. By
default, if your account's UID is 200 or more and your username and
primary group name are the same, you will be assigned a umask of
002. Otherwise, your umask will be 022.
exercise