- Unix / Linux — File Permission / Access Modes
- The Permission Indicators
- File Access Modes
- Write
- Execute
- Directory Access Modes
- Write
- Execute
- Changing Permissions
- Using chmod in Symbolic Mode
- Using chmod with Absolute Permissions
- Changing Owners and Groups
- Changing Ownership
- Changing Group Ownership
- SUID and SGID File Permission
- Ubuntu Documentation
- Understanding and Using File Permissions
- Folder/Directory Permissions
- Permissions in Action
- Changing Permissions
- Modify File Permissions with chmod
- Modify File Permissions with chmod
- Basics of Linux File Permissions
- How to Use chmod
- Changing File Permissions with chmod
- Using Symbolic Notation Syntax with chmod
- Using Octal Notation Syntax with chmod
- Examples of Common Permissions with chmod
- chmod 600 ( rw——- )
- chmod 664 ( rw-rw-r— )
- chmod 777 ( rwxrwxrwx )
- Making a File Executable
- Restore Default File Permissions
- Removing File Permissions with chmod
- Restrict File Access: Remove all Group and World Permissions
- Understanding Linux Directory Permissions
- How To Change Directory Permissions using chmod
- More Information
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.
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 −
Источник
Ubuntu Documentation
Understanding and Using File Permissions
In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user «root» has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.
To change or edit files that are owned by root, sudo must be used — please see RootSudo for details.
If the owner read & execute bit are on, then the permissions are:
There are three types of access restrictions:
Permission
Action
chmod option
There are also three types of user restrictions:
User
ls output
Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.
Folder/Directory Permissions
Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.
Permission
Action
chmod option
(view contents, i.e. ls command)
(create or remove files from dir)
(cd into directory)
read restricts or allows viewing the directories contents, i.e. ls command
write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)
execute restricts or allows changing into the directory, i.e. cd command
» height=»16″ src=»/moin_static198/light/img/icon_cool.png» title=»Info » width=»16″/> Folders (directories) must have ‘execute’ permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus).
Permissions in Action
Using the example above we have the file «/etc/hosts» which is owned by the user root and belongs to the root group.
What are the permissions from the above /etc/hosts ls output?
Changing Permissions
The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.
Источник
Modify File Permissions with chmod
Modify File Permissions with chmod
The chmod command allows users to change read and write permissions in Unix systems. In this guide, we will show you how to modify file and directory permissions with chmod.
Unix-like systems, including the Linux systems that run on the Linode platform, have an incredibly robust access control system that allows systems administrators to effectively permit multiple users access to a single system without giving every user access to every file on the file system. The chmod command is the best and easiest way to modify these file permissions.
This guide provides a brief overview of file permissions and the operation of the chmod command in addition to a number of practical examples and applications of chmod . If you find this guide helpful, please consider our basic administration practices guide and the Linux users and groups guide next.
Basics of Linux File Permissions
All file system objects on Unix-like systems have three main types of permissions: read, write, and execute access. Permissions are bestowed upon three possible classes: the owner, the group, and all other system users.
To view the file permissions of a set of files, use:
In the first column of the output, there are 10 characters that represent the permission bits. To understand why they are called permission bits, see the section on octal notation below.
A way to understand the meaning of this column is to divide the bits into groups.
File type | User | Group | Global |
---|---|---|---|
d Directory | rwx | r-x | r-x |
— Regular file | rw- | r— | r— |
l Symbolic Link | rwx | rwx | rwx |
The first character represents the type of file. The remaining nine bits in groups of three represent the permissions for the user, group, and global respectively. Each stands for:
- r : Read
- w : Write
- x : eXecute
Note that access to files targeted by symbolic links is controlled by the permissions of the targeted file, not the permissions of the link object. There are additional file permissions that control other aspects of access to files.
How to Use chmod
In this guide, chmod refers to recent versions of chmod such as those provided by the GNU project. By default, chmod is included with all images provided by Linode, and as part of the common “base” selection of packages provided in nearly all distributions of Linux-based operating systems.
Changing File Permissions with chmod
To change the file permissions using chmod, run chmod
, swapping in the desired file permissions and the directory or file. The owner can change file permissions for any user, group or others by adding — to remove or + to add certain permissions. These permissions are categorized into read, write, or executable.
In the next few sections, we are going to dive deep into chmod syntax.
Using Symbolic Notation Syntax with chmod
The format of a chmod command is:
Consider the following chmod command:
This grants all members of the usergroup that owns the file
/example.txt write permissions. Other possible options to change permissions of targeted users are:
Who (Letter) | Meaning |
---|---|
u | user |
g | group |
o | others |
a | all |
The + operator grants permissions whereas the — operator takes away permissions. Copying permissions is also possible:
The parameter g=u means grant group permissions to be same as the user’s.
Multiple permissions can be specified by separating them with a comma, as in the following example:
This adds write permissions to the usergroup members, and removes read and write permissions from the “other” users of the system. Finally the a+x adds the execute permissions to all categories. This value may also be specified as +x . If no category is specified, the permission is added or subtracted to all permission categories.
In this notation the owner of the file is referred to as the user (e.g. u+x ).
The -R option applies the modification to the permissions recursively to the directory specified and to all of its contents.
Using Octal Notation Syntax with chmod
Another method for setting permissions is through octal notation.
Here is example of a file permission that is equivalent to chmod u=rwx,g=rx,o= .
The permissions for this file are — rwx r-x — .
Disregarding the first bit, each bit that is occupied with a — can be replaced with a 0 while r , w , or x is represented by a 1 . The resulting conversion is:
This is called octal notation because the binary numbers are converted to base-8 by using the digits 0 to 7:
Binary | Octal | Permission |
---|---|---|
000 | 0 | — |
001 | 1 | –x |
010 | 2 | -w- |
011 | 3 | -wx |
100 | 4 | r– |
101 | 5 | r-x |
110 | 6 | rw- |
111 | 7 | rwx |
Each digit is independent of the other two. Therefore, 750 means the current user can read, write, and execute, the group cannot write, and others cannot read, write, or execute.
744 , which is a typical default permission, allows read, write, and execute permissions for the owner, and read permissions for the group and “world” users.
Either notation is equivalent, and you may choose to use whichever form more clearly expresses your permissions needs.
Examples of Common Permissions with chmod
chmod 600 ( rw——- )
600 permissions means that only the owner of the file has full read and write access to it. Once a file permission is set to 600, no one else can access the file. Example chmod commands (in octal and symbolic notions) setting permissions to 600:
chmod 664 ( rw-rw-r— )
664 ( rw-rw-r— ) enables the following permissions: read and write for the owner; read and write for the group; read for others. If you trust other users within the same group and everyone needs write access to the files, this is a common setting to use. Otherwise 644 permissions can be used to restrict write access to the group. Example chmod commands (in octal and symbolic notions) setting permissions to 664:
chmod 777 ( rwxrwxrwx )
chmod 777 is used to grant permissions to everyone to read, write, and execute a file. While using these permissions is a quick way to overcome a permissions-based error, it’s not a best practice for securing most files and applications. Example chmod commands (in octal and symbolic notions) setting permissions to 777:
Making a File Executable
The following examples changes the file permissions so that any user can execute the file “
Restore Default File Permissions
The default permissions for files on a Unix system are often 600 or 644 . Permissions of 600 mean that the owner has full read and write access to the file, while no other user can access the file. Permissions of 644 mean that the owner of the file has read and write access, while the group members and other users on the system only have read access.
Issue one of the following chmod commands to reset the permissions on a file back to one of the likely defaults:
For executable files, the equivalent settings would be 700 and 755 which correspond to 600 and 644 except with execution permission.
Use one of the following examples to achieve these executable “default” permissions:
Removing File Permissions with chmod
In order to remove read write permissions given to a file, use the following syntax:
For our file example.txt, we can remove read write permissions using chmod for group by running the following command:
To remove chmod read write permissions from the group while adding read write permission to public/others, we can use the following command:
But, if you wish to remove all permissions for group and others, you can do so using the go= instead:
Restrict File Access: Remove all Group and World Permissions
There are a number of cases where administrators and users should restrict access to files, particularly files that contain passwords and other sensitive information. The configuration files for msmtp and Fetchmail (
/.fetchmailrc ) are two common examples.
You can remove all access to these files with commands in one of the following forms:
Understanding Linux Directory Permissions
While directory permissions within Linux are similar to file permissions, there are a few key differences regarding how these permissions affect user operations:
- Read ( r ): User can list the items in a directory (such as when using the ls command).
- Write ( w ): User can add, delete, or rename files in a directory — provided the user also has execute permissions.
- Execute ( x ): User can navigate to the directory (such as when using the cd command).
To view permissions of all files and directories within the working directory, run ls -la . The output will be similar to snippet below. Directories are differentiated from files by the first bit within the permissions. As was covered previously, d stands for directory and — denotes the item is a file.
Permissions on an individual directory can also be viewed by running ls -dl example-directory .
How To Change Directory Permissions using chmod
Directory permissions can be adjusted using the same chmod commands as were previously outlined for modifying file permissions. The following example changes permissions on a directory to 755 (owner has read, write and execute permissions, while users with the group or any other user have read and execute permissions):
In many cases, the permissions should also be changed recursively on all files and subdirectories. This can be done through chmod by using the -R option. To change all permissions for files within a directory to read and write for the owner, read for the group, and read for other users, run the following command:
Still have a few questions?
Join our Community and post your questions for other Linode and Linux enthusiasts to help you out.
Related Questions:
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on Thursday, July 1, 2010.
Источник