Home  >  Article  >  Operation and Maintenance  >  Can linux change read and write permissions?

Can linux change read and write permissions?

青灯夜游
青灯夜游Original
2022-04-15 15:44:389861browse

Linux can change read and write permissions. In Linux, you can use the chmod command to modify read and write permissions. This command can control user permissions on files. You can use octal number mode or symbolic mode to set read and write permissions. The syntax is "chmod [-cfvR] [--help] mode file...".

Can linux change read and write permissions?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Linux can change the read and write permissions.

In Linux, you can use the chmod command to modify read and write permissions.

Linux chmod (English full spelling: change mode) command is a command that controls user permissions on files

Linux/Unix file calling permissions are divided into three levels: File Owner (Owner) , user group (Group), other users (Other Users).

Can linux change read and write permissions?

Only the file owner and superuser can modify the permissions of a file or directory. You can use absolute mode (octal number mode) and symbolic mode to specify file permissions.

Can linux change read and write permissions?

Usage permissions: all users

Syntax

chmod [-cfvR] [--help] [--version] mode file...

Parameter description

mode: permission setting string , the format is as follows:

[ugoa...][[+-=][rwxX]...][,...]

where:

  • u represents the owner of the file, g represents the person who belongs to the same group as the owner of the file, o means someone other than someone else, and a means all three.

  • means adding permissions, - means canceling permissions, = means setting only permissions.

  • r means readable, w means writable, x means executable, and X means only when the file is a subdirectory or the file has been set to be executable.

Option description:

  • -c: If the file permissions have indeed been changed, the change action will be displayed

  • -f: Do not display an error message if the file permissions cannot be changed

  • -v: Display details of the permission changes

  • -R: Make the same permission changes to all files and subdirectories in the current directory (that is, change them one by one recursively)

  • --help: Display auxiliary instructions

  • --version: Display version

Symbol mode

Use Symbolic mode can set multiple items: who (user type), operator (operator) and permission (permission). The settings of each item can be separated by commas. The command chmod will modify the file access permissions of the user type specified by who. The user type is described by one or more letters in the position of who, as shown in the symbol pattern table of who:

who User Type Description
u user File owner
g group File owner’s group
o others All other users
a The user used by all is equivalent to the symbol pattern table of ugo
##operator:

OperatorDescriptionAdd permissions for the specified user typeRemove the permissions of the specified user type Set the settings for the specified user permissions, that is, reset all permissions of the user type
-
=
permission's symbolic pattern table:

PatternNameDescriptionread is set to Read permissionWriteSet to write permissionExecution permissionSet to executable permissionSpecial execution permissionsOnly when the file is a directory file, or other types of users have executable permissions, the file permissions are set to executablesetuid/gidWhen the file is executed, set the setuid or setgid permissions of the file according to the user type specified by the who parameterPaste bitSet the paste bit. Only the super user can set this bit, and only the file owner u can use this bit

示例:

如果我们要设定 .bashrc 文件的权限为 rwxr-xr-x,则可执行如下命令:

[root@localhost ~]# chmod u=rwx,go=rx .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxr-xr-x. 1 root root 176 Sep 22 2004 .bashrc

如果想要增加 .bashrc 文件的每种用户都可做写操作的权限,可以使用如下命令:

[root@localhost ~]# ls -al .bashrc
-rwxr-xr-x. 1 root root 176 Sep 22 2004 .bashrc
[root@localhost ~]# chmod a+w .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx. 1 root root 176 Sep 22 2004 .bashrc

八进制语法

chmod命令可以使用八进制数来指定权限。文件或目录的权限位是由9个权限位来控制,每三位为一组,它们分别是文件所有者(User)的读、写、执行,用户组(Group)的读、写、执行以及其它用户(Other)的读、写、执行。历史上,文件权限被放在一个比特掩码中,掩码中指定的比特位设为1,用来说明一个类具有相应的优先级。

r
w
x
X
s
t
# 权限 rwx 二进制
7 读 + 写 + 执行 rwx 111
6 读 + 写 rw- 110
5 读 + 执行 r-x 101
4 只读 r-- 100
3 写 + 执行 -wx 011
2 只写 -w- 010
1 只执行 --x 001
0 --- 000

例如, 765 将这样解释:

  • 所有者的权限用数字表达:属主的那三个权限位的数字加起来的总和。如 rwx ,也就是 4+2+1 ,应该是 7。

  • 用户组的权限用数字表达:属组的那个权限位数字的相加的总和。如 rw- ,也就是 4+2+0 ,应该是 6。

  • 其它用户的权限数字表达:其它用户权限位的数字相加的总和。如 r-x ,也就是 4+0+1 ,应该是 5。

示例:

使用如下命令,即可完成对 .bashrc 目录文件的权限修改:

[root@localhost ~]# ls -al .bashrc
-rw-r--r--. 1 root root 176 Sep 22 2004 .bashrc
[root@localhost ~]# chmod 777 .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx. 1 root root 176 Sep 22 2004 .bashrc

再举个例子,通常我们以 Vim 编辑 Shell 文件批处理文件后,文件权限通常是 rw-rw-r--(644),那么,如果要将该文件变成可执行文件,并且不让其他人修改此文件,则只需将此文件的权限该为 rwxr-xr-x(755)即可。

相关推荐:《Linux视频教程

The above is the detailed content of Can linux change read and write permissions?. 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