Advertisement
xdxdxd123

linux permissions coursera

Mar 17th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. Folder Permissions in Linux
  2. Introduction
  3. In this lab, you'll learn the foundations of how managing user permissions works on a Linux machine. Using the new commands you learned in Bash, you'll fix up the permissions of some files and folders.
  4.  
  5. Head's up: You'll experience a delay as the labs initially load (particularly for Windows labs). So, please wait a couple of minutes for the labs to load. Please also make sure to access the labs directly through Coursera and not in the Qwiklabs catalog. If you access the labs through the Qwiklabs catalog, you will *not* receive a grade. (As you know, a passing grade is required to matriculate through the course.) The grade is calculated when the lab is complete, so be sure to hit "End Lab" when you're done!
  6.  
  7. You'll have 60 minutes to complete this lab.
  8.  
  9. What you'll do
  10.  
  11. Familiarize yourself with the process of changing permissions within a file and folder in Linux
  12. Change the ownership of a specific file and folder
  13. Go ahead and connect to the linux-instance Google Cloud instance now. As a reminder, your machines are available in Google Cloud Console under Compute Engine -> VM instances. If you're having difficulty connecting to your instance, now worries! Just follow the steps outlined in the Accessing Qwiklabs reading for detailed instructions on how to connect.
  14.  
  15. Checking permissions
  16.  
  17. Before you can even begin changing the permission of a file or folder, you need to first check the permissions of the specific file/folder. To display ownership and permissions for a file, you can use ls with the -l flag and the name of the file you're interested in:
  18.  
  19. ls -l [FILENAME]
  20. There's a file named "important_document" on your machine in the "/home/qwiklab/documents" directory. You can change to this directory from your current one using this command:
  21.  
  22. cd ../qwiklab/documents
  23. Check out its current permissions with this command, and take a look at the output below:
  24.  
  25. ls -l important_document
  26.  
  27.  
  28. You can see that it's owned by the "root" user, and that the owner has read and write (but not execute) permissions while everyone else has none at all.
  29.  
  30. Changing file permissions
  31.  
  32. Now, change the permissions of "important_document" (from the previous step) so that the owner has execute permissions on top of their current permissions. To do this, you'll use the chmod command, with the argument 700. The two zeros keep everyone, but the owner, from having any permissions at all, and the seven grants all available permissions to the owner (including execute). Keep in mind that because the file is owned by "root" you'll need to use sudo:
  33.  
  34. sudo chmod 700 important_document
  35. If you use ls to check the permissions, you'll now see that the execution permission has been added:
  36.  
  37.  
  38.  
  39. Changing folder permissions
  40.  
  41. Now you'll do a similar process, this time on a directory. First, move up one directory:
  42.  
  43. cd ..
  44. In this directory there's a folder called "secret_folder". View its current permissions using ls, this time with the -ld flag rather than -l because you're viewing a directory instead of a normal file:
  45.  
  46. ls -ld secret_folder/
  47.  
  48.  
  49. As you can see, the owner of the file (the root user) has read and write permissions, and everyone else can read only.
  50.  
  51. The goals for the lab, related to this file, are below:
  52.  
  53. The owner should have all permissions.
  54. The group should have only write permission.
  55. People other than the owner and the group should have no permissions.
  56. Head's up: When using chmod on a directory, files within that directory are not affected. While this isn't relevant to this specific lab, it's important to remember.
  57.  
  58. Previously, we used a numerical argument to set the permissions for a file. If you want to avoid figuring out the number that matches the permission levels, you can use an alternate syntax. To satisfy the first condition, you only need to add the execute permission to the owner, since they already have read and write permissions. To add execute to the owner's permissions, you can use the command below. (Note that "u" stands for "user" and "x" stands for "execute".)
  59.  
  60. sudo chmod u+x secret_folder/
  61. You can check the permissions again to see that the owner can now read, write, and execute:
  62.  
  63. ls -ld secret_folder/
  64.  
  65.  
  66. Now you can fix the group's permission. They currently have read permission and don't have write permission, which you can fix with two similar commands. These can be done in either order; "g" stands for "group" (like "u" from before), and "w" and "r" stand for "write" and "read" respectively:
  67.  
  68. sudo chmod g+w secret_folder/
  69. sudo chmod g-r secret_folder/
  70. You can check the permissions again to see that the group now has only write permissions, and the owner has the same permissions as before:
  71.  
  72. ls -ld secret_folder/
  73.  
  74.  
  75. Finally, you can remove read permissions from everyone else using the command below ("o" stands for "other"):
  76.  
  77. sudo chmod o-r secret_folder/
  78. You can see that all the criteria for this file are now met using ls again:
  79.  
  80. ls -ld secret_folder/
  81.  
  82.  
  83. Using chmod this way is easier to remember, but takes lots more commands. All the previous steps could also have been done using the numerical notation, like this:
  84.  
  85. sudo chmod 720 secret_folder/
  86. Changing owners
  87.  
  88. Now you'll change the owner of a file. In your current directory, there's a folder called "taco". Use ls to examine its permissions and see who the owner of the file is:
  89.  
  90. ls -ld taco/
  91.  
  92.  
  93. You can see from this that the root user currently owns this file. There's another user account on the machine called "cook". Go ahead and make "cook" the owner of the file, using the chown command like this:
  94.  
  95. sudo chown cook /home/qwiklab/taco
  96. Now you can use ls again to see that the owner of the file has been successfully changed:
  97.  
  98. ls -ld taco/
  99.  
  100.  
  101. More practice
  102.  
  103. There are a few more files present on your machine that you can practice on. First, move into the "documents" folder:
  104.  
  105. cd documents/
  106. There's a file in this folder called "not_so_important_document". View its permissions to see its current state:
  107.  
  108. ls -l not_so_important_document
  109.  
  110.  
  111. The owner can read and write, the group can read, and everybody else has no permissions at all. Now, use chmod to change the permissions so that these criteria are met:
  112.  
  113. The owner has all permissions.
  114. The group has read and write permissions.
  115. Everyone has read permissions.
  116. To give the owner execution permissions, you can use the same command from earlier:
  117.  
  118. sudo chmod u+x not_so_important_document
  119. Remember to use ls to double-check that everything you do behaves how you expect:
  120.  
  121. ls -l not_so_important_document
  122.  
  123.  
  124. The group already has read permissions, so all you need to do is add write permissions:
  125.  
  126. sudo chmod g+w not_so_important_document
  127. ls -l not_so_important_document
  128.  
  129.  
  130. Finally, you need to give everyone else read permissions. You can use the "o+r" argument to add read permissions to people other than the owner or group, but you can also use "a+r". This adds read permission to everyone (owner, group, and other). Because the owner and the group already have read permissions, this will only change the permissions for everyone else, but the end result is the same:
  131.  
  132. sudo chmod a+r not_so_important_document
  133. You should be able to view the permissions again and see that all criteria for this file have been met:
  134.  
  135. ls -l not_so_important_document
  136.  
  137.  
  138. Again, you can accomplish the same result using a numerical argument to set the permissions, rather than incrementally changing them. Here's the command that meets all three criteria at once:
  139.  
  140. sudo chmod 764 not_so_important_document
  141. Adding multiple permissions at once
  142.  
  143. Finally, you'll learn how to use the non-numeric argument to add multiple permissions at once. There's one more file in the current directory, named "public_document". First, view its current permissions:
  144.  
  145. ls -l public_document
  146.  
  147.  
  148. For this file, you want everyone (owner, group, and anyone else) to have all permissions. You can add read, write, and execute permissions to everyone at once using this command:
  149.  
  150. sudo chmod a+rwx public_document
  151. This should make the file as open as possible, where every user has every permission:
  152.  
  153. ls -l public_document
  154.  
  155.  
  156. Using the numeric argument form of chmod, this same result could be accomplished with this command:
  157.  
  158. sudo chmod 777 public_document
  159. Conclusion
  160.  
  161. Congrats! You've successfully used chmod on both directories and normal files, in multiple formats. You can directly set a file's permissions numerically, or add and remove specific permissions one at a time. You've also successfully used chown to change the owner of a file. Really great work!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement