Home > System Tutorial > Linux > body text

Linux system permissions analysis: permission classification and representation methods of ordinary files and directories

WBOY
Release: 2024-06-18 15:50:44
Original
324 people have browsed it

Linux 系统权限解析:普通文件与目录的权限分类及表示方法

The Linux system has many permissions. Although the purpose is to provide user security services and also makes it more difficult to understand, here I will summarize the Linux permissions according to file types

1. Ordinary files and directories

For ordinary files and directories on the Linux system, there are three permissions: rwx permissions, hidden attribute permissions and ACL permissions

1.rwx permissions (1) Use numbers to represent rwx permissions

r represents read permission; w represents write permission; x represents execution permission.

The Linux system has three identities: owner (u), group (g) and other people (o)

The Linux system uses 0 to indicate that permissions are turned off, and 1 to indicate that permissions are turned on. So for any file, how to install LINUX, there are 8 possible combinations of owner permissions, among which

Only read permission, expressed as r--, the two's complement number is 100, converted into ten's complement number is 4;

Only write permission, expressed as -w-, the two's complement number is 010, converted into ten's complement number is 2;

Only execution permission, expressed as –x, the two’s complement number is 001, converted into ten’s complement number is 1.

From this, we get the way to represent rwx permissions using ten's complement numbers:

r is represented by 4, w is represented by 2, and x is represented by 1.

(2) The impact of rwx permissions on files and directories in the system

For files, the r permission represents readable, indicating that the file content can be viewed, which corresponds to the command, which means that commands such as cat/more/less can be executed to view the content; the w permission represents writable, indicating that it can be changed (write Enter) file Linux file is authorized to user, which corresponds to the command, which is the executable vim file editor; x represents execution permission, indicating that the file can be placed and run in the system. Generally, our various commands and Scripts must have execution permissions.

For a directory, the r permission means readable, which means that the files and directory names in the directory can be viewed (the directory can be understood as a file that records file attribute information) Linux files are authorized to users, Corresponding to the command, it means you can execute ls; w permission means writeable, which means you can change the file and directory name. Corresponding to the command, you can execute commands such as touch, mkdir, rmdir, rm; x means execution permission, which means you can run This directory corresponds to the command, that is, you can use cd to enter the directory.

2. Hidden attribute permissions

linux 文件授权给用户_linux授权文件给用户_linux授予用户文件权限

There are some hidden attributes in the Linux system to ensure file security

View the following commands available:

lsattr -a
Copy after login

-a means to view all file directories. If you only want to view the directory linux version of qq, you can add the -d option.

To add or delete hidden attribute permissions to a file/directory, use the following command:

chattr -a filename#删除权限
chattr +i filename#添加权限
Copy after login

其中i选项和a选项对文件和目录又有不同的影响:

对文件来说,假如有i隐藏属性,这么该文件就是一个只读文件,难以更改;假如有a隐藏属性,只能降低数据(echo重定向形式),不能更改数据(因而a隐藏属性适宜于日志)。

对目录来说,假如有i隐藏属性,只能更改目录下已有文件,不能完善新文件以及删掉已有文件(只读目录);假如有a隐藏属性,只能新建或更改文件,不能删掉、改名已有文件。

3.ACL权限

ACL=accesscontrollist英文为“访问控制列表”

作用:可对单一用户设置文件、目录的rwx权限。

形成缘由:出于安全考虑,linux系统下的文件、目录默认对其他人有较低权限,假如某个其他人身分想要查看他不具备权限的目录或文件,要么切换他的身分为所有者,要么让他以所有者的身分登入系统,而这两种做法都具有一定安全隐患,为此,为解决该问题,ACL权限形成。

设置ACL权限可用如下的命令格式:

setfacl -m u:user:rwx filename
Copy after login

-m选项用于设置ACL权限,user表示要修改权限的用户,rwx表示要赋于该用户的权限情况,filename表示要赋于ACL权限的文件或目录。

linux授权文件给用户_linux授予用户文件权限_linux 文件授权给用户

查看ACL权限可用如下的命令格式:

getfacl filename #在当前文件目录下
Copy after login

ACL权限优势:

我们可以用getfacl查看早已设置好ACL权限的文件或目录:

#省略输出…………
user::rwx #文件所有者权限
user:user:rwx #用户user对该文件的权限
group::rwx#文件所属组权限
other::---#文件其他人权限
省略输出…………
Copy after login

可以看见,用户user不属于所有者、所属组或其他人,而是单独为他设置了对该文件的rwx权限。这样在保证其他人难以查看、修改该文件的情况下,也可同时确保用户user更改、查看该文件,达到安全目的。

二、特殊文件——可执行文件

linux系统存在着好多可执行文件,比如各类命令、shell脚本等,这么对于个别只能由超级用户才可执行的命令怎么给普通用户执行呢?这就涉及到了SetUID和SetGID的概念。

1.SetUID权限

linux系统示例:/bin/passwd命令

可以查看一下该文件的权限情况

可以见到,在所有者执行权限的x位上弄成了s,这就表示某个可执行文件被赋于了SUID权限。

作用:普通用户也可用passwd命令更改密码(给用户某执行文件的所有者权限)。

运作过程:

普通用户使用passwduser,意图更改密码------passwd拥有SUID权限且user对passwd命令有x权限------user暂时拥有root权限,以所有者身分执行passwd-------root可把密码写入密码文件/etc/shadow-----更改完成,user身分恢复。

SetUID权限添加、删除命令:

chmod u+s filename #给文件/目录添加SUID权限
chmod u-s filename #给文件/目录删除SUID权限
Copy after login

2.SetGID权限

linux系统示例:/usr/bin/locate

可以见到,在所所属组执行权限的x位上弄成了s,这就表示某个可执行文件被赋于了SGID权限。

作用:普通用户也可用locate命令查询mlocate.db数据库(给用户某执行文件的所属组权限)。

运作过程:

普通用户使用locate,意图查询系统上的某文件------locate拥有SGID权限且user对locate命令有x权限------user暂时拥有slocate组权限,以所属组身分执行locate-------该组对mlocate.db数据库有r权限-----user可以查询------查询结束,user组身分恢复。

SetGID权限添加、删除命令:

chmod g+s filename #给文件/目录添加SGID权限
chmod g-s filename #给文件/目录删除SGID权限
Copy after login

3.StickyBIT权限

linux系统示例:/tmp

可以看见,该目录的其他人执行权限上的x位弄成了t,这就表示该目录具有BIT权限。

BIT权限特征:仅对目录有效;仅其他人可用。

linux 文件授权给用户_linux授予用户文件权限_linux授权文件给用户

作用:设置了BIT权限的目录,任何人都可以在该目录下创建、修改文件,但只有该文件的所有者或root用户才可删掉文件。(谁创建,谁删掉)

权限添加、删除命令:

chmod o+t filename #给文件/目录添加BIT权限
chmod o-t filename #给文件/目录删除BIT权限
Copy after login

4.umask权限

在命令行执行umask命令时,会听到4位权限数:

其中后三位就表示一个文件/目录的rwx默认权限,它限制了新创建的文件/目录的初始权限(文件644,目录755)

这儿还存在第一位上的0,这一点结合上述执行文件三种权限即可解释:

SetUID权限:以s表示,对应所有者,用十补码4表示(100);

SetGID权限:以s表示,对应所属组,用十补码2表示(010);

BIT权限:以t表示,对应其他人,用十补码1表示(001)。

这样,任何一个文件/目录的权限可表示如下:

rwxsrwxsrwxt

ugo

三、系统命令

尽管超级用户具备linux所有命令的执行权限,但一个多用户系统假如全都由root用户管理是不现实的,正常的管理方式是root用户把一些系统命令授权给普通用户,让其协助管理,这才符合多用户系统的管理思路。

操作对象:系统命令(与上述权限不同)

工具:

1.su:切换用户身分,赋于某用户所有系统命令权限。(有安全隐患)

在这里插入图片描述

使用方式:su-用户名(不加为root)

弊病:非root用户切换他人须要密码。

2.sudo:限制用户使用系统命令

授权形式:

vi /etc/sudoers
visudo #两种方式选一个即可
Copy after login

更改模板:

root ALL=(ALL)ALL
#用户主机名身份执行的命令 
Copy after login

赋于过程(以user为例):

如果root用户想要赋于普通用户user重启服务器权限

vi /etc/sudoers #在root下加一行即可
rootALL=(ALL) ALL
userALL=usr/sbin/reboot
Copy after login

sudo作用:把原本只能由root用户执行的命令赋于普通用户执行。

四、小结

关于linux系统的权限,主要分为三个部份:普通文件/目录、可执行文件和系统命令,每种权限的诞生都是为了解决相关的权限问题,所以要明白为何会有这些权限。

The above is the detailed content of Linux system permissions analysis: permission classification and representation methods of ordinary files and directories. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!