file permission command

The Linux system is a typical multi-user system, and different users are in different positions and have different permissions. In order to protect the security of the system, the Linux system has different regulations on the permissions of different users to access the same file (including directory files). In Linux we can use ls -lcommands to display the attributes of a file and the user and group to which the file belongs.

insert image description here

If you view a directory, the number of links refers to the number of subdirectories of the directory;
if you view a file, the number of links refers to the number of hard links.

The file type and permission consist of 10 characters, and each character has its own meaning, as shown in the following figure:

insert image description here

The 1st bit indicates the file type, the 2-4th bit indicates the authority of the owner (the owner of the file) on the file, and the 5th-7th bit indicates the ownership of the file by the owner group (users in the same group as the owner) Permissions, digits 8-10 represent the permissions that other users have on the file. If - appears in the corresponding permission bit, it means that there is no such permission.

Different interpretations of rwx acting on directories and files:

  • Effect on the directory:
    r means readable, the directory can be read and viewed;
    w means writable, the directory can be modified, including creating, deleting, renaming, etc. in the directory;
    x means executable, you can enter the directory .
  • Effect on files:
    r means readable, and the file can be read and viewed;
    w means writable, the file can be modified, but not necessarily deleted. The premise of deleting a file is that the directory where the file is located has write permission ;
    x means executable and can be executed by the system.
# chmod命令有两种方式改变文件或目录的权限
# 第一种方式:chmod [{ugoa}{+-}{rwx}] 文件或目录
# u:属主,g:属组,o:其他用户,a:所有用户(u、g、o的总和)
chmod u+x abc.txt  # 修改abc.txt文件的权限,使得该文件的属主拥有执行权限

chmod g+x abc.txt  # 修改abc.txt文件的权限,使得该文件的属组拥有执行权限,即组内其他用户可以执行该文件

chmod u-x, o+x abc.txt  # 修改abc.txt文件的权限,使得该文件的属主不再拥有执行权限,并使得其他用户拥有执行权限


# 第二种方式:chmod [mode=421] 文件或目录
# r=4,w=2,x=1,rwx=4+2+1=7
chmod 777 abc.txt  # 修改abc.txt文件的权限,使得该文件的属主、属组和其他用户都具有读、写、执行的权限

chmod -R 777 siam  # 递归修改siam目录的权限,使得整个目录里面的所有文件的属主、属组和其他用户都具有读、写、执行的权限
# chown命令改变文件或目录的属主
# chown [选项] [最终用户] 文件或目录
chown aibc abc.txt  # 将abc.txt文件的属主修改成aibc

chown -R aibc:aibc siam/  # 递归修改siam目录中所有文件的属主和属组
# chgrp命令改变文件或目录的属组
# chgrp [最终用户组] 文件或目录
chgrp root /etc/passwd  # 将/etc/passwd目录的属组修改成root

Guess you like

Origin blog.csdn.net/weixin_48158964/article/details/132207653