Linux chmod and chown commands

Source: Internet
Author: User
Command name: chmod permission: Usage of all users: chmod [-cfvR] [-- help] [-- version] modefile... note: file access permissions for Linux/Unix are classified into three levels: file owner, group, and others. The chmod can be used to control the file, such as the command name chmod.
 
Permission: all users
Usage: chmod [-cfvR] [-- help] [-- version] mode file...
Note: file access permissions for Linux/Unix are classified into three levels: file owner, group, and others. Chmod can be used
To control how an archive is accessed by others.
Mode: permission setting string in the following format: [ugoa...] [[+-=] [rwxX]...] [,...], u indicates the ownership of the file.
G indicates that the owner of the archive belongs to the same group, o indicates that the owner is not the owner, and a indicates that all three are
Yes.
+ Adds a permission,-indicates canceling the permission, and = indicates a unique permission.
R indicates that the file can be read, w indicates that the file can be written, x indicates that the file can be executed, and X indicates that only when the file is a subdirectory or the file has been
Set to executable.
-C: if the permission of the file has been changed, the change action is displayed.
-F: do not display an error message if the file permission cannot be changed.
-V: displays details of permission changes.
-R: change the permissions of all files and sub-directories in the current directory in the same way (that is, change one by one in the way of delivery)
-- Help: displays auxiliary instructions
-- Version: displays the version.
Example: set the file file1.txt to readable by all users:
Chmod ugo + r file1.txt
Set the file file1.txt to readable by all users:
Chmod a + r file1.txt
Set file1.txt and file2.txt as the owner of the archive, which can be written to the same group to which the archive belongs.
Users cannot write:
Chmod ug + w, o-w file1.txt file2.txt
Set ex1.py to only the owner of the file to execute:
Chmod u + x ex1.py
Set all files and subdirectories in the current directory to be readable by anyone:
Chmod-R a + r *
In addition, chmod can use numbers to indicate permissions, such as chmod 777 file.
Syntax: chmod abc file
Each a, B, and c is a number, indicating the permissions of the User, Group, and Other respectively.
R = 4, w = 2, x = 1
If you want the rwx attribute, 4 + 2 + 1 = 7;
If the rw-attribute is required, 4 + 2 = 6;
If you want the r-x attribute, 4 + 1 = 7.
Example:
Chmod a = rwx file
And
Chmod 777 file
Same effect
Chmod ug = rwx, o = x file
And
Chmod 771 file
Same effect
If you use chmod 4755 filename, you can grant root permissions to this program.
Command name: chown
Permission: root
Usage: chown [-cfhvR] [-- help] [-- version] user [: group] file...
Note: Linux/Unix is a multi-person, multi-job operating system. all archives have owners. Chown can be used
The token is changed. Generally, this command is only used by the system administrator (root). Generally, users do not have the permission
Changing another person's archive owner does not have the permission to change his/her archive owner to another person. Only system administrators (root)
Have such permissions.
User: user IDgroup of the new owner: user group of the new owner (group)-c: If
The owner has indeed changed the file to display its change action-f: If the file owner cannot be changed, do not display the error message-h:
Only the link is changed, not the file that the link actually points to-v: displays the details of the owner's change-R:
Change the owner of all files and sub-directories in the former Directory (that is, change one by one in the way of delivery) -- help: Display assistance
Description -- version: displays the version.
Example:
Set the owner of the file file1.txt to user jessie of the users group:
Chown jessie: users file1.txt
Set all files in the current directory and sub-directory owner to user lamport of users Group:
Chown-R lamport: users *
-Rw ------- (600) -- only the owner has read and write permissions.
-Rw-r -- (644) -- only the owner has read and write permissions, while the owner and other users have only read permissions.
-Rwx ------ (700) -- only the owner has read, write, and execution permissions.
-Rwxr-xr-x (755)-The owner has the read, write, and execution permissions, while the owner and other users have only the read and execution permissions.
-Rwx -- x (711) -- The owner has read, write, and execution permissions, while the owner and other users have only execution permissions.
-Rw-(666) -- All users have the permission to read and write files. This approach is not advisable.
-Rwxrwxrwx (777)-all users have read, write, and execute permissions. It is not advisable.
The following are two common settings for the directory:
Drwx ------ (700)-only the owner can read and write data in the directory.
Drwxr-xr-x (755)-all users can read this directory, but only the owner can change the contents in the directory.
Suid indicates that the number is 4. for example, the result of 4755 is-rwsr-xr-x.
Sgid represents 2. for example, the result of 6755 is-rwsr-sr-x.
The sticky digit indicates that the number is 1. for example, the result of 7755 is-rwsr-sr-t.
(Of course, the chmod setting of 7755 does not make much sense. here is just a demonstration)
 
Chown command
Purpose
Change the owner or group associated with the file.
Syntax
Chown [
-F
] [
-H
] [
-R
] Owner [: Group] {File... | Directory ...}
Chown
-R
[
-F
] [
-H
|
-L
|
-P
] Owner [: Group] {File... | Directory ...}
Description
The chown command changes the Owner of the File specified by the File parameter to the user specified by the Owner parameter. The Owner parameter value can be a user ID or login name that can be found in the/etc/passwd file. You can also select a group. The value of the Group parameter can be a group ID or Group name that can be found in the/etc/group file.
Only root users can change the file owner. You can change the file group only when you are a root user or have the file. If you have a file but are not a root user, you can only change the group to a group where you are a member.
Although the-H,-L, and-P flags are mutually exclusive, specifying more than one mark is not considered as an error. The last flag specified determines the operations that the command draft will demonstrate.
Flag
-F
Disable all error messages except message usage.
-H
Change the ownership of the symbolic link, not the ownership of the file or directory to which the symbolic link points.
When you encounter a symbolic link without specifying the-h flag, the chown command changes the ownership of the file or directory to which the link points, rather than the ownership of the link itself.
If the-R flag is specified, the chown command recursively descending the specified directory.
-H
If the-R option is specified and the symbolic link of the file that references the type directory is specified on the command line, the chown variable changes the user ID (and group ID) of the directory referenced by the symbol, if specified) and all files in the file hierarchy under the Directory.
-L
If the-R option is specified and the file symbol that references the type directory is specified on the command line or encountered during file hierarchy traversal, the chown command changes the user ID (and group ID, if specified) of the directory referenced by the symbolic link and all files in the file hierarchy under the Directory.
-P
If the-R option is specified and the symbolic link is specified on the command line or is encountered during file hierarchy traversal, if the system supports this operation, the chown command changes the owner ID (and group ID, if specified) of the symbolic link ). The chown command does not execute symbolic links to any other part of the file hierarchy.
-R
Recursively descending directories to change the ownership of each file. When a symbolic link points to a directory, the ownership of the directory is changed, but the directory is not further traversed. However, The-h,-H,-L or-P flag is not specified, when a symbolic link is encountered and the link points to the Directory, the group ownership of the directory is changed but the directory is not further traversed.
Security
Access control: This program should be installed as a normal user program in the "trusted computing base.
Exit status
This command returns the following export value:
0
The command is successfully executed and all request changes have been executed.
> 0
An error occurred.
Example
 
To change the owner of the file program. c:
Chown jim program. c
The user access permission of program. c is now applied to jim. As the owner, jim can use
Chmod
Command to allow or deny other users access to program. c.
 
To change the owner and group of all files in the directory/tmp/src to user john and group build:
Chown-R john: build/tmp/src
File
/Usr/bin/chown
Chown command
/Etc/group
Files containing group IDs
/Etc/passwd
Files containing user IDs
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.