linux:file-permissions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
linux:file-permissions [2020/03/05 15:42] – [Table] michaellinux:file-permissions [2020/03/05 15:57] (current) michael
Line 86: Line 86:
 ===== Using chmod ===== ===== Using chmod =====
  
-The chmod command takes the following general form: +The ''chmod'' command takes the following general form:
-chmod [OPTIONS] MODE FILE... +
-The chmod command allows you to change the permissions on a file using either a symbolic or numeric mode or a reference file. We will explain the modes in more detail later in this article. The command can accept one or more files and/or directories separated by space as arguments.+
  
-Only root, the file owner or user with sudo privileges can change the permissions of a file. Be extra careful when using chmod, especially when recursively changing the permissions.+<code>chmod [OPTIONS] MODE FILE...</code>
  
-==== Symbolic (Text) Method ====+The ''chmod'' command allows you to change the permissions on a file using either a symbolic or numeric mode or a reference file. We will explain the modes in more detail later in this article. The command can accept one or more files and/or directories separated by space as arguments.
  
-The syntax of the chmod command when using the symbolic mode has the following format: +Only root, the file owner or user with sudo privileges can change the permissions of a file. Be extra careful when using ''chmod''especially when recursively changing the permissions.
-chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE... +
-The first set of flags ([ugoa…]), users flags, defines which users classes the permissions to the file are changed.+
  
-  * u - The file owner. 
-  * g - The users who are members of the group. 
-  * o - All other users. 
-  * a - All users, identical to ugo. 
  
-If the users flag is omitted, the default one is a and the permissions that are set by umask are not affected. +==== Symbolic (TextMethod ====
-The second set of flags ([-+=]), the operation flags, defines whether the permissions are to be removed, added, or set:+
  
-  * - Removes the specified permissions. +The syntax of the ''chmod'' command when using the symbolic mode has the following format:
-  * + Adds specified permissions. +
-  * = Changes the current permissions to the specified permissions. If no permissions are specified after the = symbol, all permissions from the specified user class are removed.+
  
-The permissions (perms...) can be explicitly set using either zero or one or more of the following letters: r, w, x, X, s, and t. Use a single letter from the set u, g, and o when copying permissions from one to another users class.+<code>chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE...</code>
  
-When setting permissions for more than one user classes ([,…]), use commas (without spaces) to separate the symbolic modes.+The first set of flags (''[ugoa…]''), users flags, defines which users classes the permissions to the file are changed.
  
-Below are some examples of how to use the chmod command in symbolic mode:+  * ''u'' - The file owner. 
 +  * ''g'' - The users who are members of the group. 
 +  * ''o'' - All other users. 
 +  * ''a'' - All users, identical to ''ugo''.
  
-Give the members of the group permission to read the file, but not to write and execute it:+If the users flag is omitted, the default one is a and the permissions that are set by umask are not affected. 
 +The second set of flags (''[-+=]''), the operation flags, defines whether the permissions are to be removed, added, or set:
  
-chmod g=r filename +  * ''-'' Removes the specified permissions. 
-Remove the execute permission for all users:+  * ''+'' Adds specified permissions. 
 +  * ''='' Changes the current permissions to the specified permissions. If no permissions are specified after the = symbol, all permissions from the specified user class are removed.
  
-chmod a-filename +The permissions (''perms...'') can be explicitly set using either zero or one or more of the following letters: ''r'', ''w'', ''x'', ''X'', ''s'', and t. Use a single letter from the set ''u'', ''g'', and ''o'' when copying permissions from one to another users class.
-Repulsively remove the write permission for other users:+
  
-chmod -R o-w dirname +When setting permissions for more than one user classes (''[,…]'')use commas (without spaces) to separate the symbolic modes.
-Remove the readwriteand execute permission for all users except the file’s owner:+
  
-chmod og-rwx filename +Below are some examples of how to use the ''chmod'' command in symbolic mode:
-The same thing can be also accomplished by using the following form:+
  
-chmod og= filename +  * Give the members of the group permission to read the file, but not to write and execute it: <code>chmod g=r filename</code> 
-Give read, write and execute permission to the file’s owner, read permissions to the file’s group and no permissions to all other users:+  * Remove the execute permission for all users: <code>chmod a-x filename</code> 
 +  * Repulsively remove the write permission for other users: <code>chmod -R o-w dirname</code> 
 +  * Remove the read, write, and execute permission for all users except the file’s owner: <code>chmod og-rwx filename</code> 
 +  * The same thing can be also accomplished by using the following form: <code>chmod og= filename</code> 
 +  Give read, write and execute permission to the file’s owner, read permissions to the file’s group and no permissions to all other users: <code>chmod u=rwx,g=r,o= filename</code> 
 +  * Add the file’s owner permissions to the permissions that the members of the file’s group have:<code>chmod g+u filename</code> 
 +  * Add a sticky bit to a given directory: <code>chmod o+t dirname</code>
  
-chmod u=rwx,g=r,ofilename +<WRAP center box 100%> 
-Add the file’s owner permissions to the permissions that the members of the file’s group have:+==== Numeric Method ====
  
-chmod g+u filename +The syntax of the chmod command when using numeric method has the following format:
-Add a sticky bit to a given directory:+
  
-chmod o+t dirname+<code>chmod [OPTIONS] NUMBER FILE...</code>
  
-==== Numeric Method ==== 
- 
-The syntax of the chmod command when using numeric method has the following format: 
-chmod [OPTIONS] NUMBER FILE... 
 When using the numeric mode, you can set the permissions for all three user classes (owner, group, and all others) at the same time. When using the numeric mode, you can set the permissions for all three user classes (owner, group, and all others) at the same time.
  
-The NUMBER can be a 3 or 4-digits number.+The ''NUMBER'' can be a 3 or 4-digits number.
  
 When 3 digits number is used the first digit represents the permissions of the file’s owner, the second one of the file’s group and the last one all other users. When 3 digits number is used the first digit represents the permissions of the file’s owner, the second one of the file’s group and the last one all other users.
Line 153: Line 145:
 Each write, read, and execute permissions have the following number value: Each write, read, and execute permissions have the following number value:
  
-  * r (read) = 4 +  * ''r'' (read) = ''4'' 
-  * w (write) = 2 +  * ''w'' (write) = ''2'' 
-  * x (execute) = 1 +  * ''x'' (execute) = ''1'' 
-  * no permissions = 0+  * no permissions = ''0''
  
 The permissions number of a specific user class is represented by the sum of the values of the permissions for that group. The permissions number of a specific user class is represented by the sum of the values of the permissions for that group.
Line 165: Line 157:
   * Others: r-x=4+0+0=4   * Others: r-x=4+0+0=4
  
-Using the method above we come up to the number 754, which represents the desired permissions.+Using the method above we come up to the number ''754'', which represents the desired permissions.
  
-To set up the setuid, setgid, and sticky bit flags use four digits number.+To set up the ''setuid''''setgid'', and ''sticky'' bit flags use four digits number.
  
 When the 4 digits number is used, the first digit has the following meaning: When the 4 digits number is used, the first digit has the following meaning:
Line 178: Line 170:
 The next three digits have the same meaning as when using 3 digits number. The next three digits have the same meaning as when using 3 digits number.
  
-If the first digit is 0 it can be omitted, and the mode can be represented with 3 digits. The numeric mode 0755 is the same as 755.+If the first digit is 0 it can be omitted, and the mode can be represented with 3 digits. The numeric mode ''0755'' is the same as ''755''.
  
 To calculate the numeric mode you can also use another method (binary method), but it is a little more complicated. Knowing how to calculate the numeric mode using 4, 2, and 1 is sufficient for most users. To calculate the numeric mode you can also use another method (binary method), but it is a little more complicated. Knowing how to calculate the numeric mode using 4, 2, and 1 is sufficient for most users.
Line 184: Line 176:
 You can check the file’s permissions in the numeric notation using the stat command: You can check the file’s permissions in the numeric notation using the stat command:
  
-stat -c "%a" filename+<code>stat -c "%a" filename</code> 
 + 
 +<sxh bash; gutter false;>
 644 644
-Here are some examples of how to use the chmod command in numeric mode: +</sxh>
- +
-Give the file’s owner read and write permissions and only read permissions to group members and all other users: +
- +
-chmod 644 dirname +
-Give the file’s owner read, write and execute permissions, read and execute permissions to group members and no permissions to all other users:+
  
-chmod 750 dirname +Here are some examples of how to use the ''chmod'' command in numeric mode:
-Give read, write, and execute permissions, and a sticky bit to a given directory:+
  
-chmod 1777 dirname +  * Give the file’s owner read and write permissions and only read permissions to group members and all other users: <code>chmod 644 dirname</code> 
-Recursively set read, write, and execute permissions to the file owner and no permissions for all other users on a given directory:+  * Give the file’s owner read, write and execute permissions, read and execute permissions to group members and no permissions to all other users:<code>chmod 750 dirname</code> 
 +  * Give read, write, and execute permissions, and a sticky bit to a given directory:<code>chmod 1777 dirname</code> 
 +  Recursively set read, write, and execute permissions to the file owner and no permissions for all other users on a given directory:<code>chmod -R 700 dirname</code> 
 +</WRAP>
  
-chmod -R 700 dirname 
  
 +<WRAP center box 100%>
 ==== Using a Reference File ==== ==== Using a Reference File ====
  
 The --reference=ref_file option allows you to set the file’s permissions to be same as those of the specified reference file (ref_file). The --reference=ref_file option allows you to set the file’s permissions to be same as those of the specified reference file (ref_file).
  
-chmod --reference=REF_FILE FILE +<code>chmod --reference=REF_FILE FILE</code>
-For example, the following command will assign the permissions of the file1 to file2+
  
-chmod --reference=file1 file2+For example, the following command will assign the permissions of the file1 to file2
  
 +<code>chmod --reference=file1 file2</code>
 +</WRAP>
 +
 +
 +<WRAP center box 100%>
 ==== Recursively Change the File’s Permissions ==== ==== Recursively Change the File’s Permissions ====
  
 To recursively operate on all files and directories under the given directory, use the -R (--recursive) option: To recursively operate on all files and directories under the given directory, use the -R (--recursive) option:
  
-chmod -R MODE DIRECTORY+<code>chmod -R MODE DIRECTORY</code> 
 For example, to change the permissions of all files and subdirectories under the /var/www directory to 755 you would use: For example, to change the permissions of all files and subdirectories under the /var/www directory to 755 you would use:
  
-chmod -R 755 /var/www+<code>chmod -R 755 /var/www</code> 
 +</WRAP> 
  
 +<WRAP center box 100%>
 ==== Operating on Symbolic Links ==== ==== Operating on Symbolic Links ====
  
Line 225: Line 224:
 By default, when changing symlink’s permissions, chmod will change the permissions on the file the link is pointing to. By default, when changing symlink’s permissions, chmod will change the permissions on the file the link is pointing to.
  
-chmod 755 symlink+<code>chmod 755 symlink</code>
 Chances are that instead of changing the target ownership, you will get a “cannot access ‘symlink’: Permission denied” error. Chances are that instead of changing the target ownership, you will get a “cannot access ‘symlink’: Permission denied” error.
  
-The error occurs because by default on most Linux distributions symlinks are protected, and you cannot operate on target files. This option is specified in /proc/sys/fs/protected_symlinks. 1 means enabled and 0 disabled. It is recommended not to disable the symlink protection.+The error occurs because by default on most Linux distributions symlinks are protected, and you cannot operate on target files. This option is specified in ''/proc/sys/fs/protected_symlinks''''1'' means enabled and ''0'' disabled. It is recommended not to disable the symlink protection. 
 +</WRAP>
  
 +<WRAP center box 100%>
 ==== Changing File Permissions in Bulk ==== ==== Changing File Permissions in Bulk ====
  
 Sometimes there are situations where you would need to bulk change files and directories permissions. Sometimes there are situations where you would need to bulk change files and directories permissions.
  
-The most common scenario is to recursively change the website file’s permissions to 644 and directory’s permissions to 755.+The most common scenario is to recursively change the website file’s permissions to 644 and directory’s permissions to ''755''.
  
 Using the numeric method: Using the numeric method:
Line 247: Line 248:
  
 The find command will search for files and directories under /var/www/my_website and pass each found file and directory to the chmod command to set the permissions. The find command will search for files and directories under /var/www/my_website and pass each found file and directory to the chmod command to set the permissions.
 +</WRAP>
 +
  • linux/file-permissions.1583419322.txt.gz
  • Last modified: 2020/03/05 15:42
  • by michael