How to change folder permissions in Centos

Source: Internet
Author: User

How to change Linux folder Permissions

To learn more about file attributes,

In the shell environment, enter ls-l to view the current directory file. For example:

Drwxr-xr-x 2 nsf users 1024 12-10 download file backup

They correspond:

File Attribute connection count file owner group file size file modification time file name

Here, r is readable, w writable, and x executable. The file attributes are divided into four sections, ---- --- 10 positions.

For example:

D rwx r-x

The first character specifies the file type. In general, a directory is also a file. If the first character is a horizontal line, it indicates a non-directory file. If it is d, it indicates a directory.

The second part is the attributes of the file owner,

The third section is the attributes of the group to which the file belongs,

The fourth section is the attributes of other users,

For example, the "download file backup" access permission in the preceding folder indicates that the "download file backup" folder is a directory file; the owner of the "download file backup" folder has the read and write permissions; users in the same group as the folder "download file backup" have only the read and executable permissions. Other users also have the read and executable permissions.

After determining the access permission for a file, you can use the chmod command provided by Linux to reset different access permissions. You can also use the chown command to change the owner of a file or directory. Use the chgrp command to change the user group of a file or directory.

Chmod command

Function: the chmod command is very important for changing the access permissions of files or directories. You can use it to control the access permissions of files or directories.

Syntax: This command can be used in two ways. One is the text setting method that contains letters and operator expressions, and the other is the number setting method that contains numbers.

1. text setting method

Chmod [who] [+ |-| =] [mode] File Name?

Parameters:

The operator who is one of the following letters or their combination:

U indicates "user", that is, the owner of a file or directory.

G indicates "group users", that is, all users with the same group ID as the file owner.

O indicates "Other (others) Users ".

A Indicates "all (all) Users ". It is the default value of the system.

The operation symbol can be:

+ Add a permission.

-Cancel a permission.

= Grant the given permission and cancel all other permissions (if any ).

Set the permissions indicated by mode to any combination of the following letters:

R readable.

W writable.

X executable.

X adds the x attribute only when the target file is executable to some users or the target file is a directory.

S sets the owner or group ID of the process to the file owner during file execution. In the format of "u + s", set the user ID bit of the file, and "g + s" to set the group ID bit.

T save the program text to the swap device.

U has the same permissions as the file owner.

G. users in the same group have the same permissions as file owners.

O has the same permissions as other users.

File Name: list of files separated by spaces to change permissions. Wildcards are supported.

Multiple permission methods can be provided in a command line, separated by commas. Example: chmod g + r, o + r example

Grant the same group and other users the permission to read the file example.

Chgrp command

Function: Change the group to which a file or directory belongs.

Syntax: chgrp [Option] group filename?

This command changes the user group to which the specified file belongs. The group can be either the user group ID or the group name of the user group in the/etc/group file. A file name is a list of files separated by spaces to change the group. Wildcards are supported. If the user is not the owner or super user of the file, the file group cannot be changed.

Parameters:

-R recursively changes the group of all subdirectories and files in a specified directory.

Example 1: $ chgrp-R book/opt/local/book

Change the owner group of all files in/opt/local/book/and its subdirectories to book.

Chown command

Function: Modify the owner and group of a file or directory. This command is also very common. For example, if the root user copies a file to xu, the root user should set the owner of the file to xu to allow xu to access the file. Otherwise, user xu cannot access this file.

Syntax: chown [Option] user or group file

Note: chown will change the owner of a specified file to a specified user or group. Users can be user names or user IDs. A group can be a group name or group ID. Files are separated by spaces to change the permission list. Wildcards are supported.

Parameters:

-R recursively changes the owner of all subdirectories and files under a specified directory.

-V: displays the work done by the chown command.

Example 1: Change the owner of the shiyan. c file to wang.

$ Chown wang shiyan. c

Example 2: Change the owner of the directory/his and all files and subdirectories to wang and the group to users.

$ Chown-R wang. users/his

Usage: chmod [-cfvR] mode file

Note: file access permissions for Linux/Unix are classified into three levels: Owner, group, and others. Chmod can be used to control the permission to read files.

Mode: permission setting string in the following format: [ugoa...] [+-=] [rwxX], where u indicates the file owner (user) and g indicates that the file owner belongs to the same group ), o indicates others (other), and a indicates all ).

+ Adds a permission,-Indicates canceling the permission, and = indicates a unique permission.

R indicates readable, w indicates writable, and x indicates executable.

-F: if the file permission cannot be changed, do not display an error message.

-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)

Example:

Set file1.txt to readable:

Chmod ugo + 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, but not to others:

Chmod ug + w, o-w file1.txt file2.txt

Set the directory log and all files in the directory as the owner of the file to be able to write, read, and execute. The owner of the file belongs to the same group and other persons who can read and execute the file.

Chmod-R 755 log

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.