用户管理及系统权限管理

本文详细介绍了Linux系统中的文件权限查看、读取、设定方法,包括普通权限的类型及作用、chmod命令的使用、系统默认权限设定、文件用户组管理、特殊权限如sticky位、sgid位和suid位的解释,以及ACL访问控制列表和attr权限的使用。内容覆盖了文件和目录的权限控制,强调了系统安全性和资源共享之间的平衡。
摘要由CSDN通过智能技术生成

一.权限查看及读取

1.权限查看

[root@localhost ~]# ls -l file
-rw-r--r--. 1 root root 0 Jan 29 21:00 file
[root@localhost ~]# ls -ld dir
drwxr-xr-x. 2 root root 6 Jan 29 21:03 dir

2.权限的读取

“文件的属性被叫做文件的元数据(meta date)”
“一种元数据用1个byte来记录”

文件权限信息:

- | rw-r--r-- | .  | 1 | root |  root | 0 |  Jan 29 21:00  |  file
[1]    [2]     [3]  [4]   [5]    [6]   [7]        [8]          [9]

目录权限信息:

d | rwxr-xr-x | .  | 2 | root |  root | 6 |  Jan 29 21:03  |   dir
[1]    [2]     [3]  [4]   [5]    [6]   [7]        [8]          [9]

对于每一位的解释:
[1] 文件类型

-普通文件
d目录
l软链接
b快设备
c字符设备
ssocket套接字
p通道

[2] 用户权限

rw- | r-- | r--
 u      g    o

[3]系统的selinux开启
[4]对于文件:文件内容被系统记录的次数(硬链接个数)
对于目录:目录中子目录的个数
[5]文件拥有者
[6]文件拥有组
[7]对于文件:文件内容大小
对于目录:目录中子文件的元数据大小
[8]文件内容被修改的时间
[9]文件名称

二.普通权限的类型及作用

1.用户文件的身份

u:# user 文件的拥有者,ls -l 看到的第五列信息
g:# group文件的拥有组,ls -l 看到的第六列信息
o:# other 既不是拥有者也不是拥有组成员的其他用户的统称

2.权限位

rwx |r-- |r--
 u    g    o

3.用户身份匹配

user>group>other

4.权限类型

-权限未开启
r可读
对于文件:可以读取文件内容
对于目录:可以ls列出目录中的文件
w可写
对于文件:可以更改文件内容
对于目录:可以在目录中新建或者删除文件
x可执行
对于文件:可以用文件名称调用文件内记录的程序
对于目录:可以进入目录中

三.设定普通权限的方法

1.chmod 设定文件权限

chmod复制权限
chmod --reference=/tmp /mnt/westosdir复制/tmp目录的权限到/mnt/westosdir上
chmod -R --reference=/tmp /mnt/westosdir复制/tmp目录的权限到/mnt/westosdir及目录中的子文件上。 -R 代表递归操作
chmod字符方式设定权限
chmod <a/u/g/o><+/-/=><r/w/x> file用字符方式设定文件权限
[root@localhost mnt]# chmod u-rw file1
[root@localhost mnt]# chmod u-rw,g+x,o+wx file2
[root@localhost mnt]# chmod u=rwx,g=rx,o=--- file3
[root@localhost mnt]# chmod -R u=rwx,g=rx,o=--- /mnt/dir

在这里插入图片描述
在这里插入图片描述

2.chmod数字方式设定权限

权限波尔值表示方式

rwx = 111
--- = 000
三位二进制可以表示的最大范围为8进制数
rwx=111=7
rw-=110=6
r-x=101=5
r--=100=4=r
-wx=011=3
-w-=010=2
--x=001=1=x
---=000=0

5=4+1=r-x


[root@localhost mnt]# ls -l /mnt/file1
-rw-r--r--. 1 root root 0 Jan 29 22:58 /mnt/file1
[root@localhost mnt]# chmod 600 /mnt/file1
[root@localhost mnt]# ls -l /mnt/file1
-rw-------. 1 root root 0 Jan 29 22:58 /mnt/file1

四.系统默认权限设定

系统本身存在的意义:共享资源
从安全角度讲,系统共享的资源越少,开放的权利越小,系统越安全。
既要保证系统安全,又要系统创造价值,于是把应该开放的权利默认开放,把不安全的权利默认保留。

如何保留权利

umask (查看保留权利)
umask 权利值 (临时设定系统预留权利)
文件默认权限=777-umask-111
目录默认权限=777-umask
umask值越大系统安全性越高

umask临时更改

[root@localhost mnt]# umask
0022
[root@localhost mnt]# umask 077
[root@localhost mnt]# umask
0077

永久更改

vim /etc/bashrc			##shell系统配置文件
	if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002-->022		#普通用户的umask
    else
       umask 022			#roo用户的umask
    fi

vim /etc/profile			##shell系统配置文件
	if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002-->022		#普通用户的umask
    else
       umask 022			#roo用户的umask
    fi
source /etc/bashrc			#source作用时使我们更改的内容立即被系统识别
source /etc/profile

五.文件用户用户组管理

chown username file更改文件拥有者
chgrp groupname file更改文件拥有组
chown username:groupname file同时更改文件的拥有者和拥有组
chown丨chgrp -R user丨group dir更改目录本身及目录中内容拥有者或者拥有组
[root@localhost mnt]# chown linux file1
[root@localhost mnt]# chgrp shengchan file2
[root@localhost mnt]# chown linux.shengchan file3
[root@localhost mnt]# chown linux.shengchan /mnt/dir
[root@localhost mnt]# chown linux.shengchan /mnt/dir -R

在这里插入图片描述
在这里插入图片描述
练习:
新建目录:/sc /cw /js /pub
/sc 目录是生产部门的数据储存目录,只能被生产部门的人员读写
/cw 目录是生产部门的数据储存目录,只能被财务部门的人员读写
/js 目录是生产部门的数据储存目录,只能被技术部门的人员读写
/pub为公司人员的公共目录,可以被公司任何员工读写

[root@localhost mnt]# chgrp shengchan /mnt/sc
[root@localhost mnt]# chgrp caiwu /mnt/cw
[root@localhost mnt]# chgrp jishu /mnt/js
[root@localhost mnt]# chmod 777 /mnt/pub

在这里插入图片描述

六.特殊权限

stickyid 粘滞位

针对目录:如果一个目录stickyid开启,MAME这个目录中的文件只能被文件所有人删除
chmod 1原始权限 dir
chmod o+t dir

[root@localhost ~]# mkdir /pub
[root@localhost ~]# chmod 777 /pub
[root@localhost ~]# su - westos
[westos@localhost ~]$ touch /pub/westosfile
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - lee
[lee@localhost ~]$ touch /pub/leefile
[lee@localhost ~]$ rm -rf /pub/leefile 		#可以删除
[lee@localhost ~]$ rm -rf /pub/westosfile	#不属于自己的文件也可以删除 

如何解决此问题
chmod 1777 /pub
chmod o+t /pub
以上两条命令都可以开启pub目录的t权限

[root@localhost ~]# ls -ld /pub
drwxrwxrwx. 2 root root 6 Jan 30 11:19 /pub
[root@localhost ~]# chmod o+t /pub
[root@localhost ~]# ls -ld /pub
drwxrwxrwt. 2 root root 6 Jan 30 11:19 /pub
[root@localhost ~]# su - westos
[westos@localhost ~]$ touch /pub/westosfile
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - lee
[lee@localhost ~]$ touch /pub/leefile
[lee@localhost ~]$ rm -rf /pub/leefile 			#可以删除
[lee@localhost ~]$ rm -rf /pub/westosfile		 #不属于自己的文件不能删除
rm: cannot remove '/pub/westosfile': Operation not permitted

sgid 强制位

针对目录:目录中新建的文件自动归属到目录的所属组中
设定:
chmod 2源文件权限 dir
chmod g+s dir

[root@localhost ~]# mkdir /mnt/westosdir
[root@localhost ~]# chmod 777 /mnt/westosdir/
[root@localhost ~]# chgrp westos /mnt/westosdir/
[root@localhost ~]# touch /mnt/westosdir/file		#是谁建立的文件组就是谁的
[root@localhost ~]# chmod g+s /mnt/westosdir/
[root@localhost ~]# touch /mnt/westosdir/file1		#file1自动复制了/mnt/westosdir目录组

在这里插入图片描述
只针对二进制的可执行文件(c程序)
当运行二进制可执行文件时都是用文件拥有组身份运行,和执行用户无关

[root@localhost ~]# su - westos
[westos@localhost ~]$ /bin/watch -n 1 date
root     root     watchdog/0
root     root     watchdog/1
root     root     watchdogd
westos   westos   watch

[root@localhost ~]# chmod g+s /bin/watch
[root@localhost ~]# su - westos
[westos@localhost ~]$ /bin/watch -n 1 date
[westos@localhost ~]$ ps ax -o user,group,comm | grep watch
root     root     watchdog/0
root     root     watchdog/1
root     root     watchdogd
westos   root     watch

suid 冒险位

chmod 4原属性 file
chmod u+s file
只针对二进制的可执行文件(c程序)
当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关

[root@localhost ~]# su - westos
[westos@localhost ~]$ /bin/watch -n 1 date
root     root     watchdog/0
root     root     watchdog/1
root     root     watchdogd
westos   westos   watch

[root@localhost ~]# chmod u+s /bin/watch
[root@localhost ~]# su - westos
[westos@localhost ~]$ /bin/watch -n 1 date
[westos@localhost ~]$ ps ax -o user,group,comm | grep watch
root     root     watchdog/0
root     root     watchdog/1
root     root     watchdogd
root  	 westos      watch

七.acl权限列表

Aiccess Control Lists 访问控制列表

功能:

在列表中可以设定特殊用户对特殊文件有特殊权限

acl列表开启标识
-rw-r--r--. 1 westos westos 0 Jan 30 11:24 westosfile
          ^
         没有“+”代表acl列表未开启
-rw-rw-r--+ 1 root root 0 Jan 30 14:03 westosfile
          ^
         acl列表开启

acl列表权限读取

getfacl westosfile

显示内容分析:
# file: westosfile#文件名称
# owner: root#文件拥有者
# group: root#文件拥有组
user::rw-#文件拥有者权限
user:lee:rw-#特殊指定用户权限
group::r–#文件拥有组权限
group:westos:—#特殊指定的用户组的权限
mask::rw-#能够赋予特殊用户和特殊用户组的最大权限阀值
other::r–#其他人的权限
[root@localhost mnt]# touch westosfile
[root@localhost mnt]# ls -l westosfile 
-rw-r--r--. 1 root root 0 Jan 30 14:03 westosfile
[root@localhost mnt]# setfacl -m u:lee:rw westosfile 
[root@localhost mnt]# ls -l westosfile 
-rw-rw-r--+ 1 root root 0 Jan 30 14:03 westosfile
[root@localhost mnt]# getfacl westosfile 
# file: westosfile
# owner: root
# group: root
user::rw-
user:lee:rw-
group::r--
mask::rw-
other::r--

[root@localhost mnt]# setfacl -m u::rwx westosfile 
[root@localhost mnt]# getfacl westosfile 
# file: westosfile
# owner: root
# group: root
user::rwx
user:lee:rw-
group::r--
group:westos:rw-
mask::rw-
other::r--

注意:当文件权限列表开启,不要用 ls -l 的方式来读取文件的权限

acl列表的控制
setfacl -m u:lee:rw westosfile设定
setfacl -m g:westos:rw westosfile
setfacl -m u::rwx westosfile
setfacl -m g::0 westosfile
setfacl -x u:lee westosfile删除列表中的lee
setfacl -b westosfile关闭

acl权限优先级

拥有者>特殊指定用户>权限多的组>权限少的组>其他

acl mask 控制

mask是能够赋予指定用户权限的最大阀值

当设定完毕文件的acl列表之后用chmod缩小了文件拥有组的权力,mask会发生变化
恢复:

setfacl -m m:权限 文件

acl 列表的默认权限

setfacl -m u:lee:rwx /mnt/dir  	#只对于/mnt/dir目录本身生效
setfacl -Rm u:lee:rwx /mnt/dir	#对于/mnt/dir目录和目录中已经存在的内容生效
#以上命令对新建文件不会被设定
setfacl -m d:u:lee:rwx /mnt/dir	#针对于 /mnt/dir 目录中新建文件生效

八.attr权限

attr权限限制所有用户
i #不能作任何的更改
a #能添加不能删除

lsattr file 			#查看文件attr权限
lsattr -d dir			#查看目录attr权限
chattr +i|+a|-i|-a dir|file	#设定attr权限

[root@localhost mnt]# lsattr -d dir
[root@localhost mnt]# chattr +i dir
[root@localhost mnt]# touch dir/file
touch: setting times of 'dir/file': No such file or directory
[root@localhost mnt]# chattr -i dir
[root@localhost mnt]# touch dir/file
[root@localhost mnt]# ls dir/
file  
[root@localhost mnt]# chattr +a dir
[root@localhost mnt]# touch dir/file2
[root@localhost mnt]# ls dir/
file file2
[root@localhost mnt]# rm -rf dir/file2
rm: cannot remove 'dir/file2': Operation not permitted



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值