Chmod, chown, and chgrp in Linux

Source: Internet
Author: User

You can master the following commands for Linux to master various permissions in the operating system.

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 file1.txt to readable:
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 as the owner, but other
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
  • 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.