文件权限

文件权限

文件和目录权限chmod

文件和目录权限有 读r:4 写w: 2 执行 x: 1
-rw-r--r--. 1 root root 0 10月 26 00:44 test
上面权限后的一个点是由于开启了SELINUX 而创建的文件,(getenforece setenforce [0|1] /etc/selinux/config配置文件)
上面有三组权限设置,分别为:user用户 group组 other其他用户

[root@xujb01 mnt]# touch test
[root@xujb01 mnt]# ll test
-rw-r--r--. 1 root root 0 10月 26 00:44 test
--------------------------------------------------------
[root@xujb01 mnt]# chmod 777 test  #把权限全开 数字写法
[root@xujb01 mnt]# ll test
-rwxrwxrwx. 1 root root 0 10月 26 00:44 test  #
[root@xujb01 mnt]# chmod u=rx,g=rx,o=x test  #字母表示法
[root@xujb01 mnt]# ll test
-r-xr-x--x. 1 root root 0 10月 26 00:44 test
---------------------------------------------------------------
[root@xujb01 mnt]# chmod -r test;ll test
----------. 1 root root 0 10月 26 00:44 test
[root@xujb01 mnt]# chmod +x test;ll test   #用户 组 其他全加执行权限或者 chmod a+x test
---x--x--x. 1 root root 0 10月 26 00:44 test
[root@xujb01 mnt]# chmod +r test;ll test
-r-xr-xr-x. 1 root root 0 10月 26 00:44 test  #用户 组 其他全加读权限或者 chmod a+r test
[root@xujb01 mnt]# chmod +w test;ll test  #此种方法用在写上行不通只是用户加了写权限
-rwxr-xr-x. 1 root root 0 10月 26 00:44 test
[root@xujb01 mnt]# chmod a+w test;ll test #只能用a+w 模式
-rwxrwxrwx. 1 root root 0 10月 26 00:44 test

更改文件或文件夹所有者和所属组

用户可以查看cat /etc/passwd

[root@xujb01 mnt]# chown test test;ll test    #chown 修改所有者
-rwxrwxrwx. 1 test root 0 10月 26 00:44 test
[root@xujb01 mnt]# chgrp test test;ll test   #chgrp 修改所属组
-rwxrwxrwx. 1 test test 0 10月 26 00:44 test 
[root@xujb01 mnt]# chown root:root test;ll test          #chown 同时修改所有者和所属组
-rwxrwxrwx. 1 root root 0 10月 26 00:44 test
[root@xujb01 mnt]# chown :test test;ll test          #chown 只修改组
-rwxrwxrwx. 1 root test 0 10月 26 00:44 test
------------------------------------------------------------------
[root@xujb01 mnt]# ll -d testdir;ll testdir/test.txt
drwxr-xr-x. 2 root root 4096 10月 26 01:08 testdir
-rw-r--r--. 1 root root 0 10月 26 01:08 testdir/test.txt

[root@xujb01 mnt]# chown test testdir/;ll -d testdir;ll testdir/test.txt   #不加任何参数只修改i了单个文件夹的所有者
drwxr-xr-x. 2 test root 4096 10月 26 01:08 testdir
-rw-r--r--. 1 root root 0 10月 26 01:08 testdir/test.txt
[root@xujb01 mnt]# chown -R test01 testdir/;ll -d testdir;ll testdir/test.txt   #加上`-R`选项 递归设置文件夹和文件夹里的内容(文件夹和文件)
drwxr-xr-x. 2 test01 root 4096 10月 26 01:08 testdir
-rw-r--r--. 1 test01 root 0 10月 26 01:08 testdir/test.txt

umask

影响创建文件或者文件夹的默认权限
文件有x权限就可以执行,文件夹有x权限才可以进入
创建文件或者文件夹权限管理
文件满权限 666(rw-rw-rw-)
文件夹慢权限777(rwxrwxrwx)
那创建文件或者文件夹权限怎么计算呢?
首先查看系统umask值:

root@xujb01 mnt]# umask
0022

然后计算创建文件权限为:666-022 = 644(rw-r—r—) #而且文件第三位x不参加计算不管umask值里有没有只进行前2位(rw)的计算

[root@xujb01 mnt]# touch test.txt;ll test.txt
-rw-r--r--. 1 root root 0 10月 26 01:27 test.txt
--------------------------------------------------------------------
[root@xujb01 mnt]# umask 0023;umask   
0023    #计算忽略执行位(x)的就算,所以和0022的结果是一样的也是 (rw-r--r--)
[root@xujb01 mnt]# touch test1.txt;ll test1.txt
-rw-r--r--. 1 root root 0 10月 26 01:32 test1.txt

同理计算文件夹权限为:777-022=755(rwxr-xr-x) #所有位都参加计算

[root@xujb01 mnt]# mkdir testdir;ll -d testdir
drwxr-xr-x. 2 root root 4096 10月 26 01:30 testdir

隐藏权限lsattr/chattr

lsattr/chattr : 查看隐藏属性/设置隐藏属性
chattr +i [a] +i 属性为不可以修改 +a属性为可以追加,

[root@xujb01 mnt]# ll test.txt;lsattr test.txt          #普通文件只要用户有相应权限就可以进行读写执行
-rw-r--r--. 1 root root 0 10月 26 01:27 test.txt
-------------e-- test.txt
---------------------------------------------------------------
[root@xujb01 mnt]# chattr +i test.txt;ls test.txt;lsattr test.txt  #设置了`i` 属性,不可以修改
test.txt
----i--------e-- test.txt
[root@xujb01 mnt]# mv test.txt text01.txt              #不可以转移位置或改名字
mv: 无法将"test.txt" 移动至"text01.txt": 不允许的操作
[root@xujb01 mnt]# cp test.txt test01.txt
[root@xujb01 mnt]# ll test01.txt ;lsattr test01.txt        #可以复制,但是**复制不会复制隐藏属性**
-rw-r--r--. 1 root root 0 10月 26 02:01 test01.txt
-------------e-- test01.txt
[root@xujb01 mnt]# echo "hello world" >> test.txt
bash: test.txt: 权限不够
[root@xujb01 mnt]# echo "hello world" > test.txt          #不可以进行编辑
bash: test.txt: 权限不够

chattr +a #选项文件可以追加

[root@xujb01 mnt]# chattr +a test.txt;lsattr test.txt  
-----a-------e-- test.txt
[root@xujb01 mnt]# mv test.txt test02.txt                    #`+a`选项也不可以重命名
mv: 无法将"test.txt" 移动至"test02.txt": 不允许的操作

[root@xujb01 mnt]# echo "hello world" > test.txt   #不准覆盖
bash: test.txt: 不允许的操作
[root@xujb01 mnt]# echo "hello world" >> test.txt;cat test.txt  #可以追加
hello world
hello world

[root@xujb01 mnt]# rm test.txt               #不可以删除文件
rm:是否删除普通文件 "test.txt"?y
rm: 无法删除"test.txt": 不允许的操作

[root@xujb01 mnt]# ll test.txt;touch test.txt;ll test.txt   #可以使用touch修改文件修改时间 注:+i 选项是不能做此更改的
-rw-r--r--. 1 root root 24 10月 26 02:13 test.txt
-rw-r--r--. 1 root root 24 10月 26 02:14 test.txt

以上是对文件的设置,那么对文件夹又是怎么控制的呢?
首先+i 选项是和文件类似的不可以做任何更改,
然后+a选项为追加,所以可以在该文件夹touch 文件:

[root@xujb01 mnt]# mkdir testdir                            #`+I`选项不可以在文件夹内创建文件进行修改
[root@xujb01 mnt]# ll -d testdir/ ;lsattr -d testdir
drwxr-xr--. 2 root root 4096 10月 26 02:16 testdir/
-------------e-- testdir
[root@xujb01 mnt]# chattr +i testdir
[root@xujb01 mnt]# ll -d testdir/ ;lsattr -d testdir
drwxr-xr--. 2 root root 4096 10月 26 02:16 testdir/
----i--------e-- testdir
[root@xujb01 mnt]# touch testdir/test.txt
touch: 无法创建"testdir/test.txt": 权限不够

[root@xujb01 mnt]# touch testdir/test02                #可以进行touch新的文件但是不能删除(追加概念)
[root@xujb01 mnt]# rm testdir/test01
rm:是否删除普通空文件 "testdir/test01"?t
[root@xujb01 mnt]# rm testdir/test01
rm:是否删除普通空文件 "testdir/test01"?y
rm: 无法删除"testdir/test01": 不允许的操作
[root@xujb01 mnt]# rm testdir/test02
rm:是否删除普通空文件 "testdir/test02"?y
rm: 无法删除"testdir/test02": 不允许的操作
[root@xujb01 mnt]#

[root@xujb01 mnt]# vim testdir/test02        #可以对文件进行修改
root@xujb01 mnt]# cat testdir/test02
dadsf
[root@xujb01 mnt]# echo "hello world" > testdir/test02
[root@xujb01 mnt]# cat testdir/test02
hello world
[root@xujb01 mnt]#

chattr 同 chown相同有-R选项也为递归设置

[root@xujb01 mnt]# lsattr testdir
-------------e-- testdir/test01
-------------e-- testdir/test02~
-------------e-- testdir/4913
-------------e-- testdir/test02
[root@xujb01 mnt]# lsattr -d testdir
-----a-------e-- testdir
[root@xujb01 mnt]# lsattr testdir  #递归设置
----i--------e-- testdir/test01
----i--------e-- testdir/test02~
----i--------e-- testdir/4913
----i--------e-- testdir/test02
[root@xujb01 mnt]# lsattr -d testdir
----ia-------e-- testdir

posted on 2017-10-26 02:30  游荡的鱼  阅读(277)  评论(0编辑  收藏  举报

导航