linux上机作业第六次,第六次上课作业

1、三种权限rwx对文件和目录的不同意义

对文件

r:read  可读取此文件的实际内容,如读取文本文件的文字内容等

w: write  可以编辑,新增或修改该文件的内容

x:execute  该文件具有可以被系统执行的权限==>>针对二进制文件或脚本

X:针对目录会加上x权限,如果是文件,本身没有执行权限,那么也不会加上执行权限,对目录不影响

d--------x 2 root test 14 Aug  4 14:15 1

d-------w- 2 root test 14 Aug  4 14:17 2

d------r-- 2 root test 14 Aug  4 14:17 3

-------r-- 1 root test  0 Aug  4 14:15 a

--------w- 1 root root  0 Aug  4 14:52 b

---------x 1 root test  0 Aug  4 14:17 c

[qiuwei@localhost qiuwei]$ cat a  ==>可以访问

[qiuwei@localhost qiuwei]$ cat b  ==>无法访问

cat: b: Permission denied

[qiuwei@localhost qiuwei]$ cat c ==>无法访问

cat: c: Permission denied

[qiuwei@localhost qiuwei]$ echo 13 >a ==>无法修改

bash: a: Permission denied

[qiuwei@localhost qiuwei]$ echo 13 >b ==>可以修改

[qiuwei@localhost qiuwei]$ echo 13 >c ==>无法修改

bash: c: Permission denied

[qiuwei@localhost qiuwei]$ rm -rf a ==>可以删除,因为上级目录含wx权限

[qiuwei@localhost qiuwei]$ rm -rf b ==>可以删除,因为上级目录含wx权限

[qiuwei@localhost qiuwei]$ rm -rf c ==>可以删除,因为上级目录含wx权限

[qiuwei@localhost qiuwei]$ ll

total 0

d--------x 2 root test 14 Aug  4 14:15 1

d-------w- 2 root test 14 Aug  4 14:17 2

d------r-- 2 root test 14 Aug  4 14:17 3

对目录:

x: execute  可以进入目录,可以访问目录中的文件,但是看不到里面有什么文件

r:read  进不去目录,可以看文件列表。不能访问目录内文件,文件的元数据不能查看

w: write 可以创建或删除目录中的文件,但是要匹配x权限

X:和x等价

假如对目录有写和执行,就可以删除里面的任何文件,里面文件u=,也一样

total 0

d--------x 2 root test 14 Aug  4 14:15 1

d-------w- 2 root test 14 Aug  4 14:17 2

d------r-- 2 root test 14 Aug  4 14:17 3

[qiuwei@localhost qiuwei]$ ll 1

ls: cannot open directory 1: Permission denied ==>x权限无法显示文件列表

[qiuwei@localhost qiuwei]$ ll 2

ls: cannot open directory 2: Permission denied ==>w权限无法显示文件列表

[qiuwei@localhost qiuwei]$ ll 3

ls: cannot access 3/a: Permission denied

total 0

?????????? ? ? ? ?            ? a   ==>r权限可以显示文件列表,但是看不到元数据

[qiuwei@localhost qiuwei]$ cd 1 ==>x权限可以进入到目录中

[qiuwei@localhost 1]$ cd ..

[qiuwei@localhost qiuwei]$ cd 2  ==>w权限无法进入到目录中

bash: cd: 2: Permission denied

[qiuwei@localhost qiuwei]$ cd 3  ==>r权限无法进入到目录中

bash: cd: 3: Permission denied

[qiuwei@localhost qiuwei]$ cat 1/c  ==>x权限可以访问目录下文件

[qiuwei@localhost qiuwei]$ cat 2/a  ==>w权限无法访问目录下文件

cat: 2/a: Permission denied

[qiuwei@localhost qiuwei]$ cat 3/a  ==>r权限无法访问目录下文件

cat: 3/a: Permission denied

[qiuwei@localhost qiuwei]$ rm -rf 1/c

rm: cannot remove ‘1/c’: Permission denied

[qiuwei@localhost qiuwei]$ rm -rf 2/a

rm: cannot remove ‘2/a’: Permission denied

[qiuwei@localhost qiuwei]$ rm -rf 3/a

rm: cannot remove ‘3/a’: Permission denied   无法删除创建文件

[qiuwei@localhost qiuwei]$ touch 1/b

touch: cannot touch ‘1/b’: Permission denied

[qiuwei@localhost qiuwei]$ touch 2/b

touch: cannot touch ‘2/b’: Permission denied

[qiuwei@localhost qiuwei]$ touch 3/b

touch: cannot touch ‘3/b’: Permission denied

2.umask和acl,mask的区别和联系

umask是针对属主属组other用户的,用来设置新建文件权限的掩码

umask的使用设定是掩码,即在文件或目录全部权限的基础上,除去umask的权限,得到文件或目录的权限,一般系统上使用unmask来设置新建文件的权限

ACL则是针对特殊用户和组的,不是属主属组和other用户的

ACL则是直接设置文件或目录的权限,不会影响原文件上的权限,ACL用户的权限独立,只受ACL设置权限限制

mask则是针对除去属主和other用户之外的其他用户

属组和ACL特殊权限用户的权限受到mask的限制,只能比mask低,如果某用户权限比mask高,也会被屏蔽,不会拥有此权限,有效权限只能和mask权限一样活着比mask低

3.三种特殊权限的应用场景和作用

SUID:s ===>只针对二进制文件可执行文件

在我们linux上,有很多重要文件权限设置的很低,不让一般用户随意访问,造成不必要的问题,例如/etc/shadow,权限为———,这个文件里面存放的是用户的密码等信息,及其重要,肯定不能让普通用户随意访问,但是我们普通用户也要为自己修改密码啊,所以SUID这个特殊权限就出现了,通俗讲s权限就是当一个文件的属主拥有s权限时,其他用户执行此文件后,进程属主将获得此文件属主的权限

在linux上我们通过执行passwd命令,来修改密码的,那么过程究竟是怎样的呢

passwd这个文件是有s权限的,使用普通用户执行passwd命令后,此普通用户暂时获得passwd属主(root)的权限,有了root用户的权限,修改shadow文件当然是没问题了

以上只是些文字描述,具体实际操作在上一篇博客中有,这里就不再做了

SGID:s  ===>只针对二进制文件可执行文件

如果上面SUID明白了,这个SGID就好理解了,和上面原理一样,

总结为:当我们执行一个属组拥有s权限的文件时,用户会暂时获得此文件属组的权限,并且以此权限去访问一些其他的文件

SGID作用在目录上时:

对目录加上g+s,任何人不管是不是目录所在组的成员,在里面创建文件时,该目录里面的文件属组继承目录的属组

即作用在目录上,将使在该目录中新建目录或文件将自动继承该目录所属组

Skity :t ==>针对于目录

当用户对一个目录拥有rw权限时,是可以删除目录里面的任何文件,无论该文件的权限或拥有者,为了避免这种情况,我们设置Skity,增加了此权限,只有文件的所有者或者root可以删除该文件

4.设置user1,使之新建文件权限为rw——–

操作如下

[root@localhost qiuwei]# su qiuwei ==>切换到普通用户qiuwe

[qiuwei@localhost qiuwei]$ umask 166 ==>设置umask为166

[qiuwei@localhost qiuwei]$ umask

0166

[qiuwei@localhost qiuwei]$ touch test ==>创建文件test

[qiuwei@localhost qiuwei]$ ll

total 0

d--------x 2 root   test   14 Aug  4 14:15 1

d------r-- 2 root   test   14 Aug  4 14:17 3

-rw------- 1 qiuwei qiuwei  0 Aug  4 16:29 test ==>文件权限为rw-------

小练习:

 1. 当用户xiaoming 对/testdir  目录无执行权限时,意味着无法

做哪些操作?

[root@localhost qiuwei]# su qiuwei

[qiuwei@localhost qiuwei]$ ll

total 0

drw-rw-rw- 2 root root 14 Aug  4 16:45 test

[qiuwei@localhost qiuwei]$ ll

total 0

drw-rw-rw- 2 root root 14 Aug  4 16:45 test

[qiuwei@localhost qiuwei]$ ll test/

ls: cannot access test/a: Permission denied

total 0

?????????? ? ? ? ?            ? a   ==>能看到目录里面的文件名,无法看到文件元数据

[qiuwei@localhost qiuwei]$ rm -rf test/a

rm: cannot remove ‘test/a’: Permission denied  ==>无法删除目录里面文件

[qiuwei@localhost qiuwei]$ cd test ==>无法进入到此目录

bash: cd: test: Permission denied

2. 当用户xiaoqiang 对/testdir  目录无读权限时,意味着无法做

哪些操作?

[qiuwei@localhost qiuwei]$ ll

total 0

d-wx-wx-wx 2 root root 14 Aug  4 16:45 test

[qiuwei@localhost qiuwei]$ cd test  ==>可以进入目录

[qiuwei@localhost test]$ cd ..

[qiuwei@localhost qiuwei]$ ll test/

ls: cannot open directory test/: Permission denied  ==>无法看到目录里面的文件

3.  当用户wangcai  对/testdir  目录无写权限时,该目录下的只

读文件file1 是否可修改和删除?

[qiuwei@localhost qiuwei]$ ll

total 0

dr-xr-xr-x 2 root root 14 Aug  4 16:45 test  ==>无写权限

[qiuwei@localhost qiuwei]$ cd test/

[qiuwei@localhost test]$ ll

total 0

-r--r--r-- 1 root root 0 Aug  4 16:45 a  ==>只读文件

[qiuwei@localhost test]$ rm -rf a

rm: cannot remove ‘a’: Permission denied ==>无法删除

4.  复制/etc/fstab 文件到/var/tmp 下,设置文件所有者为

wangcai 读写权限,所属组为sysadmins 组有读写权限,其他

人无权限

[root@localhost qiuwei]# cp /etc/fstab  /var/tmp/

[root@localhost qiuwei]# chmod 660 /var/tmp/fstab

[root@localhost qiuwei]# chown wangcai:sysadmins /var/tmp/fstab

[root@localhost qiuwei]# ll /var/tmp/fstab

-rw-rw---- 1 wangcai sysadmins 595 Aug  4 17:04 /var/tmp/fstab

5, 误删除了用户wangcai 的家目录,请重建并恢复该用户家目录

及相应的权限属性

[root@localhost home]# cp -r qiuwei  wangcai ==>复制配置文件

[root@localhost home]# ll

[root@localhost home]# chown -R wangcai:wangcai wangcai ==>递归更改属主和属组

[root@localhost home]# ll -a wangcai

total 32

drwx------   5 wangcai wangcai 4096 Aug  4 17:11 .

drwxr-xr-x. 18 root    root    4096 Aug  4 17:11 ..

-rw-r--r--   1 wangcai wangcai    0 Aug  4 17:11 3

-rw-------   1 wangcai wangcai 3390 Aug  4 17:11 .bash_history

-rw-r--r--   1 wangcai wangcai   18 Aug  4 17:11 .bash_logout

-rw-r--r--   1 wangcai wangcai  193 Aug  4 17:11 .bash_profile

-rw-r--r--   1 wangcai wangcai  231 Aug  4 17:11 .bashrc

drwxr-xr-x   3 wangcai wangcai   17 Aug  4 17:11 .cache

drwxr-xr-x   3 wangcai wangcai   17 Aug  4 17:11 .config

-rwxr-xr-x   1 wangcai wangcai    0 Aug  4 17:11 g

-rw-------   1 wangcai wangcai   40 Aug  4 17:11 .lesshst

drwxr-xr-x   4 wangcai wangcai   37 Aug  4 17:11 .mozilla

-rwxr-xr-x   1 wangcai wangcai    0 Aug  4 17:11 shabi

-rw-------   1 wangcai wangcai  596 Aug  4 17:11 .viminfo

6.在/data/testdir 里创建的新文件自动属于g1 组,组g2 的成

员如:alice 能对这些新文件有读写权限,组g3 的成员如

:tom 只能对新文件有读权限,其它用户(不属于

g1,g2,g3 )不能访问这个文件夹

drwxr-xr-x 2 root root 6 Aug  3 17:48 testdir

[root@localhost data]# chmod  g+s testdir ==>属组增加s权限

[root@localhost data]# chgrp g1 testdir  ==>属组更改为g1

[root@localhost data]# ll

total 0

drwxr-sr-x 2 root g1 6 Aug  3 17:48 testdir

[root@localhost data]# cd testdir/

[root@localhost testdir]# touch f1

[root@localhost testdir]# chmod 640 f1

[root@localhost testdir]# ll

total 0

-rw-r----- 1 root g1 0 Aug  4 17:24 f1 ==>新建文件属组为g1

[root@localhost testdir]# setfacl -m g:g2:rw f1

[root@localhost testdir]# setfacl -m g:g3:r f1

[root@localhost testdir]# su alice

[alice@localhost testdir]$ echo haha >f1 ==>alice用户可写

[alice@localhost testdir]$ cat f1  ==>alice用户可读

haha

[root@localhost testdir]# su tom

[tom@localhost testdir]$ ll

total 4

-rw-rw----+ 1 root g1 5 Aug  4 17:28 f1

[tom@localhost testdir]$ cat f1  ==>ton用户可读

haha

[tom@localhost testdir]$ echo 456 >f1 ==>tom用户无法写

bash: f1: Permission denied

[root@localhost testdir]# su qiuwei

[qiuwei@localhost testdir]$ cat f1 ==>  other无法读

cat: f1: Permission denied

[qiuwei@localhost testdir]$ echo 1111111 >f1  ==>other无法写

bash: f1: Permission denied

[qiuwei@localhost testdir]$

原创文章,作者:qiuwei,如若转载,请注明出处:http://www.178linux.com/28885

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值