Home  >  Article  >  Operation and Maintenance  >  Linux command to change access permissions of files or directories

Linux command to change access permissions of files or directories

巴扎黑
巴扎黑Original
2017-08-10 11:35:012293browse

[Introduction] Every file and directory in the Linux system has access permissions, which are used to determine who can access and operate the files and directories in what way. There are three types of access permissions for files or directories: read-only, write-only and executable. Taking a file as an example, read-only permission means that only its contents are allowed to be read, and access permissions are prohibited for every file and directory in the

Linux system. Use it to determine who can access the file and how to access it. Files and directories can be accessed and manipulated.

The access permissions of files or directories are divided into three types: read-only, write-only and executable. Taking a file as an example, read-only permission means that only its contents are allowed to be read, but no changes are allowed. Executable permissions allow the file to be executed as a program. When a file is created, the file owner automatically has read, write, and executable permissions on the file to facilitate reading and modifying the file. Users can also set access permissions to any combination required.

There are three different types of users who can access files or directories: file owners, users in the same group, and other users. The owner is usually the creator of the file. The owner can allow the same group of users to have access to the file, and can also grant access to the file to other users in the system. In this case, every user in the system can access files or directories owned by that user.

Each file or directory has three groups of access permissions, each group is represented by three digits, which are the read, write and execute permissions of the file owner; the read, write and execution permissions of users in the same group as the owner. Execute permissions; read, write, and execute permissions for other users in the system. When using the ls -l command to display detailed information about a file or directory, the leftmost column lists the file access permissions. For example:

$ ls -l sobsrc. tgz-rw-r--r-- 1 root root 483997 Ju1 l5 17:3l sobsrc. tgz

Horizontal lines represent empty permissions. r stands for read-only, w stands for write, and x stands for executable. Note that there are 10 locations in total. The first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a dash, it means it is a non-directory file. If it is d, it means a directory.

For example:

- rw- r-- r--

Normal file file primary group user other users

is the access permission of the file sobsrc.tgz, indicating that sobsrc.tgz is a Ordinary file; the owner of sobsrc.tgz has read and write permissions; users in the same group as the owner of sobsrc.tgz only have read permissions; other users also only have read permissions.

After determining the access permissions of a file, the user can use the chmod command provided by the Linux system 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.

These commands are introduced below.

chmod command

The chmod command is very important and is used to change the access permissions of files or directories. It is used by users to control access permissions to files or directories.

This command has two uses. One is a text setting method that includes letters and operator expressions; the other is a numeric setting method that includes numbers.

1. Text setting method

chmod [who] [+ | - | =] [mode] File name?

Each of the commands The meaning of the options is:

2. Number setting method

We must first understand the meaning of the attributes represented by numbers: 0 means no permission, 1 means executable Permissions, 2 means Shang Ci ㄏ clover? means read permissions, and then add them up. So the format of the numeric attribute should be 3 octal numbers from 0 to 7 in the order (u)(g)(o).

For example, if you want the owner of a file to have "read/write" permissions, you need to set 4 (readable) + 2 (writable) = 6 (read/write).

The general form of the number setting method is:

chmod [mode] file name?

chgrp command

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

Syntax: chgrp [options] group filename?

This command changes the user group to which the specified file belongs. Where group can be the user group ID or the group name of the user group in the /etc/group file. The file name is a space-separated list of files whose groups are to be changed, and wildcards are supported. If the user is not the owner or superuser of the file, the group of the file cannot be changed.

The meaning of each option of this command is:

- R recursively changes the ownership group of the specified directory and all subdirectories and files under it.

chown command

Function: Change the owner and group of a file or directory. This command is also very commonly used. For example, the root user copies one of his files to the user xu. In order to allow the user xu to access the file, the root user should set the owner of the file to xu. Otherwise, the user xu cannot access the file.

Syntax: chown [option] user or group file

Description: chown changes the owner of the specified file to the specified user or group. User can be a username or user ID. Group can be a group name or a group ID. File is a space-separated list of files whose permissions need to be changed. Wildcards are supported.

The meaning of each option of this command is as follows:

- R 递归式地改变指定目录及其下的所有子目录和文件的拥有者。- v 显示chown命令所做的工作。

The operation object who can be any one of the following letters or their combination:

u 表示“用户(user)”,即文件或目录的所有者。g 表示“同组(group)用户”,即与文件属主有相同组ID的所有用户。o 表示“其他(others)用户”。a 表示“所有(all)用户”。它是系统默认值。

The operation symbol can be:

+ 添加某个权限。- 取消某个权限。= 赋予给定权限并取消其他所有权限(如果有的话)。

The permission represented by setting mode can be any combination of the following letters:

r 可读。w 可写。x 可执行。X 只有目标文件对某些用户是可执行的或该目标文件是目录时才追加x 属性。s 在文件执行时把进程的属主或组ID置为该文件的文件属主。方式“u+s”设置文件的用户ID位,“g+s”设置组ID位。t 保存程序的文本到交换设备上。u 与文件属主拥有一样的权限。g 与和文件属主同组的用户拥有一样的权限。o 与其他用户拥有一样的权限。

文件名:以空格分开的要改变权限的文件列表,支持通配符。

在一个命令行中可给出多个权限方式,其间用逗号隔开。例如:chmod g+r,o+r example

使同组和其他用户对文件example 有读权限。


The above is the detailed content of Linux command to change access permissions of files or directories. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn