New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich Brey - Lesson Contribution - Ownership and Permissions in Linux #841

Open
sheraaron opened this Issue Aug 31, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@sheraaron

sheraaron commented Aug 31, 2018

Ownership and Permissions in Linux - quick overview

Files and directories in Linux have an owner and a set of permissions that determine how users can interact with them.

There are 3 classes to which permissions are applied
1. Owner
2. Group
3. Other

Each class may have 3 different permission levels
1. Read (r--) Allowed to read the contents of the file
2. Write (-w-) Allowed to modify or delete the file
3. Execute (--x) Allowed to run the file as a process, if possible

Example of viewing file permissions:
-bash-4.2$ touch testfile
-bash-4.2$ ls -l testfile
-rw-rw-r-- 1 user1 user1 0 Aug 30 09:44 testfile

In this example, user1 is the account that owns testfile, and user1 is also the group assigned to the file. The -rw-rw-r-- at the left indicates the permissions. The first character, "-", indicates that testfile is a file, not a directory. The rw- shows the permissions for the user, or owner, of the file, user1. The r indicates read permission; the w, write permission; and the -, indicating the file can't currently be executed. The next three characters, rw-, show permissions for the group class, which is also user1 in this example. Finally, the last three characters, r--, display permissions for the other class - any account that is not user1 and is not in the user1 group.

Changing permissions

The chmod command is used to change file and directory permissions in linux. The format of the chmod command is chmod [permission] [file/directory name].

Example:

-bash-4.2$ chmod +x testfile
-bash-4.2$ ls -l testfile
-rwxrwxr-x 1 user1 user1 0 Aug 30 09:44 testfile

in this case, perhaps testfile is a shell script that we want to execute. The +x flag tells chmod to add the execute permission to the file, for all classes. chmod can also be used to set permissions for specific classes:

-bash-4.2$ chmod o-rx testfile
-bash-4.2$ ls -l testfile
-rwxr-x--- 1 user1 user1 0 Aug 30 09:44 testfile

This removed read and execute permissions for Other - now only user1 and members of the user1 group have permissions to the file.

Changing group

The chgrp command is used to change the group assigned to a file or directory. It's used similarly to chmod: chgrp [group name] [file/directory name]

Example:

-bash-4.2$ chgrp testgroup testfile
-bash-4.2$ ls -l testfile
-rwxrwx--- 1 user1 testgroup 0 Aug 30 09:44 testfile

in this example, the group assigned to the file testfile was changed from user1 to testgroup. Now all members of the group testgroup will have read, write, and execute permissions to this file. Group can be changed by the file/directory owner as well as system adiministrators.

Assuming user1 wants group members to be able read and execute this file, but not change it, the group write permission would need to be removed with:

-bash-4.2$ chmod g-w testfile
-bash-4.2$ ls -l testfile
-rwxr-x--- 1 user1 testgroup 0 Aug 30 09:44 testfile

Now members cannot edit the file.

More Information

Linux file system permissions can be complex, for additional information see:

chmod --help
chgrp --help

and

man chmod
man chgrp

Submitted by Richar Brey

@gcapes

This comment has been minimized.

Contributor

gcapes commented Sep 7, 2018

My thoughts would be that this is out of the scope of a novice lesson, and is covered in the shell-extras lesson: http://swcarpentry.github.io/shell-extras/04-permissions/

@jttkim

This comment has been minimized.

Contributor

jttkim commented Sep 7, 2018

If the concern is to avoid overloading the novice lesson I very much understand that. However, I also think there is a case for a quick introduction of chmod u+x. This could be done without expanding about groups and others, and what permissions mean for regular files and directories etc. My reason for thinking this is that after running shell novice based courses in-house or in contexts where I've been around after the lesson, questions to which chmod u+x is the answer are the most frequent ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment