{"id":197,"date":"2018-10-10T08:48:00","date_gmt":"2018-10-10T00:48:00","guid":{"rendered":"https:\/\/yeslq.com\/201810197.html"},"modified":"2019-05-07T10:43:20","modified_gmt":"2019-05-07T02:43:20","slug":"the-linux-command-line-permissions-2","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=197","title":{"rendered":"The Linux Command Line&#8212;Permissions"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div>\n<span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div>\n<span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe Unix-like operating systems, such as Linux differ from other computing systems in that they are not only&nbsp;<i>multitasking<\/i>&nbsp;but also&nbsp;<i>multi-user<\/i>.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhat exactly does this mean? It means that more than one user can be operating the computer at the same time. While your computer only has one keyboard and monitor, it can still be used by more than one user. For example, if your computer is attached to a network, or the Internet, remote users can log in via&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/ssh1.html\" style=\"color: #002740;\">ssh<\/a>&nbsp;(secure shell) and operate the computer. In fact, remote users can execute graphical applications and have the output displayed on a remote computer. The X Window system supports this.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe multi-user capability of Unix-like systems is a feature that is deeply ingrained into the design of the operating system. If you remember the environment in which Unix was created, this makes perfect sense. Years ago before computers were &#8220;personal,&#8221; they were large, expensive, and centralized. A typical university computer system consisted of a large mainframe computer located in some building on campus and&nbsp;<i>terminals<\/i>&nbsp;were located throughout the campus, each connected to the large central computer. The computer would support many users at the same time.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn order to make this practical, a method had to be devised to protect the users from each other. After all, you could not allow the actions of one user to crash the computer, nor could you allow one user to interfere with the files belonging to another user.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThis lesson will cover the following commands:<\/div>\n<ul style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/chmod1.html\" style=\"color: #002740;\">chmod<\/a>&nbsp;&#8211; modify file access rights<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/su1.html\" style=\"color: #002740;\">su<\/a>&nbsp;&#8211; temporarily become the superuser<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/sudo1.html\" style=\"color: #002740;\">sudo<\/a>&nbsp;&#8211; temporarily become the superuser<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/chown1.html\" style=\"color: #002740;\">chown<\/a>&nbsp;&#8211; change file ownership<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/chgrp1.html\" style=\"color: #002740;\">chgrp<\/a>&nbsp;&#8211; change a file&#8217;s group ownership<\/li>\n<\/ul>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nFile Permissions<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nOn a Linux system, each file and directory is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file (i.e., run the file as a program).<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTo see the permission settings for a file, we can use the&nbsp;ls&nbsp;command. As an example, we will look at the&nbsp;bash&nbsp;program which is located in the&nbsp;\/bin&nbsp;directory:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls -l \/bin\/bash<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">\n-rwxr-xr-x 1 root root  316848 Feb 27  2000 \/bin\/bash\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere we can see:<\/div>\n<ul style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li>The file &#8220;\/bin\/bash&#8221; is owned by user &#8220;root&#8221;<\/li>\n<li>The superuser has the right to read, write, and execute this file<\/li>\n<li>The file is owned by the group &#8220;root&#8221;<\/li>\n<li>Members of the group &#8220;root&#8221; can also read and execute this file<\/li>\n<li>Everybody else can read and execute this file<\/li>\n<\/ul>\n<div class=\"separator\" style=\"clear: both; text-align: center;\">\n<\/div>\n<div class=\"separator\" style=\"clear: both; text-align: center;\">\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn the diagram below, we see how the first portion of the listing is interpreted. It consists of a character indicating the file type, followed by three sets of three characters that convey the reading, writing and execution permission for the owner, group, and everybody else.<\/p>\n<p><a href=\"http:\/\/yeslq.com\/wp-content\/uploads\/2018\/10\/file_permissions-1.png\" imageanchor=\"1\" style=\"font-family: &quot;Microsoft YaHei&quot;; margin-left: 1em; margin-right: 1em; text-align: center;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"386\" data-original-width=\"660\" height=\"374\" src=\"http:\/\/yeslq.com\/wp-content\/uploads\/2018\/10\/file_permissions-1.png\" width=\"640\" \/><\/a><\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<a href=\"https:\/\/draft.blogger.com\/null\" name=\"chmod\">chmod<\/a><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe&nbsp;chmod&nbsp;command is used to change the permissions of a file or directory. To use it, you specify the desired permission settings and the file or files that you wish to modify. There are two ways to specify the permissions. In this lesson we will focus on one of these, called the&nbsp;<i>octal notation<\/i>&nbsp;method.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIt is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here&#8217;s how it works:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\">rwx rwx rwx = 111 111 111\nrw- rw- rw- = 110 110 110\nrwx --- --- = 111 000 000\n\nand so on...\n\nrwx = 111 in binary = 7\nrw- = 110 in binary = 6\nr-x = 101 in binary = 5\nr-- = 100 in binary = 4\n\n<\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNow, if you represent each of the three sets of permissions (owner, group, and other) as a single digit, you have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set&nbsp;some_file&nbsp;to have read and write permission for the owner, but wanted to keep the file private from others, we would:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;chmod 600 some_file<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere is a table of numbers that covers all the common settings. The ones beginning with &#8220;7&#8221; are used with programs (since they enable execution) and the rest are for other kinds of files.<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\">\n<tbody>\n<tr>\n<td><strong>Value<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>777<\/strong><\/td>\n<td><strong>(rwxrwxrwx)<\/strong>&nbsp;No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.<\/td>\n<\/tr>\n<tr>\n<td><strong>755<\/strong><\/td>\n<td><strong>(rwxr-xr-x)<\/strong>&nbsp;The file&#8217;s owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.<\/td>\n<\/tr>\n<tr>\n<td><strong>700<\/strong><\/td>\n<td><strong>(rwx&#8212;&#8212;)<\/strong>&nbsp;The file&#8217;s owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.<\/td>\n<\/tr>\n<tr>\n<td><strong>666<\/strong><\/td>\n<td><strong>(rw-rw-rw-)<\/strong>&nbsp;All users may read and write the file.<\/td>\n<\/tr>\n<tr>\n<td><strong>644<\/strong><\/td>\n<td><strong>(rw-r&#8211;r&#8211;)<\/strong>&nbsp;The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.<\/td>\n<\/tr>\n<tr>\n<td><strong>600<\/strong><\/td>\n<td><strong>(rw&#8212;&#8212;-)<\/strong>&nbsp;The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nDirectory Permissions<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe&nbsp;chmod&nbsp;command can also be used to control the access permissions for directories. Again, we can use the octal notation to set permissions, but the meaning of the r, w, and x attributes is different:<\/div>\n<ul style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li><b>r<\/b>&nbsp;&#8211; Allows the contents of the directory to be listed if the x attribute is also set.<\/li>\n<li><b>w<\/b>&nbsp;&#8211; Allows files within the directory to be created, deleted, or renamed if the x attribute is also set.<\/li>\n<li><b>x<\/b>&nbsp;&#8211; Allows a directory to be entered (i.e.&nbsp;cd dir).<\/li>\n<\/ul>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere are some useful settings for directories:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\">\n<tbody>\n<tr>\n<td><strong>Value<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>777<\/strong><\/td>\n<td><strong>(rwxrwxrwx)<\/strong>&nbsp;No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.<\/td>\n<\/tr>\n<tr>\n<td><strong>755<\/strong><\/td>\n<td><strong>(rwxr-xr-x)<\/strong>&nbsp;The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.<\/td>\n<\/tr>\n<tr>\n<td><strong>700<\/strong><\/td>\n<td><strong>(rwx&#8212;&#8212;)<\/strong>&nbsp;The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nBecoming The Superuser For A Short While<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIt is often necessary to become the superuser to perform important system administration tasks, but as you have been warned, you should not stay logged in as the superuser. In most distributions, there is a program that can give you temporary access to the superuser&#8217;s privileges. This program is called&nbsp;su&nbsp;(short for substitute user) and can be used in those cases when you need to be the superuser for a small number of tasks. To become the superuser, simply type the&nbsp;su&nbsp;command. You will be prompted for the superuser&#8217;s password:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;su<br \/>Password:<br \/>[root@linuxbox me]#<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAfter executing the&nbsp;su&nbsp;command, you have a new shell session as the superuser. To exit the superuser session, type&nbsp;exit&nbsp;and you will return to your previous session.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn some distributions, most notably Ubuntu, an alternate method is used. Rather than using&nbsp;su, these systems employ the&nbsp;sudo&nbsp;command instead. With&nbsp;sudo, one or more users are granted superuser privileges on an as needed basis. To execute a command as the superuser, the desired command is simply preceeded with the&nbsp;sudo&nbsp;command. After the command is entered, the user is prompted for the user&#8217;s password rather than the superuser&#8217;s:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;sudo some_command<br \/>Password:<br \/>[me@linuxbox me]$<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nChanging File Ownership<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nYou can change the owner of a file by using the&nbsp;chown&nbsp;command. Here&#8217;s an example: Suppose I wanted to change the owner of&nbsp;some_file&nbsp;from &#8220;me&#8221; to &#8220;you&#8221;. I could:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;su<br \/>Password:<br \/>[root@linuxbox me]#&nbsp;chown you some_file<br \/>[root@linuxbox me]#&nbsp;exit<br \/>[me@linuxbox me]$<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNotice that in order to change the owner of a file, you must be the superuser. To do this, our example employed the&nbsp;su&nbsp;command, then we executed&nbsp;chown, and finally we typed&nbsp;exit&nbsp;to return to our previous session.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nchown&nbsp;works the same way on directories as it does on files.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nChanging Group Ownership<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe group ownership of a file or directory may be changed with&nbsp;chgrp. This command is used like this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;chgrp new_group some_file<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn the example above, we changed the group ownership of&nbsp;some_file&nbsp;from its previous group to &#8220;new_group&#8221;. You must be the owner of the file or directory to perform a&nbsp;chgrp.<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts The Unix- &#8230; <a title=\"The Linux Command Line&#8212;Permissions\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=197\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Permissions\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,52,53],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=197"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}