Linux文件和目录权限企业级实战用法 | 学习笔记

简介: 快速学习Linux文件和目录权限企业级实战用法

开发者学堂课程【Linux企业运维实战 - 入门及常用命令Linux文件和目录权限企业级实战用法】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/550/detail/7614


Linux文件和目录权限企业级实战用法


目录

一、LINUX文件系统上的特殊权限

1.SUID

2.SGID

3.Sticky

二、特殊权限数字法

三、设定文件特定属性

四、原权限不变的情况下增加一个新需求

五、访问控制列表

六、总结

七、访问控制列表


一、LINUX文件系统上的特殊权限

1.SUID

[root@centos6 ~]#11 /data/f1 ..to ldtn151

-r--rw-rwx. 1 wang gi 0 Apr4 09:50 /data/f1

[root@centos6~]#su- wang

[wang@centos6~]$passwd

changing password for user wang.

changing password for wang.

(current)'UNIXpassword:

New password:

BAD PAsSwORD: it is based on a dictionary word

New password:

BAD PASSwORD: it is based on a dictionary word

New password:

Retype new password:

Sorry, passwords do not match.

Password:

passwd: Have exhausted maximum number of retries for service

[wang@centos6 ~]$passwd

changing password for user wang.

changing password for wang.

(current)'UNIXpassword:

New password:

Retype new password:

passwd: a11 authentication, tokens updated successfully.

[wang@centos6 ~]$77 /etc/shadow

----------. 1 root root 1105 Apr414:03 /etc/shadowr.nAo--e."_'

[wang@centos6 ~ ] $which pas swd

/usr 7bin/ passwd

[wang@centos6 ~]$11 /usr /bin/passwd

-rwsr-xr-x. 1 root root 30768 NOv 24 2015 /usr /bin/ passwd

SUID权限是作用在所有者的执行位上,效果是当用户运行程序时,正常情况下程序去访问某个文件等到的权限是由用户决定,这是以前的情况。

一旦有了SUID权限,就去看这个程序的所有者是谁,SUID的功能就是会继承权限所有者的权限,程序所有者是root,相当于用户在运行程序时提升身份变成了root。

设权限可以使用数字法和模式法

[root@centos6 ~] #chmod u-X /usr /bin/ passwd

[root@centos6 ~]#11 /usn /bin/ passwd

rwSr-xr-x. 1 root r oot  30768 NOV 24  2015 /usr/bin/pas swd

[root@centos6 ~] #chmod U+x /usr /bin/pas swd

[root@centos6 ~]#11 /usr/bin/passwd

rwsr-xr-x. 1 root root 30768 NOV 24  2015 /usr /bin/ passwd

[root@centos6 ~] #chmod u-S /usr/bin/pas swd

[root@centos6 ~]#11 /usr/bin/ pas swd

rwxr-xr-x. 1 root root 30768 NOV 24 2015 /usr/bin/pas swd

[root@centos6 ~ 1 #chmod U+S /usr /bin/ passwd

[root@centos6 ~]#11 /usr /bin/passwd

rwsr-xr-X. 1 root root 30768 NOV24  2015 /usr/bin/passwd

[root@centos6 ~]chomd 4755 /usr/bin/passwd suid=4

[rootacentos6 ~」 #which nano/bin/nano

[root@centos6 ~]#11 /bin/nano

-rwxr-xr-x.1 root root 178896 NOV 12 2010 /bin/nano

[root@centos6 ~ 1 #chmod U+S /bin/nano

[root@centos6 ~]#11 /bi n/nano

-rwsr-xr-x. 1 root root 178896 NOV 12 2010 /bin/nano

[ root@centos6] #SU-wang

[wang@centos6 -] Snano /etc/ shadow

 

SUID权限除了特殊情况一般情况下不会加,相当于用这个工具可以修改任何人的文件。

SUID权限只适合用应用于二进制的可执行的程序上,其他都不适合。作用是继承所有者的权限。

SUID的特殊场景(共享修改文件):

[root@centos7 dir1]#usermod -aG opts wang[root@centos7 dir1]#usermod -aG opts magelroot@centos7dir1#id wang

uid=1000(wang) .gid1000(wang) groups=1000(wang),1003(opts)[root@centos7 dir1]#idmage

uid=1001(mage) gid1002(mage) groups=1002(mage),1003(opts)[root@centos7 dir1]#17

total 0

-rw-rw-r--. 1 mage_mage 0 Apr422:16 magefile[root@centos7 dir1]#chgrp opts*

[root@centos7 dir1j#11

Total 0

-rw-rw-r--. 1 mage_opts 0 Apr4 22:16 magefile[root@centos7 dir1]#su wang

[wang@centos7 dir1]1$17

Total 0

-rw-rw-r--. 1 mage_opts 0 Apr . 422:16 magefile

[wang@centos7 dir1]$echo wangdata >> magefile

[wang@centos7 dir1]$touch wangfile

lwang@centos7 dirij$11

Total 4

-rw-rw-r--. 1 mage opts 9 Apr4 22:26 magefile

-rw-rw-r--.1 wang wang o Apr4 22:26 wangfile

[wang@centos7 diri]$exit

exit

[root@centos7 dir1]#su mage

[mage@centos7 dir1]$77

total4

-rw-rw-r--. 1 mage opts 9 Apr4 22:26 magefile

-rw-rw-r--. 1 wang_wang 0 Apr4 22:26 wangfile

[mage@centos7 dir1]$exit

exit

[root@centos7dir1]#chgrp opts  *

[root@centos7 dir1]#11

Total 4

-rw-rw-r--. 1 mage opts 9 Apr4 22:26 magefile

-rw-rw-r--. 1 wang opts 0 Apr4 22:26 wangfile

2.SGID

作用:继承所属组的权限。当一个用户运行程序的时候,它将继承该程序所属组的权限,如果这个组可以进行对资源的访问,那么用户就获取了对资源的访问权限。

[wang@centos6 ~]$11 /usr/bin/passwd

- rwsr-xr-x. 1 root root 30768 NOV 24 2015 /usr /bin/passwd

[wang@centos6 ~] $exit

Logout

[root@centos6 ~]#11 /bi n/nano

-rwsr-xr-x. 1 root root 178896 NOV 12 2010 /bi n/nano

" root@centos6 ~] #chmod u-S /bin/nano

[root@centos6 ~]#11 /bin/nano

-rwxr-xr-x. 1 root root  178896 NOV 12 2010 /bin/nano

[root@centos6 ~ ] #chmod g+s /bin/ nano

[root@centos6 ~#11 /bin/nano

-rwxr-sr-x.1 root root 178896 Nov 12 2010 /bin/nano

同样它也支持模式法授权

[root@centos6 ~] #chmod g+s /bin/nano

[root@centos6 ~]#11 /bin/ nano

-rwxr-sr-x. 1 root root 178896 NOV 12 2010 /bin/nano

[root@centos6 ~] #chmod 2755 /bin/nano

[root@centos6 ~]#11 /bin/nano

-rwxr-sr-x. 1 root root 178896 NOV 12 2010 /bin/nano

[root@centos6 ~]#suid4 sgid2 sticky1 粘滞位

只要对目录有写权限,就可以在目录里任何人建立新文件。

3.Sticky

作用:一旦加了这种权限(drwxrwxrwx.2 root root 22 apr  4  22:16 dirl)就不可以在目录随便删除文件了,可以自己删除自己的,但是不可以删除别人的,即使拥有写权限。那么如何添加?

[root@centos7 [data] #chmod o+t dir1

[root@centos7 data]#11

total 0

drwxrwxrwt.2 rootroot  22 Apr 4 22:16 dir1

[wang@centos7 dir1]$stouch wangfile

[wang@centos7 dir1]$11

Total 0

-rw-rw-r--. 1 mage mage 0 Apr 4 22:16 magefile

-rw-rw-r--.1 wang. wang 0 Apr 4 2 22:18 wangfile

[wang@centos7 dir1]$rm  -f wangfile

[wang@centos7 dir1]$rm  -f magedile

rm: cannot r emove" magefi le' : operation not permi tted

[wang@centos7 dir1]$11 -d /data/dir1

drwxrwxrwt.2 root root 22 Apr  4 22:18 , data/dirl

[wang@centos7 dir1]$exit

Exit

[root@centos7 dir1] #chmod 1777 /data/dir1

[root@centos7 dir1]#11 -d /data/dir1

drwxrwxrwt.2 root root 22 Apr  4  22:18 7 data/dir1

Sticky位

◆具有写权限的目录通常用户可以删除该目录中的任何文件, 无论该

文件的权限或拥有权

◆在目录设置 Sticky 位,只有文件的所有者或 root 可以删除该文件

◆sticky设置在文件上无意义

◆权限设定:

chmod o+t DIR...,

chmod o-t DIR...

例如:

➢Is -Id /tmp drwxrwxrwt 12 root root 4096 Nov 2 15:44 /tmp

4.总结:

Suid:继承二进制程序所有组的权限

Sgid:

1)继承二进制程序所有组的权限

2)作用于目录,此目录新建的文件继承目录的所属组

sticky:作用于目录,此目录的文件只能被所有者删除

 

二、特殊权限数字法

> SUID SGID STICKY

000 0

001 1

010 2

011 3

100 4

101 5

110 6

111 7

◆chmod 4777 /tmp/a.txt

 

三、设定文件特定属性

◆chattr+i不能删除,改名,更改.

◆chattr +a只能追加内容

◆lsattr显示特定属性

所有的权限似乎都是针对普通用户说了算,好像管理员都是超脱程序控制的,所以容易对文件进行误操作。所以为了避免重要文件被破坏,系统中额外提供了一些手段:chatter。

这个工具可以针对特定的文件加以保护,避免 root 账号直接修改和删除以及更换该文件名。

[root@centos7 ~]#cp /etc/ shadow /data

[ root@centos7 ~]#cd /data

[root@centos7 data]#ls Dir1 shadow

[root@centos7 data]#rm -rf dirl/

[root@centos7 data]#ls

shadow

[root@centos7 data]#11

total 4

1 root root 1203 Apr 4 22:37 shadow

[root@centos7 data]#cat shadow

增加权限:[root@centos7 data]#chatter  +i s

此文件可以读但是不可以更改了。

去掉权限:[root@centos7 data]#chatter  -i s

只能追加不能删除的锁定方法:chatter  +a ss

去掉方法:chatter-a ss

锁定读时间:chatter +A atime

 

四、原权限不变的情况下增加一个新需求

[root@centos7 ~ ] #umask

0022

[root@centos7 ~]#cd /data

[root@centos7 data]#rm -f *

[ root@centos7 data] #touch f1

[r oot@centos7 data]#] 1

total 0

rw-r--r--. 1 root root R Apr 4 22:48 f1

 

五、访问控制列表

◆ACL : Access Control List ,实现灵活的权限管理

◆除了文件的所有者,所属组和其它人,可以对更多的用户设置权限

◆CentOS7默认创建的xfs和ext4文件系统具有ACL功能

◆CentOS7之前版本,默认手工创建的ext4文件系统无ACL功能,需手动增加

tune2fs - o acl /dev/sdb1

mount -o acl /dev/sdb1 /mnt/test

◆ACL生效顺序:所有者,自定义用户,自定义组,其他人

[root@centos7 ~] #umask

0022

[root@centos7 ~]#cd /data

[root@centos7 data]#rm -f *

[root@centos7 datal #touch f1

[root@centos7 data]#] 1

total 0

-rw-r--r--.1 root root 0 Apr  4 22:48 f1

[root@centos7 data]#setfacl -m u:wang:0 f1

[root@centos7 data]#1 1 f1

-rw-r--r--+ 1 root root 0 Apr 4 22:48 f1

[root@centos7 data] #sU wang

[wang@centos7 data] $cat f1

Cat: f1: Permission deni ed

Lwang@centos7 dataysh f1

-rw-r--r--+ 1 root root 0 Apr 4 22:48 f1

[wang@centos7 data] $exit

exit

[root@centos7 data] #su mage

[mage@centos7 data]$11

total 0

-rw-r--r--+ 1 root. root 0 Apr 4 22:48 f1

[mage@centos7 data] $cat f1

[ mage@centos7 data] $echo magedata >> f1

bash: f1: Permission denied

[ mage@centos7 data] $exit

exit

[root@centos7 data]#setfacl -m u:mage:rw f1

[root@centos7 data]#17f1

rw-rw-r--+ 1root_root 9 Apr422:55f1[root@centos7 data]#setfacl -m g:gi:rw f1[root@centos7 dataj#getfaci f1

[root@centos7 data] #id wang

uid=1000(wang) gid=1000(wang)groups=1000(wang),1003(opts)[root@centos7 data] #gpasswd -a wang g1

Adding user wang to group gl

[root@centos7data]#id wang

uid=1000(wang) gid=1000(wang)groups=1000(wang),1001(g1),1003(opts)

[root@centos7 data]#17f1

-rw-rw-r--+ 1 root_root _9 Apr4 22:55 f1

[root@centos7 data] #getfacl f1

[root@centos7 data]#su wang

[wang@centos7 data]$cat f1at:f1:Permission denied

[wang@centos7 data]$exit

exit

[root@centos7data]#groupadd g2

[root@centos7dataj#setfac1 -mg:g2:x f1

[root@centos7 data]#useradd tom-root@centos7dataj#usermod -aG gi,g2 tomlroot@centos7data]#id tom_

uid=1002(tom) gid=i005(tom) groups=1005(tom),1001(g1),1004(g2)

[root@centos7 data]#setfac1

Usage: setfac1 [-bkndRLP] { -ml-Ml-xl-x ... } file ...Trysetfac1_--help' for more information.

[root@centos7 data]#getfaci f1

删除方法:[tom@centos7 data]$exitexit

[root@centos7 data]#setfacl -xg2 f1

[root@centos7 datai#getfacl f1

 

需求:用户除了所有者(user)和other,剩下的人(自己人为指定的用户)进行一下控制,统一设一个最大权限。

[root@centos7 data]#setfacl -m g:g2:x f1

[root@centos7 data]#getfaclfF1

#file: f1

#owner : root

#group : rootuser : :rw-

user :wang:---user : mage : rw-group : :r--group : g1l:rw-group : g2:--区mask : :rwx

Other:r--

如果想实现所有的特定用户只能是读,传统方法一个一个更改,比较麻烦。Mask相当于直接定义了other的最高权限,类似于交通中限高的作用,只能比读权限小,不能超过读权限。

[root@centos7 data]#17f1

-rw-rwxr--+ 1 root_root 9 Apr4 23:01 f1

[root@centos7 data]#setfaci -m mask : :r f1

[root@centos7 dataj]#getfacifF1

#fiTe: f1

#owner : root

#group : rootuser : :rw-

user :wang: ---

user :mage : rw-      #effective: r--

group: :r--

group : g1: rw-       #effective: r--

group :g2 :--×        #effective: ---

mask: :r--

other : :r--

表面上给了mage读写权限,但是生效的是读权限;实际上给了g2执行权限,实际上是没有权限。这就是限高起了作用。

Mask权限就是组权限,换句话说,一旦设了ACL权限,chomd g=rw f1改的并不是组的权限,而是mask的权限。

ACL权限如果想全部清除可以如下操作:

[root@centos7 data]#setfac -b f1

[root@centos7 data]11  f1

rw-r--r--1 root_root9 Apr4 23:01 f1

Usage: setfac1 [-bkndRLP] i -m!-Ml-xl-X... } file ...

Try  setfac1_--help' for more  information.

[root@centos7 data]#getfacl  f1

#fiTe: f1

#owner: root

#group : root

user : : rw-

group : :r--

other: : r--


六、总结

在没有 ACL 权限的情况下如果是传统权限,就是看是否是文件所有者,是的话所有者权限直接生效。如果不是的话,就看组,是组的话所属组权限生效。如果不是组就是 other 权限最终生效。

如果有了ACL权限,先看所有者,如果是所有者,所有者直接生效。如果不是就按照顺序一个一个看,先看自定义用户,再看自定义组,如果都不是,就是 other。

一旦有了mask,那么自定义用户和自定义组就会受mask权限的一个控制,只能等于或者小于它。ACL不是随便一个系统都能进行设置的。

 

七、访问控制列表

为多用户或者组的文件和目录赋予访问权限rwx

mount -o acl /directory

getfacl file |directory

setfacl -m u:wang:rwx file | directory

setfacl -Rm g:sales:rwx directory

setfacl -M file.acl file| directory

setfacl -m g:salesgroup:rw file | directory

setfacl -m d:u:wang:rx directory

setfacl -x u:wang file |directory

setfacl -X file.acl directory

[root@centos7 data]#1l1 f1

-rw-r--r--. 1 root root 9 Apr 4 23:01 f1

[root@centos7 data] #

[ root@centos7 data] #

[ root@c entos7 data]#file f1

f1: ASCII text

[root@centos7 data]#cat > acl . txt

u:wang:rx

g:g1:rw

g:g2:x

[root@centos7 data ] #AC

[ root@centos7 data] #cat ac1.txt

u :wang:rx

g:g1:rw

g:g2:x

 

◆ACL文件上的 group 权限是 mask 值(自定义用户,自定义组,拥有组的最大权

限) ,而非传统的组权限

◆getfacl可看到特殊权限: flags

◆通过ACL赋予目录默认x权限,目录内文件也不会继承x权限

◆base ACL不能删除

setfacl -k dir 删除默认ACL权限

◆setfacl -b file1清除所有ACL权限

◆getfacl file1| setfacl --set-file=- file2 复制 file1 的 acI 权限给file2

◆mask只影响除所有者和 other 的之外的人和组的最大权限

Mask需要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective Permission)

用户或组的设置必须存在于 mask 权限设定范围内才会生效

setfacl -m

mask:rx file

◆-- set选项会把原有的 ACL 项都删除,用新的替代,需要注意的是一定要包含

UGO的设置,不能象-m-样只是添加ACL就可以

◆示例:

setfacl --set u::rw,u:wang:rw,g.:ro::- file1

◆备份和恢复ACL

◆主要的文件操作命令 cp 和 mv 都支持 ACL ,只是 cp 命令需要加上-p参数。但是tar等常见的备份工具是不会保留目录和文件的ACL信息

getfacl -R /tmp/dir1 > acl.txt

setfacl -R -b /tmp/dir1(-b:清空)

setfacl -R --set-file=acl.txt /tmp/dir1

setfacl --restore acl.txt

getfacl -R /tmp/dir1

[root@centos7 data]#cp -P f1 f3

[root@centos7 data] #getfacl f3

#file:f3

# owner: r oot

# group: root

user: :rw-

group: :r--

group:g2:--X

mask: :r-x

other: :r--

 

三种常用权限︰r, w, xuser, group, other

安全上下文

前提:进程有属主和属组﹔文件有属主和属组

(1)任何一个可执行程序文件能不能启动为进程,取决发起者对程序文件是否拥有执行权限

(2)启动为进程之后,其进程的属主为发起者,进程的属组为发起者所属的组(3)进程访问文件时的权限,取决于进程的发起者

(a)进程的发起者,同文件的属主∶则应用文件属主权限

(b)进程的发起者,属于文件属组﹔则应用文件属组权限

(c)应用文件“其它”权限

相关文章
|
9天前
|
存储 Linux
Linux 目录名称
Linux系统目录结构简介:根目录(/)下包含各类功能目录,如/bin存放用户命令,/etc存储配置文件,/home为用户主目录,/var记录日志等可变数据,/usr存放用户工具,/tmp用于临时文件。各目录分工明确,保障系统有序运行。(238字)
108 5
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
310 3
|
6月前
|
Linux 定位技术
Linux系统中的cd命令:目录切换技巧
踏过千山,越过万水,人生就是一场不断前行的旅程,总充满了未知与挑战。然而,“cd”命令如同你的旅伴,会带你穿梭在如棋盘一般的文件系统中,探索每一处未知。希望你能从“cd”命令中找到乐趣,像是掌控了一种络新妙的魔法,去向未知进发,开始你的探索之旅。
257 24
|
7月前
|
监控 Linux
Linux基础:文件和目录类命令分析。
总的来说,这些基础命令,像是Linux中藏匿的小矮人,每一次我们使用他们,他们就把我们的指令准确的传递给Linux,让我们的指令变为现实。所以,现在就开始你的Linux之旅,挥动你的命令之剑,探索这个充满神秘而又奇妙的世界吧!
153 19
|
7月前
|
安全 Linux 数据安全/隐私保护
【Linux】深入理解linux权限
本文深入解析Linux权限管理机制,涵盖权限概念、用户角色、文件属性及操作方法。文章分为前言、权限介绍、用户与角色、文件属性、权限修改及常见问题六大板块。详细说明了权限类型(r/w/x)、角色优先级、chmod/chown指令用法,以及目录权限、umask掩码、粘滞位等重点内容。掌握这些知识,可有效提升Linux系统安全性和灵活性,是管理员必备技能。喜欢的话别忘了点赞支持哦! ❤❤❤
371 6
|
7月前
|
Linux
Linux目录删除指南:彻底解决“Is a directory”错误
在 Linux 系统中遇到 `cannot remove 'xxx': Is a directory` 错误,是因为删除目录时未使用正确参数。解决方法包括:1) 使用 `rmdir` 删除空目录或 `rm -r` 删除非空目录;2) 检查并调整目录权限(如通过 `sudo` 提权);3) 处理特殊场景,例如文件属性异常、特殊字符或进程占用;4) 替代方法如 `find -delete` 或文件系统修复。操作前建议备份数据,并启用防误删功能(如 `alias rm='rm -i'`)。掌握 `rm` 和 `rmdir` 的区别是关键。
566 1
|
26天前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
266 1
二、Linux文本处理与文件操作核心命令
|
26天前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
536 56
|
11天前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
121 2
|
21天前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
70 0
Linux内存问题排查命令详解