- Ubuntu Documentation
- Understanding and Using File Permissions
- Folder/Directory Permissions
- Permissions in Action
- Changing Permissions
- How to manage Linux permissions for users, groups, and others
- Linux security
- Setting up a playground
- How do I create directories and files?
- How do I manage ownership and groups?
- How do I manage permissions?
- How do I use absolute mode?
- More Linux resources
- How do I use symbolic mode?
- Special permissions and Access Control Lists
- Wrap up
- Manage file permissions on Unix-like systems
- Overview
- View file permissions
- Change file permissions
- Symbolic method
- Absolute form
- Common issues when sharing data with other users
- Get help
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.
Источник
How to manage Linux permissions for users, groups, and others
Photo by Min An from Pexels
Managing access to resources is a fundamental task for sysadmins. This responsibility consists of three components: identities, resources, and permissions. This article covers several user, group, and file management commands to control access to resources. The article uses a «How do I…?» format, and it assumes you have a few resources to work with. Specifically, I cover the following topics:
- Creating directories and files
- Managing ownership and associated groups
- Setting permissions with absolute and symbolic modes
Linux security
Setting up a playground
I’ve been in IT for about 25 years, and most of that time was spent as a technical trainer. That means that the things that I write are usually structured as some sort of lab or other hands-on opportunity. It’s just how I cover material. With that in mind, I’ll assume you have a couple of identities and resources to experiment with as you read the rest of the article. You can use the following commands to set up a playground. It’s best to do this on a virtual machine rather than your personal Linux box, but these tasks are relatively harmless.
Create two new users and two new groups to work with. Note that you do not need to configure passwords for the users in this exercise, as you won’t log on with those accounts.
Note: You would use the passwd user01 command to set the user’s password.
In your home directory, create a new directory named playground :
Change into the
/playground directory by using the cd command. You are ready to work with the commands and concepts below.
When you’ve completed the article and learned the techniques I’ve covered, delete the two user accounts, the groups, and the playground directory. Use rm -fR /playground , userdel user01 , and groupdel groupA to remove the resources.
How do I create directories and files?
Use the mkdir command to create directories. The touch command is one of many ways to create files.
How do I create a directory named Resources ?
How do I create a directory path (a series of directories that don’t yet exist)?
Note: The goal here is to create the 2020data directory, but the given path’s data directory does not yet exist. The -p option creates parent directories as needed to complete the path.
How do I create a file named file1 ?
How do I create several files at once?
How do I manage ownership and groups?
In the playground directory, display the current owner and group associated with the Resources directory and the files.
How do I display permission, owners, and groups?
The ls -l command displays directory contents in long format. The long format contains both permissions and ownership. You can see that the user account that created the resources also owns those resources. The group association is also that user’s primary group.
How do I change the user/owner associated with file1 ?
How do I change the group associated with file1 ?
How do I change the owner and group at the same time for file2 ?
There is a specific chgrp command, but I prefer only to memorize one command ( chown ) and apply it to both functions (user and group associations) rather than chown for the user and then have to recall chgrp for the group.
So how do I use chgrp ?
How do I change the user/group for a directory and all of its contents?
The above task provides a recursive configuration. Technically, recursive commands are repeated on each specified object. Effectively, recursive means «this and everything in it.» In the above example, you are configuring the related user/group for the Resources directory and everything in it. Without the -R option, you would only affect the Resources directory itself, but not its contents.
How do I manage permissions?
The change mode or chmod command sets permissions. The syntax is straight-forward:
Here are two examples of manipulating permissions for file2 :
But wait! Those appear to be radically different examples (they’re not, actually). What are all those letters and numbers?
We need to discuss absolute mode and symbolic mode.
How do I use absolute mode?
Absolute mode is one of two ways of specifying permissions. I’ve seen this mode referred to as octal or numeric mode, but the term I learned was absolute. That term also makes the most sense to me because it’s an absolute statement of the desired permissions. I always told my students that this seemed like the most complex of the two modes but is actually the simplest. Usually, they agreed.
Each access level (read, write, execute) has an octal value:
Access level | Octal value |
Read | 4 |
Write | 2 |
Execute | 1 |
Each identity (user, group, others) has a position:
Identity | Position |
User | First or left-most |
Group | Middle |
Others | Last or right-most |
More Linux resources
The absolute mode syntax states the desired permissions from left to right.
How do I grant the user (owner) read, write, and execute, the group read-only, and all others no access to file2 by using absolute mode?
The three permissions values are associated with identities:
ugo
740
- The 7 is assigned to the user and is the sum of 4+2+1 or read+write+execute (full access)
- The 4 is assigned to the group and is the sum of 4+0+0 (read-only)
- The 0 is assigned to others and is the sum of 0+0+0 (no access)
In this example, the user has rwx, the group has r only, and all others have no access to file2 .
Let’s look at one more example.
How do I grant the user (owner) read and write, the group read-only, and all others read-only to file2 ?
- The user has 6 (read and write)
- The group has 4 (read-only)
- All others have 4 (read-only)
I find this easier because there are no calculations involved. I’m not concerned with adding or subtracting specific permissions based on the current settings. Instead, I say, «set the permissions to be this,» and that’s the end result I get. It’s an absolute statement.
How do I set permissions for the Resources directory and all of its contents by using absolute mode?
How do I use symbolic mode?
Symbolic mode uses more symbols, but the symbols are simpler to understand. That’s attractive to sysadmins that are new to standard Linux permissions.
Each access level has a symbol:
Access level | Symbol |
Read | r |
Write | w |
Execute | x |
Each identity has a symbol:
Identity | Symbol |
User | u |
Group | g |
Others | o |
There are also operators to manipulate the permissions:
Task | Operator |
Grant a level of access | + |
Remove a level of access | — |
Set a level of access | = |
The general chmod command syntax is the same:
Here is an example:
How do I remove the read permissions from others for file2 by using symbolic mode?
This example removes ( — ) the read ( r ) permission from others ( o ) for file2 .
Here’s another simple example:
How do I grant the read and write permissions to the group for file2 ?
This one gives ( + ) read and write ( rw ) to the group ( g ) for file2 .
How do I set permissions for a directory and all of its contents by using symbolic mode?
Special permissions and Access Control Lists
The above discussion covers standard Linux permissions—applying rwx to the user, group, and all others. Linux has far more flexibility, however. Special permissions permit users to run applications with other credentials, control the inheritance of group associations, and keep files from being changed accidentally. Check out this great article on special permissions.
Linux also has a way of enforcing different permissions for different users and groups. Access Control Lists (ACLs) permit sysadmins to define permissions for more than just one user and one group, which adds a great deal more flexibility to standard permissions. For example, user01 can be granted rw- to file1 , while user02 can be granted r— to file1 . Here is a great article on ACLs.
Wrap up
Creating resources, managing users, and setting permissions are fundamental tasks for Linux users. My goal was to provide a quick and easy guide based on common questions or tasks that we must all accomplish regularly. If you’re new to Linux, having a solid grasp of the eight commands discussed above will make your sysadmin life much easier.
Источник
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.
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.
Источник