{"id":874,"date":"2015-08-13T16:03:21","date_gmt":"2015-08-13T23:03:21","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=874"},"modified":"2015-08-13T16:03:21","modified_gmt":"2015-08-13T23:03:21","slug":"unix-file-permission-access-modes","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/08\/13\/unix-file-permission-access-modes\/","title":{"rendered":"Unix &#8211; File Permission \/ Access Modes"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><\/span><\/p>\n<p>File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes \u2212<\/p>\n<ul class=\"list\">\n<li><b>Owner permissions<\/b> \u2212 The owner&#8217;s permissions determine what actions the owner of the file can perform on the file.<\/li>\n<li><b>Group permissions<\/b> \u2212 The group&#8217;s permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.<\/li>\n<li><b>Other (world) permissions<\/b> \u2212 The permissions for others indicate what action all other users can perform on the file.<\/li>\n<\/ul>\n<h2>The Permission Indicators<\/h2>\n<p>While using <b>ls -l<\/b> command it displays various information related to file permission as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l <\/span><span class=\"pun\">\/<\/span><span class=\"pln\">home<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">amrood\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xr<\/span><span class=\"pun\">--<\/span>  <span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  myfile\ndrwxr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xr<\/span><span class=\"pun\">---<\/span> <span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  mydir<\/span><\/pre>\n<p>Here first column represents different access mode ie. permission associated with a file or directory.<\/p>\n<p>The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x) \u2212<\/p>\n<ul class=\"list\">\n<li>The first three characters (2-4) represent the permissions for the file&#8217;s owner. For example &#8211;<span style=\"color: red;\">rwx<\/span><span style=\"color: green;\">r-x<\/span><b>r&#8211;<\/b> represents that onwer has read (r), write (w) and execute (x) permission.<\/li>\n<li>The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example &#8211;<span style=\"color: red;\">rwx<\/span><span style=\"color: green;\">r-x<\/span><b>r&#8211;<\/b> represents that group has read (r) and execute (x) permission but no write permission.<\/li>\n<li>The last group of three characters (8-10) represents the permissions for everyone else. For example &#8211;<span style=\"color: red;\">rwx<\/span><span style=\"color: green;\">r-x<\/span><b>r&#8211;<\/b> represents that other world has read (r) only permission.<\/li>\n<\/ul>\n<h2>File Access Modes<\/h2>\n<p>The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the <b>read<\/b>, <b>write<\/b>, and <b>execute<\/b> permissions, which are described below \u2212<\/p>\n<h2>1. Read<\/h2>\n<p>Grants the capability to read ie. view the contents of the file.<\/p>\n<h2>2. Write<\/h2>\n<p>Grants the capability to modify, or remove the content of the file.<\/p>\n<h2>3. Execute<\/h2>\n<p>User with execute permissions can run a file as a program.<\/p>\n<h2>Directory Access Modes<\/h2>\n<p>Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned:<\/p>\n<h2>1. Read<\/h2>\n<p>Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.<\/p>\n<h2>2. Write<\/h2>\n<p>Access means that the user can add or delete files to the contents of the directory.<\/p>\n<h2>3. Execute<\/h2>\n<p>Executing a directory doesn&#8217;t really make a lot of sense so think of this as a traverse permission.<\/p>\n<p>A user must have execute access to the <b>bin<\/b> directory in order to execute ls or cd command.<\/p>\n<h2>Changing Permissions<\/h2>\n<p>To change file or directory permissions, you use the <b>chmod<\/b> (change mode) command. There are two ways to use chmod: symbolic mode and absolute mode.<\/p>\n<h2>Using chmod in Symbolic Mode<\/h2>\n<p>The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Chmod operator<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><b>+<\/b><\/td>\n<td>Adds the designated permission(s) to a file or directory.<\/td>\n<\/tr>\n<tr>\n<td><b>&#8211;<\/b><\/td>\n<td>Removes the designated permission(s) from a file or directory.<\/td>\n<\/tr>\n<tr>\n<td><b>=<\/b><\/td>\n<td>Sets the designated permission(s).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here&#8217;s an example using testfile. Running ls -1 on testfile shows that the file&#8217;s permissions are as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxrwxr<\/span><span class=\"pun\">--<\/span>  <span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile<\/span><\/pre>\n<p>Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$chmod o<\/span><span class=\"pun\">+<\/span><span class=\"pln\">wx testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxrwxrwx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile\n$chmod u<\/span><span class=\"pun\">-<\/span><span class=\"pln\">x testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rw<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxrwx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile\n$chmod g<\/span><span class=\"pun\">=<\/span><span class=\"pln\">rx testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rw<\/span><span class=\"pun\">-<\/span><span class=\"pln\">r<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xrwx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile<\/span><\/pre>\n<p>Here&#8217;s how you could combine these commands on a single line:<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$chmod o<\/span><span class=\"pun\">+<\/span><span class=\"pln\">wx<\/span><span class=\"pun\">,<\/span><span class=\"pln\">u<\/span><span class=\"pun\">-<\/span><span class=\"pln\">x<\/span><span class=\"pun\">,<\/span><span class=\"pln\">g<\/span><span class=\"pun\">=<\/span><span class=\"pln\">rx testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rw<\/span><span class=\"pun\">-<\/span><span class=\"pln\">r<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xrwx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile<\/span><\/pre>\n<h2>Using chmod with Absolute Permissions<\/h2>\n<p>The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.<\/p>\n<p>Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Number<\/th>\n<th>Octal Permission Representation<\/th>\n<th>Ref<\/th>\n<\/tr>\n<tr>\n<td><b>0<\/b><\/td>\n<td>No permission<\/td>\n<td>&#8212;<\/td>\n<\/tr>\n<tr>\n<td><b>1<\/b><\/td>\n<td>Execute permission<\/td>\n<td>&#8211;x<\/td>\n<\/tr>\n<tr>\n<td><b>2<\/b><\/td>\n<td>Write permission<\/td>\n<td>-w-<\/td>\n<\/tr>\n<tr>\n<td><b>3<\/b><\/td>\n<td>Execute and write permission: 1 (execute) + 2 (write) = 3<\/td>\n<td>-wx<\/td>\n<\/tr>\n<tr>\n<td><b>4<\/b><\/td>\n<td>Read permission<\/td>\n<td>r&#8211;<\/td>\n<\/tr>\n<tr>\n<td><b>5<\/b><\/td>\n<td>Read and execute permission: 4 (read) + 1 (execute) = 5<\/td>\n<td>r-x<\/td>\n<\/tr>\n<tr>\n<td><b>6<\/b><\/td>\n<td>Read and write permission: 4 (read) + 2 (write) = 6<\/td>\n<td>rw-<\/td>\n<\/tr>\n<tr>\n<td><b>7<\/b><\/td>\n<td>All permissions: 4 (read) + 2 (write) + 1 (execute) = 7<\/td>\n<td>rwx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here&#8217;s an example using testfile. Running ls -1 on testfile shows that the file&#8217;s permissions are as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxrwxr<\/span><span class=\"pun\">--<\/span>  <span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile<\/span><\/pre>\n<p>Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chmod <\/span><span class=\"lit\">755<\/span><span class=\"pln\"> testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">x  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile\n$chmod <\/span><span class=\"lit\">743<\/span><span class=\"pln\"> testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">rwxr<\/span><span class=\"pun\">---<\/span><span class=\"pln\">wx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile\n$chmod <\/span><span class=\"lit\">043<\/span><span class=\"pln\"> testfile\n$ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l testfile\n<\/span><span class=\"pun\">----<\/span><span class=\"pln\">r<\/span><span class=\"pun\">---<\/span><span class=\"pln\">wx  <\/span><span class=\"lit\">1<\/span><span class=\"pln\"> amrood   users <\/span><span class=\"lit\">1024<\/span>  <span class=\"typ\">Nov<\/span> <span class=\"lit\">2<\/span> <span class=\"lit\">00<\/span><span class=\"pun\">:<\/span><span class=\"lit\">10<\/span><span class=\"pln\">  testfile<\/span><\/pre>\n<h2>Changing Owners and Groups<\/h2>\n<p>While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions mentioned above are also assigned based on Owner and Groups.<\/p>\n<p>Two commands are available to change the owner and the group of files \u2212<\/p>\n<ul class=\"list\">\n<li><b>chown<\/b> \u2212 The chown command stands for &#8220;change owner&#8221; and is used to change the owner of a file.<\/li>\n<li><b>chgrp<\/b> \u2212 The chgrp command stands for &#8220;change group&#8221; and is used to change the group of a file.<\/li>\n<\/ul>\n<h2>Changing Ownership<\/h2>\n<p>The chown command changes the ownership of a file. The basic syntax is as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chown user filelist<\/span><\/pre>\n<p>The value of user can be either the name of a user on the system or the user id (uid) of a user on the system.<\/p>\n<p>Following example \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chown amrood testfile\n$<\/span><\/pre>\n<p>Changes the owner of the given file to the user <b>amrood<\/b>.<\/p>\n<p><b>NOTE:<\/b> The super user, root, has the unrestricted capability to change the ownership of a any file but normal users can change only the owner of files they own.<\/p>\n<h2>Changing Group Ownership<\/h2>\n<p>The chrgp command changes the group ownership of a file. The basic syntax is as follows \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chgrp <\/span><span class=\"kwd\">group<\/span><span class=\"pln\"> filelist<\/span><\/pre>\n<p>The value of group can be the name of a group on the system or the group ID (GID) of a group on the system.<\/p>\n<p>Following example \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chgrp special testfile\n$<\/span><\/pre>\n<p>Changes the group of the given file to <b>special<\/b> group.<\/p>\n<h2>SUID and SGID File Permission<\/h2>\n<p>Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.<\/p>\n<p>As an example, when you change your password with the <b>passwd<\/b> command, your new password is stored in the file \/etc\/shadow.<\/p>\n<p>As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have write permission to this file. This means that the <b>passwd<\/b> program has to give you additional permissions so that you can write to the file \/etc\/shadow.<\/p>\n<p>Additional permissions are given to programs via a mechanism known as the Set User ID ( SUID) and Set Group ID ( SGID) bits.<\/p>\n<p>When you execute a program that has the SUID bit enabled, you inherit the permissions of that program&#8217;s owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.<\/p>\n<p>This is true for SGID as well. Normally programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.<\/p>\n<p>The SUID and SGID bits will appear as the letter &#8220;s&#8221; if the permission is available. The SUID &#8220;s&#8221; bit will be located in the permission bits where the owners execute permission would normally reside. For example, the command<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l <\/span><span class=\"pun\">\/<\/span><span class=\"pln\">usr<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">bin<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">passwd\n<\/span><span class=\"pun\">-<\/span><span class=\"pln\">r<\/span><span class=\"pun\">-<\/span><span class=\"pln\">sr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">xr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">x  <\/span><span class=\"lit\">1<\/span><span class=\"pln\">   root   bin  <\/span><span class=\"lit\">19031<\/span> <span class=\"typ\">Feb<\/span> <span class=\"lit\">7<\/span> <span class=\"lit\">13<\/span><span class=\"pun\">:<\/span><span class=\"lit\">47<\/span>  <span class=\"pun\">\/<\/span><span class=\"pln\">usr<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">bin<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">passwd<\/span><span class=\"pun\">*<\/span><span class=\"pln\">\n$<\/span><\/pre>\n<p>Which shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.<\/p>\n<p>If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users \u2212<\/p>\n<ul class=\"list\">\n<li>The owner of the sticky directory<\/li>\n<li>The owner of the file being removed<\/li>\n<li>The super user, root<\/li>\n<\/ul>\n<p>To set the SUID and SGID bits for any directory try the following \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">$ chmod ug<\/span><span class=\"pun\">+<\/span><span class=\"pln\">s dirname\n$ ls <\/span><span class=\"pun\">-<\/span><span class=\"pln\">l\ndrwsr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">sr<\/span><span class=\"pun\">-<\/span><span class=\"pln\">x <\/span><span class=\"lit\">2<\/span><span class=\"pln\"> root root  <\/span><span class=\"lit\">4096<\/span> <span class=\"typ\">Jun<\/span> <span class=\"lit\">19<\/span> <span class=\"lit\">06<\/span><span class=\"pun\">:<\/span><span class=\"lit\">45<\/span><span class=\"pln\"> dirname\n$<\/span><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes \u2212&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-874","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=874"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/874\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}