As many people can access the system simultaneously and some resources are shared, Linux controls access through ownership and permissions. Show
Linux file ownershipIn Linux, there are three types of owners: Linux UserA user is the default owner and creator of the file. So this user is called owner as well. Linux GroupA user-group is a collection of users. Users that belonging to a group will have the same Linux group permissions to access a file/ folder. You can use groups to assign permissions in a bulk instead of assigning them individually. A user can belong to more than one group as well. OtherAny users that are not part of the user or group classes belong to this class. Linux File PermissionsFile permissions fall in three categories: 0, 1, and 2.Read permissionFor regular files, read permissions allow users to open and read the file only. Users can't modify the file. Similarly for directories, read permissions allow the listing of directory content without any modification in the directory. Write permissionWhen files have write permissions, the user can modify (edit, delete) the file and save it. For folders, write permissions enable a user to modify its contents (create, delete, and rename the files inside it), and modify the contents of files that the user has write permissions to. Execute permissionFor files, execute permissions allows the user to run an executable script. For directories, the user can access them, and access details about files in the directory. Below is the symbolic representation of permissions to user, group, and others. Symbolic representation of permissionsNote that we can find permissions of files and folders using long listing ( 3) on a Linux terminal.Output of long listingIn the output above, 4 represents a directory and 5 represents a regular file.How to Change Permissions in Linux Using the chmod u+x mymotd.sh6 CommandNow that we know the basics of ownerships and permissions, let's see how we can modify permissions using the 6 command.Syntax of 6:
Where,
We can change permissions using two modes:
Now, let's see them in detail. How to Change Permissions using Symbolic ModeThe table below summarize the user representation: User representationDescriptionuuser/ownerggroupootherWe can use mathematical operators to add, remove, and assign permissions. The table below shows the summary: OperatorDescription+Adds a permission to a file or directory–Removes the permission=Sets the permission if not present before. Also overrides the permissions if set earlier.Example:Suppose, I have a script and I want to make it executable for owner of the file 5.Current file permissions are as follows: Let's split the permissions like this: To add execution rights ( 6) to owner ( 1) using symbolic mode, we can use the command below:
Output: Now, we can see that the execution permissions have been added for owner 5.Additional examples for changing permissions via symbolic method:
How to Change Permissions using Absolute ModeAbsolute mode uses numbers to represent permissions and mathematical operators to modify them. The below table shows how we can assign relevant permissions: PermissionProvide permissionreadadd 4writeadd 2executeadd 1Permissions can be revoked using subtraction. The below table shows how you can remove relevant permissions. PermissionRevoke permissionreadsubtract 4writesubtract 2executesubtract 1Example:
5This is how we performed the calculation: Note that this is the same as 6.
To remove execution from 8 and group , subtract 1 from the execute part of last 2 octets.
This would be the same as 0.How to Change Ownership using the groupadd project-manager useradd -G project-manager Fatima passwd Fatima 1 CommandNext, we will learn how to change the ownership of a file. You can change the ownership of a file or folder using the 1 command. In some cases, changing ownership requires 3 permissions.Syntax of 1:
How to change user ownership with groupadd project-manager useradd -G project-manager Fatima passwd Fatima 1Let's transfer the ownership from user 5 to user 7. 8Command to change ownership: 9Output: How to change user and group ownership simultaneouslyWe can also use 1 to change user and group simultaneously.
How to change directory ownershipYou can change ownership recursively for contents in a directory. The example below changes the ownership of the
How to change group ownershipIn case we only need to change the group owner, we can use 1 by preceding the group name by a colon user 4
Linux Permissions Guided ExerciseUp until now we have explored permissions, ownerships, and the methods to change them. Now we will reinforce our learning with a guided exercise. Goal: To create groups and assign relevant permissions to its members. Verify access by accessing it from unauthorized users. Task: Create a group called Create another group Visualization of the problemWe can visualize the problem like this: Step 1: Switch to root user. Use the 3 command with flag group 4.If you have the root password, you can login using that as well. Show solutionEnter Enter If you do not have 3.Step 2: Create a group Use the Syntax: Enter Verify: Step 3: Create two new users John and Bob and add them to the Use command
Syntax: Where 00 01Verify: Step 4: Provide passwords for users John and Bob Show hintUse command 03 03 creates a password for users.Syntax: 05Show solution 06 07Step 5: Create a directory in /home and name it Use command 09 09 creates a directory.Syntax: 11Show solution 12Verify: Step 6: Change the group ownership of the folder Use command 1Syntax: 16Show solution 17Step 7: Make sure the permissions of folder Use command 6Write permissions allow users and groups to create and delete files. Syntax: 20Show solution 21Step 8: Ensure that 'others' don't have any access to the files of Use command 6Remove read, write, execute permissions from 'others' if they exist. Syntax: 20Show solution 25Step 9: Exit the 27Show hintUse command 28 to logout of the root user.Use 29 to switch users.Syntax: 30To confirm current user, use command 28 33Verify with command Step 10: Navigate to folder: Use command 36 to switch folders.Syntax: 37Confirm current path with 38.Show solution 39Step 11: Create an empty file in the folder: Use command 41 to create an empty file.Syntax: 42Show solution 43Verify: 44Step 12: Change the group ownership of the created file to Use command 1 to change ownership.Syntax: 47Show solution 48Once group ownership is modified, all members of the group can access this file. Verify 44Step 13: Exit the shell and switch to user 50Show hintUse command 28 to exit the terminal.Use 29 to switch users.Syntax: 30To confirm current user, use command 28 56Verify the current user with command Step 14: Navigate to the path Use command 36 to switch folders.Syntax: 37Confirm current path with 38.Show solution 39Step 15: Find out 63 privileges to access 64Show hintUse command 3 for long listing.Syntax: 66Does group have 67 permissions?Show solution 68Step 16: Modify the file 64 while logged in as 50Show hintUse command 71 to add some text to the file.Syntax: 72This would redirect the quoted text to end of the file. Show solution 73If all the permissions are correctly set, 50 would be allowed to edit and save this file. Otherwise you would get an error like this: 75.Verify 76Step 17: Create another group Use command Syntax: Create a new user with command Use flag
Step 18: Navigate to folder Use 36 to navigate to user 6.Show solution 39.We get this error: This is because, If we recall, below are the rights of the Wrapping upPermissions and ownerships are useful concepts for enforcing security across multi-user operating systems. I hope you were able to learn about changing permissions and ownerships in depth. What’s your favorite thing you learned from this tutorial? Let me know on Twitter! You can also read my other posts here. Thanks to Tom Mondloch for his help with the guided exercise. ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT I am a DevOps Consultant and writer at FreeCodeCamp. I aim to provide easy and to-the-point content for Techies! If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What are the commands to change owner and group owner of a file or directory?Use the following procedure to change the group ownership of a file.. Become superuser or assume an equivalent role.. Change the group owner of a file by using the chgrp command. $ chgrp group filename. group. ... . Verify that the group owner of the file has changed. $ ls -l filename.. Which of these commands can change both the owner of the file and the group of the file?The chown command is used to change the file owner or group. Syntax: chown [ OPTION ] [OWNER][:[GROUP]] FILE… chown [ OPTION ] –reference=RFILE FILE…
Which command changes the owner and groups on a file or folder Linux?The chown command changes the owner of a file, and the chgrp command changes the group.
Which command is used to change the group ownership of a file called Dockerfile?Short for change ownership, Chown command is a command-line utility that is used to change the user or group ownership of a file or directory and even links. The Linux philosophy is such that every file or directory is owned by a specific user or group with certain access rights.
|