Linux用户权限

基本权限UGO

文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

权限对象:
属主------->u ----->r w x  所有者
属组------->g ----->r w x  所属组
其他人----->o------>r w x	
基本权限类型:
读(read):r   ---->4
写(write):w  ---->2
执行(exec):x ---->1

案例:

r w x        rw-        r--       alice   hr    file1.txt
属主权限    属组权限   其他人权限     属主    属组      文件

#前提条件:jack属于hr组
一  alice对file1.txt文件有什么权限?
二  jack对file1.txt文件有什么权限?
          a. jack是所有者吗?
          b. jack属于hr组吗?
          
三 tom对file1.txt文件有什么权限? 
          a. tom是所有者吗?
          b. tom属于hr组吗?
          c. tom为其他人吗? 
设置权限
chown:改变文件或目录的所属主以及所属组
chmod:为文件或目录设置访问权限

更改文件的属主(拥有者)、属组 (所属组)

chown:

[root@sxw ~]# chown alice.hr file1.txt  #修改属主、属组
[root@sxw ~]# chown tom  file1.txt  #修改属主
[root@sxw ~]# chown .it file1.txt   #只改属组
[root@sxw ~]# chown -R alice.hr dir1 #递归修改---针对目录
更改权限
[root@sxw ~]# chmod u+x file1.txt     #属主增加执行
[root@sxw ~]# chmod a=rwx file1.txt   #所有人等于读写执行
[root@sxw ~]# chmod a=- file1.txt     #所有人都没有权限
[root@sxw ~]# chmod ug=rw,o=r file1.txt  #属主属组等于读写,其他人只读
[root@sxw ~]# ll
-rw-rw-r--. 1 tom   it      0 Nov  1 15:30 file1.txt
[root@sxw ~]# chmod 644 file1.txt 
[root@sxw ~]# ll file1.txt 
-rw-r--r--. 1 tom it 0 Nov  1 15:30 file1.txt

[root@sxw ~]# chmod 755 file1.txt
[root@sxw ~]# ll
-rwxr-xr-x  1 root root    0 Jul 23 22:40 file1.txt

[root@sxw ~]# chmod 521 file1.txt
[root@sxw ~]# ll
-r-x-w---x  1 root root    0 Jul 23 22:40 file1.txt
权限案例 UGO

r、w、x权限对文件和目录的意义

对文件:
r ----cat vim  tail  head  more  less
w ---vi、vim  echo  >   >>
x ---- bash /dir/file  注意:如果没有x权限,root用户都无法执行  chmod a-x file    ./file

其他人对文件只有写权限的时候

对目录:
r  -----ls 			如果只有r权限,可以看到目录下的内容,但是有报错信息
w  -----touch、rm	可以对目录下的文件创建和删除但是要有执行权限
x  ---- cd 			进入目录
实战
rwx对文件的影响

实战案例1:rwx对文件的影响

[root@sxw ~]# vim /home/file1
date
[root@sxw ~]# ll /home/file1 
-rw-r--r--. 1 root root 5 Nov  3 15:19 /home/file1

[root@sxw ~]# su - sxw  #切换普通用户
[sxw@sxw ~]$  cat /home/file1 
date
[sxw@sxw ~]$  /home/file1   #执行文件
-bash: /home/file1: Permission denied
[sxw@sxw ~]$  exit
logout
[root@sxw ~]# chmod o+x /home/file1
[sxw@sxw ~]$  /home/file1 
Sun Nov  3 15:26:21 CST 2019

[root@sxw ~]# chmod o+w /home/file1 
[sxw@sxw ~]$  vim /home/file1
date
123
ls

rwx对目录的影响

实战案例2:对目录没有w,对文件有rwx

[root@sxw ~]# mkdir /dir10
[root@sxw ~]# touch /dir10/file1
[root@sxw ~]# chmod 777 /dir10/file1 
[root@sxw ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 19 Nov  3 15:37 /dir10/
[root@sxw ~]# ll /dir10/file1 
-rwxrwxrwx. 1 root root 0 Nov  3 15:37 /dir10/file1
[root@sxw ~]# vim /dir10/file1
jack
[root@sxw ~]# su - sxw
Last login: Sun Nov  3 15:28:06 CST 2019 on pts/0
[sxw@sxw ~]$  cat /dir10/file1 
jack
[sxw@sxw ~]$  rm -rf /dir10/file1   #权限不够
rm: cannot remove ‘/dir10/file1’: Permission denied
[sxw@sxw ~]$  touch /dir10/file2   #权限不够
touch: cannot touch ‘/dir10/file2’: Permission denied

其他人对父目录没有w权限,则对其目录下的文件也没有w权限,无关乎文件的权限,但是目录又要执行权限,否则无法cd进去

实战案例3:对目录有w,对文件没有任何权限

[root@sxw ~]# chmod 777 /dir10/
[root@sxw ~]# chmod 000 /dir10/file1 
[root@sxw ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 19 Nov  3 15:38 /dir10/
[root@sxw ~]# ll /dir10/file1 
----------. 1 root root 5 Nov  3 15:38 /dir10/file1
[root@sxw ~]# su - sxw   #切换普通用户
Last login: Sun Nov  3 15:38:53 CST 2019 on pts/0
[sxw@sxw ~]$  cat /dir10/file1 
cat: /dir10/file1: Permission denied    #没有权限
[sxw@sxw ~]$  rm -rf /dir10/file1 
[sxw@sxw ~]$  touch /dir10/file2

#小结
对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
注意事项
文件: x 权限小心给予
目录: w 权限小心给予

特殊权限

umask 用户掩码

控制用户创建文件和目录的默认权限

#root用户默认最高权限
目录---777 文件---666

#查看umask
[root@sxw ~]# umask
0022 root账户默认
0002 普通用户默认

#通过计算得出root用户创建目录和文件的权限为:
也是现在root用户创建完目录和文件的默认权限:
目录:755
文件:644

#修改umask
[root@sxw ~]# umask 0111

高级权限

suid普通文件	#二进制可执行文件普通文件,让普通用户和root用户一样执行命令
#提权
[root@sxw ~]# chmod u+s /usr/bin/cat
[root@sxw ~]# chmod 4777 file 
#取消提权
[root@sxw ~]# chmod u-s /usr/bin/cat

sgid	#目录,继承父目录属组的权限
[root@sxw ~]# chown .hr /opt/hr/  #设置属组
[root@sxw ~]# chmod g+s /opt/hr/
[root@sxw ~]# chmod 2770 dir 

sticky	#目录,当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除
[root@sxw ~]# chmod o+t /opt/hr/
[root@sxw ~]# chmod 1770 dir 

这三个权限不能给到同一个文件
suid ==== 4 提权 (只对二进制命令文件生效,其他不管用)
sgid ==== 2 继承属组权限    (只能对目录设置)
sticky == 1 (t权限)  权限控制
vim /etc/sudoers   | visudo
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL	#yy p 
sxw    ALL=(ALL)       NOPASSWD:ALL   #添加内容 不输入密码 放开全部权限
alice   ALL=(ALL)       NOPASSWD:/usr/bin/mkdir, /usr/bin/rm, /usr/bin/touch	#放开个别命令root权限

%whell 	ALL=(ALL)	NOPASSWD:ALL

visudo -c   #解析是否有语法错误
#将普通用户加入到wheel组
usermod -G wheel  zhangsan
gpasswd -a lhg,mch wheel

[sxw@sxw ~]$ sudo mkdir /test1
访问控制权限
setfacl 针对个人设置权限  一个人查看一个文件的权限
getfacl 查看权限
setfacl -m u:xiaoming:rwx /tmp/a.txt
-m 设置facl权限
u:  用户,也可以指定组 g
xiaoming: 需要指定的用户
rwx: 权限
#那么如何收回权限呢
方法1、
setfacl -m u:xiaoming:--- a.txt
方法2、
setfacl -x u:xiaoming a.txt
方法3:
setfacl -b file
我们也可以设置该文件为所有人所有组访问
setfacl -m ::rwx a.txt

隐藏权限

防止root用户误删
lsattr *   #查看文件属性
chattr	#设置隐藏权限
chattr +i 文件	#不允许做任何操作
chattr +a 文件	#只允许追加不允许修改删除
chattr +A 文件	#固定最近访问时间

  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值