How to set permissions on Linux?

Linux set under authority

1. How do you see and read rights information

1. Check the file attributes

ls -l file View file properties
ls -l say Check the directory within the file information
ls said -ld View properties directory
ll file Equivalent to ls -l file (ls -l equivalent ll)
ll -d dir The equivalent of ls -ld dir
ls -lR file Subdirectories recursively display

Experimental operation
(1) view the file properties

[root@workstation Desktop]# ls -l file1 
[root@workstation Desktop]# ll file1 

Such as viewing file attributes are:

 - | rw-r--r--| . | 1 | root | root | 6 |Apr 2 02:03 | filename
 1     2        3   4      5    6      7        8           9  

The meaning of each field as follows:
Type / permissions / security context / number of hard links / owner / owned group / file size / file was last modified time / file name

Here Insert Picture Description
(2) View catalog file information

[root@workstation Desktop]# ls -l dir1/
[root@workstation Desktop]# ll dir1/
 d | rw-r--r--| . | 2 | root | root | 32 |Apr 2 02:03 | dirname
 1     2        3   4      5    6      7        8           9  

Meaning once for each field:
Type / permissions / security context /Catalog number of subdirectories / Owner / owned group / The size of the sub-file or subdirectory metadata/ File was last modified time / file name
Here Insert Picture Description
property (3) View catalog

[root@workstation Desktop]# ls -ld dir1/
[root@workstation Desktop]# ll -d dir1/

Here Insert Picture Description
(4) display subdirectories recursively

[root@workstation Desktop]# ls -lR dir1/
[root@workstation Desktop]# ll -R dir1/

Here Insert Picture Description

2. For understanding the properties of each field
1. For an understanding of the file attributes field

1 Types of
2 file permission
3 SELinux Context (security context)
4 The number of hard linked files
5 File has a
6 File has set
7 File size
8 The file was last modified time
9 file name

(1) File Type

     -     #普通文件
     d    #目录
     l    #软连接(类似于快捷方式)
     b    #块设备(磁盘/u盘)
     c    #字符设备
     s    #套接字socket(进入程序(eg.数据库)内部操作 ls没有对外的接口)
           套接字类似于程序(例如数据库)的门(程序对外开放的接口)
     p    #管道|

(2) file permissions
| Rw- | rw- | r- | three to one, respectively, on behalf of the user, group, other (abbreviated as U, G, O)
U: What operating file owner of the file can do (the owner)
G : file all set to file what action (all groups) do
o: what action (other people) to files other people can do

r write
w read
x carried out
- No function

(3) The security context, (Judge things like firewall is turned on). Enables
(4) the number of hard links

储存文件的区域划分为节点区和数据区
当节点区域的内存不够时,可以在数据区已有的文件写数据内容,但是不能创建文件
当数据区的内存不够时,可以新建文件,但是不能在数据区内写数据内容

  • 硬链接:一个节点对应数据区的多个文件(硬链接含义:文件被记录的次数)
  • 软链接:通过找其他的节点,再找对应的文件,也就是数据区的一个文件读英节点区的多个节点

(5)文件拥有着user

(6) 文件所有组grooup

(7)文件大小(文件的字节数)
文件大小是根据文件内容决定的,需要注意,最后还有一个换行符号
(8)文件最后一次被修改的时间
修改文件的时间戳

Here Insert Picture Description

[root@workstation Desktop]# touch -t 07201212 file1 

(9)文件名称
3.对于目录各字段的理解
目录与文件的区别是

  • 第4个 代表文件中子目录的个数
  • 第7个 代表子文件或者子目录元数据的大小
    实验:
    == 1.子目录的个数==
    Here Insert Picture Description
    2子文件或者子目录元数据的大小
    Here Insert Picture Description

解析:
新建目录,里面包含因此隐藏文件,共有6个
新建名称为1的文件,在redhat8中原始有8个元数据,名称1有一个元数据,共有9个元数据

两者相加,故共有15个元数据

2.如何改变文件的拥有者和拥有组

前言知识

chown 用户名 文件 更改文件的拥有者
chgrp 组名称 文件 更改文件的所有组
chown -R 用户名 文件 更改目录的拥有者
chgrp -R 组名称 文件 更改目录的拥有组
chown 用户名:组名称 文件/目录 更改文件或者目录的拥有者和拥有组

实验操作:
思路:先建立文件和目录,再监控文件和目录所有人所有组改变的过程
step1:监视文件

[root@workstation Desktop]# watch -n 1 ls -lR /mnt/

Here Insert Picture Description
step2.更改文件的所有人和所有组
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
step3.递归更改目录用户名/组名称

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

3.让你增加对文件权限的认知理解!

- 此位表示权限未开启
r 查看权限 对文件,可以查看文件里的内容;对目录,可看目录中有什么内容(列出目录中文件/目录名称)
w 可写权限 对文件,可以更改文件内的内容;对目录,可以对目录里文件和内容进行重命名/删除/新建操作,但是不能修改文件的时间戳和权限
x 可执行权限 对文件,可用文件名称调用文件内部记录的程序命令;对目录,可进入目录(进入目录后怎么操作,要看用户对此目录有何权限)

r:查看权限(可读权限)
Here Insert Picture Description
Here Insert Picture Description

w:可写权限
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
对于修改权限,可以修改目录权限,不能修改文件权限
x:可执行权限
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

4.文件权限设定方式

1.依照模板复制权限
(1)复制文件,但是没有复制文件的权限

[root@workstation mnt]# cp file file3

Here Insert Picture Description
(2)复制文件权限以及文件内容

[root@workstation mnt]# cp -p file file2

Here Insert Picture Description
(3)复制文件权限,但是不复制文件内容

注意(1)第一个为源文件 第二个为待效果文件(2)第二个文件一定要存在

[root@workstation mnt]# chmod --reference=file file3

Here Insert Picture Description
(4)复制目录权限,但是不复制目录内容

注意:第二个目标文件一定要存在
Here Insert Picture Description

2.字符方式设定文件权限
(1)同时更改拥有者/拥有组和其他人权限,使用逗号隔开

[root@workstation mnt]# chmod o+r,u-r,g-r dir

Here Insert Picture Description
(2)可同时改变可读/可写/可执行权限

[root@workstation mnt]# chmod o-rwx dir

Here Insert Picture Description
(3)-r -x -w 的区别
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
3.数字方式设定文件权限

[root@workstation mnt]# chmod 777 file

Here Insert Picture Description

5.系统预留权限阀值

1.对于权限来说,开放的越大(例如rwx都开了),共享效果越明显,但是安全性越差;
2.资源存在的意义在于共享,在系统中应该开放相应的权力(共享一定的资源),并且保留不安全的权力以确保系统功能性及安全性。

1.临时修改权限
(1)shell 中可以直接使用umask来查看系统预留权限值


[root@workstation Desktop]# umask
0022

系统默认的权限预留值为0022
建立目录时,权限值默认为为777-022=755
建立文件时,权限值默认为为777-022-111=644

Here Insert Picture Description
(2)并且可以直接使用umask设定系统预留权限值

[root@workstation Desktop]# umask 337

Here Insert Picture Description

临时更改系统默认权限值为337
建立目录时,权限值默认为777-337=440
建立文件时,权限值默认为777-337-111=440

2.永久修改权限

(1)系统默认权限预留阀值的更改需要更改两个配置文件
(2)两个配置文件分别是(1)shell配置文件 /etc/bashrc (2)系统环境配置文件 /etc/profile

(3)修改完配置文件后,需要告诉系统,配置文件已经更新
(4)告诉系统的命令为 source /etc/bashrc ; source /etc/profile ;

step1:vim打开配置文件

[root@workstation Desktop]# vim /etc/profile
[root@workstation Desktop]# vim /etc/bashrc

解读配置文件内容:

    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 226
    fi

如果uid大于199,并且uid等于gid的情况下,在第一行修改;(并且的关系)
否则在第二行修改
root用户在第二行修改呀,因为root的uid为0

普通用户一般在第一行进行预留权限阀值的修订

step2:找到umask关键字(/ 反斜杠搜索功能),更改文件配置
step3:告诉系统,配置文件已经修改

[root@workstation Desktop]# source /etc/bashrc 
[root@workstation Desktop]# source /etc/profile

Here Insert Picture Description
Here Insert Picture Description

6.特殊权限

stickyid粘滞位 目录:当目录上有stickyid权限时,所有用户均可在该目录创建文件,但只有文件所有人和root用户可删除该目录下的文件;对文件的影响:不调用的时候也会被加载到交换空间
sgid强制位 当目录上有sgid权限时,任何人在该目录建立的文件或者目录的所有人都属于该目录(理解:我在这个厂子干活,我所生产的物品是属厂商的)
suid冒险位 对于文件,当文件上有suid权限时,任何人执行这个文件所产生的进程都属于文件的所有人(拥有者)(理解:当我用工厂的机器加工商品时,这个机器的所有人仍然归工厂所有)

stickyid粘滞位

思路:
(1)建立public公共目录(给目录满权限),建立监控
(2)其次:普通用户全部都可以在目录内增删任意文件,此目录存在问题(我的东西能被别人的删掉)
(3)t权限: chmod o+t /mnt/public chmod o-t /mnt/public
对目录的影响:other位有t 文件只能被所有人和超级用户删除(操作不被认可)
对文件的影响:不调用的时候也会被加载到交换空间

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description
sgid强制位

实验思路:
(1)保证实验环境纯净,建立public 设置权限满权限,监控watch -n 1 ls -lR /mnt/
(2)创建proc组,改变public所有组为 proc
[root@workstation mnt]# groupadd proc
[root@workstation mnt]# chgrp proc public/
(3)切换到student,使用student建立的文件,但是文件的所有组属于student
(3)但是我们需要,在public组内建立的文件,需要和public的拥有组是一样的
(4)所以需要chmod g+s /mnt/public/
[root@workstation ~]# chmod g+s /mnt/public/
(5)任何用户再再建立文件的时候,文件的拥有组属于proc组
理解:
在工厂中建立了奔驰汽车,是属于公司工厂的
这里prod是生产商,student是自己,自己建的汽车是属于工厂的
注意点:当目录具有sgid权限时,再次建立的文件和public目录时相同的拥有组,但是现存的文件的拥有组不会改变
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

suid冒险位

程序是文件 程序正在运行的状态是进程
来理解:我们用程序在干活 活是谁的 和工具是谁的是两码事

(1)eg cat本身是属于root组,student组运性,这个进程就是student的!谁发起的 进程是谁的
(2)想要实现:不管是谁执行,进程都是文件的所有组。(用户提升了,本身和root用户不沾边)
(3)具体做法:可以先让命令有xxxx身份。当你用这程序工具时,将具有xxx身份。
(4)usermod -G root student (却是student做任何事情都是root) 只是想让student运行cat时,使用root身份!
(5) chmod u+s /bin/cat 任何人在运行cat 都是root用户 运行完之后,该是谁还是谁。只是在执行这个程序时,让你变成所有人/所有组。

(1)cat命令实验
step1:监视进程的所有人和所有组

[root@workstation Desktop]# watch -n 1 "ps ax -o user,group,comm | grep cat"

step2:student用户运行cat命令,显示cat进程的所有人是student
step3:更改冒险位(更改所有人

[root@workstation Desktop]# chmod 4777 /usr/bin/cat

或者(更改所有组

```bash
[root@workstation Desktop]# chmod 6777 /usr/bin/cat

step4:任何用户执行cat都是root身份(因为设置了root时的冒险位)
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
(2)rm命令实验
使用rm实验防止问题:防止删除文字的时候,误操作
step1:监视进程和rm文件(/usr/bin/rm)
step2:更改文件的所有人和所有组(以便其他用户在执行这个命令时,使用的是更改后的所有人和所有组)

[root@workstation Desktop]# chown student:student /usr/bin/rm

step3:更改冒险位

[root@workstation Desktop]# chmod u+s,g+s /usr/bin/rm

step4:使用root执行删除命令,显示没有权限
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

7.ACL权限列表

(1) 引入:想让root和student对文件可写. access control listACL (who can do what added)
(2) When permission to open a list of attributes of a file permissions after a "+" plus sign
(3) when the list of permissions to open, view the list of permissions permissions: getfacl Username

ACL experimental ideas:
(1) experimental environment clean and
(2) monitoring and directory permissions list

[root@workstation Desktop]#watch -n 1 "ls -l /mnt/;getfacl /mnt/file"

(3) to open the permissions list (after permission to add a "+", and added user: student: rwx)

[root@workstation Desktop]# setfacl -m u:student:rwx /mnt/file

(4) remove permissions list (does not specify a particular user)user: student: rwx disappear! , But "+" is still

[root@workstation Desktop]# setfacl -x u:student /mnt/file

(5) close the list of permissions "+" Disappears

[root@workstation Desktop]# setfacl -b /mnt/file

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Published 11 original articles · won praise 3 · Views 532

Guess you like

Origin blog.csdn.net/baidu_40389082/article/details/103807275