linux 文件权限中第一个,linux课题之文件系统权限

[TOC]

用户和群组

文件拥有者

群组:只有在组中文件才可用

其他人:其他组中的人

文件权限

bVbu7s0?w=271&h=162

drwxr-xr-x. 4 root root 31 Oct 4 00:54 zhanghao

drwxr-xr-x:

第一个字符d: 表示文件类型

d: 目录

- : 文件

l :链接文件

b:可随机存储设备

c: 键盘等一次性读取设备

第一组rwx: 表示文件所属用户的使用权限,rwx表示可读写可执行

第二组r-x: 表示用户组权限

第三组r-x:表示其他人权限

4: 表示有多少个文件链接到这个i-node

root root: 文件的所属主和组

时间: 文件创建时间可以使用ls -l --full-time显示可读时间

文件权限解答

例题:假设test1, test2, test3同属于testgroup这个群组,如果有下面的两个文件,请说明两个

文件的拥有者与其相关的权限为何?

-rw-r--r-- 1 root root 238 Jun 18 17:22 test.txt

-rwxr-xr-- 1 test1 testgroup 5238 Jun 19 10:25 ping_tsai

答:

文件test.txt的拥有者为root,所属群组为root。至于权限方面则只有root这个帐号可以存取此文件,其他人则仅能读此文件;文件拥有者test1[rwx]可以在本目录中进行任何工作;

另一个文件ping_tsai的拥有者为test1,而所属群组为testgroup。其中:test1 可以针对此文件具有可读可写可执行的权力;而同群组的test2, test3两个人与test1同样是testgroup的群组帐号,则仅可读可执行但不能写(亦即不能修改) ;至于没有加入testgroup这一个群组的其他人则仅可以读,不能写也不能执行!

而testgroup这个群组[r-x]的帐号,例如test2, test3亦可以进入本目录进行工作,但是不能

在本目录下进行写入的动作;

* 至于other的权限中[r--]虽然有r ,但是由于没有x的权限,因此others的使用者,并不能进

入此目录!

[zhanghao@zhanghao-server ~]$ cd /home/test1/

-bash: cd: /home/test1/: Permission denied

[zhanghao@zhanghao-server ~]$ ls

[zhanghao@zhanghao-server ~]$ exit

logout

[root@zhanghao-server test1]# su - test2

[test2@zhanghao-server ~]$ cd /home/test1/

-bash: cd: /home/test1/: Permission denied

[test2@zhanghao-server ~]$ ll /home/test1/

ls: cannot open directory /home/test1/: Permission denied

[test2@zhanghao-server ~]$ exit

logout

[root@zhanghao-server test1]# ll /home/

total 0

drwx------. 3 test1 test 95 Oct 4 03:54 test1

drwx------. 2 test2 test 83 Oct 4 03:55 test2

drwx------. 2 zhanghao zhanghao 83 Oct 4 03:55 zhanghao

从上面看,test1和test2虽然是属于同一个test组,但是/home下面目录对于组没有任何的权限,给组一个读权限

[root@zhanghao-server test1]# chmod 0611 /home/test1

[root@zhanghao-server test1]# su - test2

Last login: Thu Oct 4 03:55:27 EDT 2018 on pts/0

[test2@zhanghao-server ~]$ cd /home/test1/

[test2@zhanghao-server test1]$ ls

ls: cannot open directory .: Permission denied

从上面可以看出其他用户在test1目录下面没有任何的读写,没办法ls,touch。cd属于执行权限

[root@zhanghao-server home]# chmod 755 test1

[root@zhanghao-server home]# ll -h

total 0

drwxr-xr-x. 3 test1 test 95 Oct 4 03:54 test1

drwx------. 2 test2 test 83 Oct 4 03:55 test2

drwx------. 2 zhanghao zhanghao 83 Oct 4 03:55 zhanghao

[root@zhanghao-server home]# su - test2

Last login: Thu Oct 4 03:58:41 EDT 2018 on pts/0

[test2@zhanghao-server ~]$ cd /home/test1

[test2@zhanghao-server test1]$ ls

test

[test2@zhanghao-server test1]$ touch nihao

touch: cannot touch ‘nihao’: Permission denied

[test2@zhanghao-server test1]$ cd test/

[test2@zhanghao-server test]$ ls

nihao

[test2@zhanghao-server test]$ cat nihao o

test1

cat: o: No such file or directory

[test2@zhanghao-server test]$ cat nihao

test1

[test2@zhanghao-server test]$ vim nihao

-bash: vim: command not found

[test2@zhanghao-server test]$ vi nihao

vim 保存显示只读文件。无法touch,

修改文件权限

chgrp : 修改文件组

chown: 修改文件拥有者

chmod: 修改文件的权限,SUID,SGID,SBIT

chgrp

/zhanghao目录其他人权限为0,新建用户zhtest在zhanghao组中,可以有执行和读的权限,不能写。

[zhanghao@zhanghao-server home]$ touch zhanghao

[zhanghao@zhanghao-server home]$ exit

logout

[root@zhanghao-server ~]# su - test1

Last login: Thu Oct 4 03:53:52 EDT 2018 on pts/0

[test1@zhanghao-server ~]$ cd /home/

[test1@zhanghao-server home]$ ls

nihao test1 test2 zhanghao

[root@zhanghao-server /]# chmod 0750 /zhanghao/

[root@zhanghao-server /]# ll /

total 24

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin

dr-xr-xr-x. 4 root root 4096 Oct 3 04:00 boot

drwxr-xr-x. 19 root root 3160 Oct 4 02:29 dev

drwxr-xr-x. 80 root root 8192 Oct 4 03:55 etc

drwxr-xr-x. 5 root root 61 Oct 4 04:09 home

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib

lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64

drwxr-xr-x. 2 root root 6 Mar 10 2016 media

dr-xr-xr-x. 9 root root 4096 Jul 11 2017 mnt

drwxr-xr-x. 2 root root 6 Mar 10 2016 opt

dr-xr-xr-x. 106 root root 0 Oct 3 23:55 proc

dr-xr-x---. 3 root root 147 Oct 4 00:17 root

drwxr-xr-x. 26 root root 800 Oct 4 00:20 run

lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin

drwxr-xr-x. 2 root root 6 Mar 10 2016 srv

dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys

drwxrwxrwt. 11 root root 4096 Oct 4 03:26 tmp

drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr

drwxr-xr-x. 19 root root 267 Oct 3 03:55 var

drwxr-x---. 4 zhanghao zhanghao 31 Oct 4 00:54 zhanghao

[root@zhanghao-server /]# su - test1

Last login: Thu Oct 4 04:18:33 EDT 2018 on pts/0

[test1@zhanghao-server ~]$ ls

test

[test1@zhanghao-server ~]$ cd /zhanghao/

-bash: cd: /zhanghao/: Permission denied

[test1@zhanghao-server ~]$

[root@zhanghao-server /]# useradd zhtest -g zhanghao

[root@zhanghao-server /]# su - zhtest

[zhtest@zhanghao-server ~]$ cd /zhanghao/

[zhtest@zhanghao-server zhanghao]$ ls

test test1

[zhtest@zhanghao-server zhanghao]$ touch niaho

touch: cannot touch ‘niaho’: Permission denied

修改组权限

将/zhanghao目录的组权限修改为test,让test组下面的用户test1,有读和执行的权限。

[root@zhanghao-server /]# chgrp test /zhanghao

[root@zhanghao-server /]# ll -h

total 24K

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin

dr-xr-xr-x. 4 root root 4.0K Oct 3 04:00 boot

drwxr-xr-x. 19 root root 3.1K Oct 4 02:29 dev

drwxr-xr-x. 80 root root 8.0K Oct 4 04:21 etc

drwxr-xr-x. 6 root root 75 Oct 4 04:21 home

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib

lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64

drwxr-xr-x. 2 root root 6 Mar 10 2016 media

dr-xr-xr-x. 9 root root 4.0K Jul 11 2017 mnt

drwxr-xr-x. 2 root root 6 Mar 10 2016 opt

dr-xr-xr-x. 106 root root 0 Oct 3 23:55 proc

dr-xr-x---. 3 root root 147 Oct 4 00:17 root

drwxr-xr-x. 26 root root 800 Oct 4 00:20 run

lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin

drwxr-xr-x. 2 root root 6 Mar 10 2016 srv

dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys

drwxrwxrwt. 11 root root 4.0K Oct 4 03:26 tmp

drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr

drwxr-xr-x. 19 root root 267 Oct 3 03:55 var

drwxr-x---. 4 zhanghao test 31 Oct 4 00:54 zhanghao

[root@zhanghao-server /]# su - test1

Last login: Thu Oct 4 04:19:58 EDT 2018 on pts/0

[test1@zhanghao-server ~]$ cd /zhanghao/

[test1@zhanghao-server zhanghao]$ ls

test test1

[test1@zhanghao-server zhanghao]$ touch nihao

touch: cannot touch ‘nihao’: Permission denied

chown

修改文件拥有者

[root@zhanghao-server /]# chown -R root:root /zhanghao

[root@zhanghao-server /]# ll /

total 24

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin

dr-xr-xr-x. 4 root root 4096 Oct 3 04:00 boot

drwxr-xr-x. 19 root root 3160 Oct 4 02:29 dev

drwxr-xr-x. 80 root root 8192 Oct 4 04:21 etc

drwxr-xr-x. 6 root root 75 Oct 4 04:21 home

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib

lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64

drwxr-xr-x. 2 root root 6 Mar 10 2016 media

dr-xr-xr-x. 9 root root 4096 Jul 11 2017 mnt

drwxr-xr-x. 2 root root 6 Mar 10 2016 opt

dr-xr-xr-x. 107 root root 0 Oct 3 23:55 proc

dr-xr-x---. 3 root root 147 Oct 4 00:17 root

drwxr-xr-x. 26 root root 800 Oct 4 00:20 run

lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin

drwxr-xr-x. 2 root root 6 Mar 10 2016 srv

dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys

drwxrwxrwt. 11 root root 4096 Oct 4 03:26 tmp

drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr

drwxr-xr-x. 19 root root 267 Oct 3 03:55 var

drwxr-x---. 4 root root 31 Oct 4 00:54 zhanghao

chown -R 用户: 组 文件, 可以单纯的改组

[root@zhanghao-server /]# chown -R :test zhanghao

[root@zhanghao-server /]# ll -h

total 24K

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin

dr-xr-xr-x. 4 root root 4.0K Oct 3 04:00 boot

drwxr-xr-x. 19 root root 3.1K Oct 4 02:29 dev

drwxr-xr-x. 80 root root 8.0K Oct 4 04:21 etc

drwxr-xr-x. 6 root root 75 Oct 4 04:21 home

lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib

lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64

drwxr-xr-x. 2 root root 6 Mar 10 2016 media

dr-xr-xr-x. 9 root root 4.0K Jul 11 2017 mnt

drwxr-xr-x. 2 root root 6 Mar 10 2016 opt

dr-xr-xr-x. 107 root root 0 Oct 3 23:55 proc

dr-xr-x---. 3 root root 147 Oct 4 00:17 root

drwxr-xr-x. 26 root root 800 Oct 4 00:20 run

lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin

drwxr-xr-x. 2 root root 6 Mar 10 2016 srv

dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys

drwxrwxrwt. 11 root root 4.0K Oct 4 03:26 tmp

drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr

drwxr-xr-x. 19 root root 267 Oct 3 03:55 var

drwxr-x---. 4 root test 31 Oct 4 00:54 zhanghao

chmod

权限

r: 4

w:2

x:1

附加者

u: 所属主

g: 所属组

o: 其他人

chmod 755 filename

chmod u=rwx,go=rx filename

隐藏权限

umask

显示当前用户创建目录和文件夹的时候,默认的权限,用777减去umask值是默认的权限值,umask后面加上数字表示设置。

[root@zhanghao-server ~]# umask

0022

[root@zhanghao-server ~]# umask -S

u=rwx,g=rx,o=rx

chattr修改隐藏权限

[root@study ~]# chattr [+-=][ASacdistu] 文件或目录名称

选项与参数:

+ :增加某一个特殊参数,其他原本存在参数则不动。

- :移除某一个特殊参数,其他原本存在参数则不动。

= :设置一定,且仅有后面接的参数

A :当设置了 A 这个属性时,若你有存取此文件(或目录) 时,他的存取时间 atime 将不会被修改,可避免 I/O 较慢的机器过度的存取磁盘。(目前建议使用文件系统挂载参数处理这个项目)

S :一般文件是非同步写入磁盘的(原理请参考[前一章sync](../Text/index.html#sync)的说明) ,如果加上 S 这个属性时,当你进行任何文件的修改,该更动会“同步”写入磁盘中。

a :当设置 a 之后,这个文件将只能增加数据,而不能删除也不能修改数据,只有root 才能设置这属性

c :这个属性设置之后,将会自动的将此文件“压缩”,在读取的时候将会自动解压缩,但是在储存的时候,将会先进行压缩后再储存(看来对于大文件似乎蛮有用的!)

d :当 dump 程序被执行的时候,设置 d 属性将可使该文件(或目录) 不会被 dump 备份

i :这个 i 可就很厉害了!他可以让一个文件“不能被删除、改名、设置链接也无法写入或新增数据!”对于系统安全性有相当大的助益!只有 root 能设置此属性

s :当文件设置了 s 属性时,如果这个文件被删除,他将会被完全的移除出这个硬盘空间,所以如果误删了,完全无法救回来了喔!

u :与 s 相反的,当使用 u 来设置文件时,如果该文件被删除了,则数据内容其实还存在磁盘中,可以使用来救援该文件喔!

注意1:属性设置常见的是 a 与 i 的设置值,而且很多设置值必须要身为 root 才能设置

注意2:xfs 文件系统仅支持 AadiS 而已

范例:请尝试到/tmp下面创建文件,并加入 i 的参数,尝试删除看看。

[root@study ~]# cd /tmp

[root@study tmp]# touch attrtest <==创建一个空文件

[root@study tmp]# chattr +i attrtest <==给予 i 的属性

[root@study tmp]# rm attrtest <==尝试删除看看

rm: remove regular empty file `attrtest'? y

rm: cannot remove `attrtest': Operation not permitted

# 看到了吗?呼呼!连 root 也没有办法将这个文件删除呢!赶紧解除设置!

范例:请将该文件的 i 属性取消!

[root@study tmp]# chattr -i attrtest

文件特殊权限

SUID(set UID)(4)

SUID只能在二进制执行文件上生效,比如/usr/bin/passwd上面。普通用户执行具有SUID权限的二进制权限的时候,会临时具有root用户的超级权限。也就是说普通用户也是可以执行。

/usr/bin/passwd之所以需要s权限是因为普通用户修改密码后,写入shadow文件没有权限,只有root超级权限才可以网/etc/shadown里面写入配置。

SGID(2)

SGID对于目录和文件也有效, 程序执行需要用户有执行x权限。假如我用zhanghao用户执行,zhanghao用户的组是zhanghao,但是我要执行文件的用户组是haozi,那么我用zhanghao执行就会有haozi组的权限,对于目录和文件来说,有SGID权限,使用者可以对这个目录进行读和执行。

SBIT(1)

只是对于目录有效,对于文件已经没有任何效果。

使用者对于这个目录有w和x的权限

使用者在目录下面创建的目录,只有自己和root用户才可以删除。

比如/tmp目录。拥有SBIT目录,在里面创建的文件只有自己或者root权限才可以删除。

设置特殊权限

### SUID

$ chmod 4755

### SGID

$ chmod 2755

### SBIT

$ chmod 1755

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值