Linux read and execute permission

Unix / Linux — File Permission / Access Modes

In this chapter, we will discuss in detail about file permission and access modes in Unix. File ownership is an important component of Unix that provides a secure method for storing files. Every file in Unix has the following attributes −

Owner permissions − The owner’s permissions determine what actions the owner of the file can perform on the file.

Group permissions − The group’s permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.

Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.

The Permission Indicators

While using ls -l command, it displays various information related to file permission as follows −

Here, the first column represents different access modes, i.e., the permission associated with a file or a directory.

The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x) −

The first three characters (2-4) represent the permissions for the file’s owner. For example, -rwxr-xr— represents that the owner has read (r), write (w) and execute (x) permission.

The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr— represents that the group has read (r) and execute (x) permission, but no write permission.

The last group of three characters (8-10) represents the permissions for everyone else. For example, -rwxr-xr— represents that there is read (r) only permission.

File Access Modes

The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which have been described below −

Grants the capability to read, i.e., view the contents of the file.

Write

Grants the capability to modify, or remove the content of the file.

Execute

User with execute permissions can run a file as a program.

Directory Access Modes

Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned −

Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.

Write

Access means that the user can add or delete files from the directory.

Execute

Executing a directory doesn’t really make sense, so think of this as a traverse permission.

A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions

To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod — the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

Adds the designated permission(s) to a file or directory.

Removes the designated permission(s) from a file or directory.

Sets the designated permission(s).

Here’s an example using testfile. Running ls -1 on the testfile shows that the file’s permissions are as follows −

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes −

Here’s how you can combine these commands on a single line −

Using chmod with Absolute Permissions

The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Sr.No. Chmod operator & Description
1
Number Octal Permission Representation Ref
0 No permission
1 Execute permission —x
2 Write permission -w-
3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx
4 Read permission r—
5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx

Here’s an example using the testfile. Running ls -1 on the testfile shows that the file’s permissions are as follows −

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes −

Changing Owners and Groups

While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions mentioned above are also assigned based on the Owner and the Groups.

Two commands are available to change the owner and the group of files −

chown − The chown command stands for «change owner» and is used to change the owner of a file.

chgrp − The chgrp command stands for «change group» and is used to change the group of a file.

Changing Ownership

The chown command changes the ownership of a file. The basic syntax is as follows −

The value of the user can be either the name of a user on the system or the user id (uid) of a user on the system.

The following example will help you understand the concept −

Changes the owner of the given file to the user amrood.

NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own.

Changing Group Ownership

The chgrp command changes the group ownership of a file. The basic syntax is as follows −

The value of group can be the name of a group on the system or the group ID (GID) of a group on the system.

Following example helps you understand the concept −

Changes the group of the given file to special group.

SUID and SGID File Permission

Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.

As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow.

Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program’s owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

The SUID and SGID bits will appear as the letter «s» if the permission is available. The SUID «s» bit will be located in the permission bits where the owners’ execute permission normally resides.

For example, the command −

Shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.

If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users −

  • The owner of the sticky directory
  • The owner of the file being removed
  • The super user, root

To set the SUID and SGID bits for any directory try the following command −

Источник

Manage file permissions on Unix-like systems

Overview

Unix-like operating systems, such as Linux, running on shared high-performance computers use settings called permissions to determine who can access and modify the files and directories stored in their file systems. Each file and directory in a file system is assigned «owner» and «group» attributes.

Most commonly, by default, the user who creates a file or directory is set as owner of that file or directory. When needed (for example, when a member of your research team leaves), the system’s root administrator can change the user attribute for files and directories.

The group designation can be used to grant teammates and/or collaborators shared access to an owner’s files and directories, and provides a convenient way to grant access to multiple users.

View file permissions

To view the permissions for all files in a directory, use the ls command with the -la options. Add other options as desired; for help, see List the files in a directory in Unix.

For example, if you enter:

You should see output similar to the following:

In the output example above, the first character in each line indicates whether the listed object is a file or a directory. Directories are indicated by a ( d ); the absence of a d at the beginning of the first line indicates that myfile.txt is a regular file.

The letters rwx represent different permission levels:

Permission Files Directories
r can read the file can ls the directory
w can write the file
x can execute the file can cd to the directory

Note the multiple instances of r , w , and x . These are grouped into three sets that represent different levels of ownership:

    Owner or user permissions: After the directory ( d ) slot, the first set of three characters indicate permission settings for the owner (also known as the user).

In the example -rw-r—r— , the owner permissions are rw- , indicating that the owner can read and write to the file but can’t execute it as a program.

In the example drwxr-xr-x , the owner permissions are rwx , indicating that the owner can view, modify, and enter the directory.

Group permissions: The second rwx set indicates the group permissions. In the fourth column of the example above, group1 is the group name.

In the example -rw-r—r— , group members can only read the file.

In the example drwxr-xr-x , group members can view as well as enter the directory.

  • Other permissions: The final rwx set is for «other» (sometimes referred to as «world»). This is anyone outside the group. In both examples above, these are set to the same permissions as the group.
  • Change file permissions

    To change file and directory permissions, use the command chmod (change mode). The owner of a file can change the permissions for user ( u ), group ( g ), or others ( o ) by adding ( + ) or subtracting ( — ) the read, write, and execute permissions.

    There are two basic ways of using chmod to change file permissions: The symbolic method and the absolute form.

    Symbolic method

    The first and probably easiest way is the relative (or symbolic) method, which lets you specify permissions with single letter abbreviations. A chmod command using this method consists of at least three parts from the following lists:

    Access class Operator Access Type
    u (user) + (add access) r (read)
    g (group) — (remove access) w (write)
    o (other) = (set exact access) x (execute)
    a (all: u, g, and o)

    For example, to add permission for everyone to read a file in the current directory named myfile , at the Unix prompt, enter:

    The a stands for «all», the + for «add», and the r for «read».

    If you omit the access class, it’s assumed to be all, so you could also enter the previous example as:

    You can also specify multiple classes and types with a single command. For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) on a file named myfile , you would enter:

    You can also specify that different permissions be added and removed in the same command. For example, to remove write permission and add execute for all users on myfile , you would enter:

    In each of these examples, the access types that aren’t specified are unchanged. The previous command, for example, doesn’t change any existing settings specifying whether users besides yourself may have read ( r ) access to myfile . You could also use the exact form to explicitly state that group and other users’ access is set only to read with the = operator:

    The chmod command also operates on directories. For example, to remove write permission for other users on a subdirectory named mydir , you would enter:

    To do the same for the current directory, you would enter:

    To change permissions recursively in all subdirectories below the specified directory, add the -R option; for example, to grant execution permissions for other users to a directory ( mydir ) and all the subdirectories it contains, you would enter:

    Be careful when setting the permissions of directories, particularly your home directory; you don’t want to lock yourself out by removing your own access. Also, you must have execute permission on a directory to switch ( cd ) to it.

    Absolute form

    The other way to use the chmod command is the absolute form, in which you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file’s permissions.

    The three numbers are specified in the order: user (or owner), group, and other. Each number is the sum of values that specify read, write, and execute access:

    Permission Number
    Read (r) 4
    Write (w) 2
    Execute (x) 1

    Add the numbers of the permissions you want to give; for example:

    • For file myfile , to grant read, write, and execute permissions to yourself (4+2+1=7), read and execute permissions to users in your group (4+0+1=5), and only execute permission to others (0+0+1=1), you would use:
    • To grant read, write, and execute permissions on the current directory to yourself only, you would use:

    You can think of the three digit sequence as the sum of attributes you select from the following table:

    Read by owner 400
    Write by owner 200
    Execute by owner 100
    Read by group 040
    Write by group 020
    Execute by group 010
    Read by others 004
    Write by others 002
    Execute by others 001

    Sum all the accesses you wish to permit. For example, to give write and execute privileges to the owner of myfile (200+100=300), and give read privileges to all (400+040+004=444), you would enter:

    Some other examples are:

    777 anyone can do anything (read, write, or execute)
    755 you can do anything; others can only read and execute
    711 you can do anything; others can only execute
    644 you can read and write; others can only read

    Common issues when sharing data with other users

    To share a file or directory that you own with someone, you can grant read and execute privileges for that user. However, you must also set the same privileges on any parent directories above the item you’re sharing; if you don’t, the user can’t look and change into ( cd ) all the parent directories above your file or directory.

    If you think of a file system as a physical place, then permissions work like keys that let you access different directories:

    • The read ( r ) permission lets users look ( ls ) into directories.
    • The execute ( x ) permission lets users move ( cd ) into directories.
    • The write ( w ) permission lets users add and remove files.

    For example, say you want to give someone access to /N/u/username/Carbonate/scripts . Imagine the path as a physical space:

    • /N is the gated community where you live.
    • /u is the unit.
    • /username is your apartment.
    • /Carbonate is a room in your apartment.
    • /scripts is a closet in your room.

    If someone wanted to run your scripts, you would need to give that person access to every part of /N/u/username/Carbonate/scripts . You might try to do it this way:

    However, a user can’t read or access a subdirectory unless the user also has x permissions to the parent directories. In other words, the above command gives out a key to your closet, but not to your room or apartment.

    To resolve this, give x permissions to the parent directories you control:

    This will let others move ( cd ) to the scripts directory. Because the parent directories don’t have r permissions, users will only be able to look ( ls ) within the scripts directory, keeping the rest of your file system private.

    Get help

    For more about chmod , consult the manual page. At the Unix prompt, enter:

    At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

    Источник

    Читайте также:  Отображение fps windows 10
    Оцените статью