文件和目录权限chmod 、 更改所有者和所属组chown 、 umask 、隐藏权限lsattr/chattr

2.14 文件和目录权限chmod
[root@wangshuang-01 ~]# ls -l
总用量 4
-rw-------. 1 root root 3620 6月   5 22:50 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 5月  30 22:36 mtu
一个文件有3个权限位,r=4、w=2、x=1,读、写、执行
所有者权限:rw-r--r--=644
所属组权限:rw-r-xr-x=655
除所有者、所属组、其他用户权限:
chmod  change mode 更改文件的权限
chmode 700 /root/.ssh
第一列后面的点,和selinux开启和关闭有关系
selinux 开启  setenfore 0   显示点
selinux 关闭  getenfore    不显示点 
编辑文件  /etc/selinux/config
chmod 操作的权限 ,仅对文件或目录本身有效,
[root@wangshuang-01 tmp]# chmod 777 test
[root@wangshuang-01 tmp]# ls -l
drwxrwxrwx. 3 root root     15 6月   4 21:59 test
目录下文件的权限不会更改
[root@wangshuang-01 tmp]# ls -l test
总用量 0
drwxr-xr-x. 3 root root 28 6月   5 22:26 2
chmod -R ,操作的文件或目录会对目录或文件下的子目录或子文件都有效(批量更改)
[root@wangshuang-01 tmp]# chmod -R  777 test
[root@wangshuang-01 tmp]# ls -l test
总用量 0
drwxrwxrwx. 3 root root 28 6月   5 22:26 2
chmod u=rwx,g=r,o=r ;
u : user,  g : group,  o : other
[root@wangshuang-01 tmp]# chmod u=rwx,g=r,o=r test
[root@wangshuang-01 tmp]# ls -ld test
drwxr--r--. 3 root root 15 6月   4 21:59 test
chmod a+x test;给所有权限位+X权限
[root@wangshuang-01 tmp]# chmod a+x test
[root@wangshuang-01 tmp]# ls -ld test
drwxr-xr-x. 3 root root 15 6月   4 21:59 test
chmod a-x test;给所有权限位-X权限
[root@wangshuang-01 tmp]# chmod a-x test
[root@wangshuang-01 tmp]# ls -ld test
drw-r--r--. 3 root root 15 6月   4 21:59 test
chmod u/g/o-x test;给u/g/x权限位-X权限
2.15 更改所有者和所属组chown
chown  change owner   更改所有者、所属组
chown user1 /tmp/test/1.txt;更改1.txt文件的所有者
[root@wangshuang-01 tmp]# chown user1 /tmp/test/1.txt
[root@wangshuang-01 tmp]# ls -l /tmp/test
总用量 0
-rw-r--r--. 1 user1 root  0 6月   6 22:36 1.txt
drwxrwxrwx. 3 root  root 28 6月   5 22:26 2
chgrp change group ;更改1.txt文件的所属组
[root@wangshuang-01 tmp]# chgrp user1 /tmp/test/1.txt
[root@wangshuang-01 tmp]# ls -l /tmp/test
总用量 0
-rw-r--r--. 1 user1 user1  0 6月   6 22:36 1.txt
drwxrwxrwx. 3 root  root  28 6月   5 22:26 2
chown user2:user2 /tmp/test/1.txt ; 同时修改文件的所有者和所属组
[root@wangshuang-01 tmp]# chown user2:user2 /tmp/test/1.txt
[root@wangshuang-01 tmp]# ls -l /tmp/test
总用量 0
-rw-r--r--. 1 user2 user2  0 6月   6 22:36 1.txt
drwxrwxrwx. 3 root  root  28 6月   5 22:26 2
chown :root /tmp/test/1.txt;只修改所属组
[root@wangshuang-01 tmp]# chown :root /tmp/test/1.txt
[root@wangshuang-01 tmp]# ls -l /tmp/test
总用量 0
-rw-r--r--. 1 user2 root  0 6月   6 22:36 1.txt
drwxrwxrwx. 3 root  root 28 6月   5 22:26 2
chown -R /tmp/test;修改该目录下的子文件、目录的所有者、所属组
[root@wangshuang-01 tmp]# chown -R user1:user1 /tmp/test/
[root@wangshuang-01 tmp]# ls -l /tmp/test
总用量 0
-rw-r--r--. 1 user1 user1  0 6月   6 22:36 1.txt
drwxrwxrwx. 3 user1 user1 28 6月   5 22:26 2
2.16 umask
umask:决定文件和目录的权限的
[root@wangshuang-01 tmp]# umask
0022
[root@wangshuang-01 tmp]# umask 003
[root@wangshuang-01 tmp]# umask
0003
通过umask 判断文件、目录的权限是多少?
文件:666-003= (rw-rw-rw-)-(-------wx)=(rw-rw-r--)664
目录:777-003=(rwxrwxrwx)-(-------wx)=(rwxrwxr--)774
2.17 隐藏权限lsattrattr 
设置隐藏权限:chattr  
man chattr
chattr +i /tmp/test/1.txt;给1.txt加隐藏权限
[root@wangshuang-01 tmp]# chattr +i /tmp/test/1.txt
vi /tmp/test/1.txt  不可编辑,只读,不可touch、不能追加、不能删除
查看权限:lsattr 
[root@wangshuang-01 test]# lsattr 1.txt
----i----------- 1.txt
chattr -i 1.txt ;减掉  i  权限

chattr +a  1.txt;只能 追加,可以touch、不能删除、不能修改、不能重命名
[root@wangshuang-01 test]# head -n2 /etc/passwd > 1.txt
[root@wangshuang-01 test]# more 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
chattr  -a  1.txt;减掉 a 权限

给目录加  i  权限后,不能更改,不能创建子文件、子目录、不能删除
lsattr -R  /tmp;可以查看该目录下所有文件,全部列出来
lsattr  -a /tmp ; 可查看隐藏文件
lsattr -d /tmp;查看本身











猜你喜欢

转载自blog.csdn.net/wangshuang_2013/article/details/80603203
今日推荐