Home  >  Article  >  Computer Tutorials  >  File permissions in Linux: Have you mastered the three types of read, write and execute?

File permissions in Linux: Have you mastered the three types of read, write and execute?

WBOY
WBOYforward
2024-02-19 21:39:27640browse

File permissions in Linux: Have you mastered the three types of read, write and execute?

In the Linux system, file permissions play a key role, determining the user's access permissions and executable operations on files. This article will delve into the concept of Linux file permissions, different types of permissions, how to modify permissions, and frequently asked questions. It is suitable for readers with a certain Linux foundation.

The concept of file permissions

In Linux systems, each file has an owner and a group. File permissions are used to control user access rights to files, and are divided into three types: read, write, and execute. You can view the permission information of a file by using the command "ls -l".

$ ls -l file.txt
-rw-r--r-- 1 user user 0 May 16 2023 file.txt

In this example, we use the ls -l command to list the permissions and other information of the file.txt file. The first character - indicates that this is a normal file. The next 9 characters can be divided into 3 groups of 3 characters each. The first group rw- indicates that the owner has read and write permissions, the second group r– indicates that group users only have read permissions, and the third group r– indicates that other users also only have read permissions.

Permission type

Read permission

Read permissions allow the user to view the file contents, ensuring the user has the appropriate permissions to access the file.

$ chmod +r file.txt # 允许所有用户读取文件
$ chmod u-r file.txt# 取消所有者的读取权限
$ chmod g+r file.txt# 允许组用户读取文件

In this example, we use the chmod command to set the read permission of the file. r means to add read permission, u-r means to cancel the owner's read permission, and gr means to allow group users to read the file.

Write Permission

Write permission allows the user to edit the file contents. No write permission will restrict modifications. The following example shows setting file write permissions.

$ chmod +w file.txt # 允许所有用户写入文件
$ chmod u-w file.txt# 取消所有者的写入权限
$ chmod g+w file.txt# 允许组用户写入文件

In this example, we use the chmod command to set the write permissions of the file. w means to add write permissions, u-w means to remove the owner's write permissions, and g w means to allow group users to write files.

Execution permission

Execute permissions allow users to run specific files in the system. Not having execute permission prevents the user from running the file.

$ chmod +x file.txt # 允许所有用户运行文件
$ chmod u-x file.txt# 取消所有者的执行权限
$ chmod g+x file.txt# 允许组用户运行文件

In this example, we use the chmod command to set the execution permissions of the file. x means to add execution permissions, u-x means to remove the owner's execution permissions, and g x means to allow group users to run the file.

Permission modification

In Linux, we can use the chmod command to modify the permissions of files. Here is an example showing how to set permissions on a file:

$ chmod u+rwx file.txt# 允许所有者读写执行文件
$ chmod g+rw file.txt # 允许组用户读写文件
$ chmod o-rwx file.txt# 禁止其他用户读写执行文件

In this example, we use the chmod command to set the permissions of the file. u rwx indicates that the owner is allowed to read and write the executable file, g rw indicates that group users are allowed to read and write the file, and o-rwx indicates that other users are prohibited from reading and writing the executable file.

common problem

How to check the permissions of the current user?

To view the permissions of the current user, run the following command:

$ id

This command will display the UID and GID of the current user, as well as the group to which it belongs.

How to change the owner and group of a file?

To change the owner and group of a file, run the following command:

$ chown new_owner file.txt
$ chgrp new_group file.txt

These commands will change the file's owner and group respectively.

How to check the owner and group of a file?

To view the owner and group of a file, run the following command:

$ ls -l file.txt

In the output, column 3 is the owner of the file and column 4 is the group of the file.

How to set default permissions?

To set the default permissions of a file, use the umask command. This command allows you to set a default permission mask to use when creating new files. Here is an example:

$ umask 022# 设置默认权限掩码为022

In this example, we set the default permission mask to 022. This means that the permissions of the new file will be rw-r–r–.

in conclusion

In Linux, file permissions are one of the very important concepts. It determines which users can access the file and what operations can be performed. This article describes permission types, permission modifications, and common issues. We hope readers can master the concept and usage of file permissions in Linux.

The above is the detailed content of File permissions in Linux: Have you mastered the three types of read, write and execute?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete