Linux学习
一、文件和目录权限chmod
二、更改所有者和所属组chown
三、umask
四、隐藏权限lsattr/chattr
一、文件和目录权限chmod
1、r=4、w=2、x=1
[root@01 ~]# ls -l
total 20
-rw-r--r--. 1 root root 11397 Sep 13 09:49 1.txt //1.txt的权限:6、4、4=rw-=4+2+0、r--=4+0+0、r=4+0+0
drwxr-xr-x. 3 root root 15 Sep 13 08:47 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]#
2、一个文件有三个位:第一个位是user;第二个位是group;第三个位是other;rwx是指该文件相对于user、group和other是否具有读、写、执行权限;rw-是文件所有者的权限(r:read、w:write、-:不可执行;)。
[root@01 ~]# ll
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rw-r--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 root root 28 Sep 16 01:40 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]# chmod u=rwx,g=r,o=r 1.txt
[root@01 ~]# ll
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rwxr--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 root root 28 Sep 16 01:40 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]#
3、三个位之后的“.”表示该文件受限于selinux;rw-r--r--:第一个是所有者的权限,第二个是所属组的权限,第三个是other的权限;r=4 w=2 x=1 rwx=7
[root@01 ~]# getenforce
Disabled
[root@01 ~]# mkdir 12345
[root@01 ~]# ls
12345 1.txt 2 3 4 anaconda-ks.cfg passwd
[root@01 ~]# ls -l
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rw-r--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxr-xr-x. 3 root root 15 Sep 13 08:47 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]#
3、chmod=change mode;chmod -R继承功能(递归),更改目录或者文件及子目录、子文件的权限。也可以写成 chmod a+rwx 2.txt = 777
[root@01 2]# pwd
/root/2
[root@01 2]# ll
total 0
drwxr-xr-x. 2 root root 6 Sep 13 09:23 1
[root@01 2]# touch 1.txt
[root@01 2]# cd
[root@01 ~]# ll
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rw-r--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxr-xr-x. 3 root root 28 Sep 16 01:40 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]# chmod -R 777 2/
[root@01 ~]# ll
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rw-r--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 root root 28 Sep 16 01:40 2
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]# cd 2
[root@01 2]# ll
total 0
drwxrwxrwx. 2 root root 6 Sep 13 09:23 1
-rwxrwxrwx 1 root root 0 Sep 16 01:40 1.txt
[root@01 2]#
二、更改所有者和所属组chown
1、chown change owner。系统的所有用户都放在:/etc/passwd下面;系统的所有用户密码都放在:/etc/shadow下面;系统的所有组都放在:/etc/group/下面。chown user1 /tmp/yum.log 把/tmp/yum.log 文件所有者更改为user1
user1:x:1000:1000::/home/user1:/bin/bash
[root@01 ~]#
[root@01 ~]#
[root@01 ~]# ls -al /tmp/yum.log
-rw-------. 1 root root 0 Sep 11 16:47 /tmp/yum.log
[root@01 ~]# chown user1 /tmp/yum.log
[root@01 ~]# !l
ls -al /tmp/yum.log
-rw-------. 1 user1 root 0 Sep 11 16:47 /tmp/yum.log
[root@01 ~]#
2、chgrp更改文件或者目录的所属组
[root@01 ~]# chgrp user1 /tmp/yum.log
[root@01 ~]# ll /tmp/yum.log
-rw-------. 1 user1 user1 0 Sep 11 16:47 /tmp/yum.log
3、chown -R user1:user1 2 该目录下单文件也会被更改所有者和所属组(继承)
[root@01 ~]# chown -R user1:user1 2
[root@01 ~]# ll
total 20
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rwxr--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 user1 user1 28 Sep 16 01:40 2
-rwxrwxrwx 1 root root 0 Sep 16 01:50 2.txt
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]# cd 2
[root@01 2]# ll
total 0
drwxrwxrwx. 2 user1 user1 6 Sep 13 09:23 1
-rwxrwxrwx 1 user1 user1 0 Sep 16 01:40 1.txt
[root@01 2]#
三、umask
1、系统的umask值默认是0022;umask值是决定文件或目录权限的
[root@01 ~]# umask
0022
[root@01 ~]# touch 11.txt
[root@01 ~]# ll
total 20
-rw-r--r-- 1 root root 0 Sep 16 02:07 11.txt
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rwxr--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 user1 user1 28 Sep 16 01:40 2
-rwxrwxrwx 1 root root 0 Sep 16 01:50 2.txt
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]#
2、系统umask值:0022=022=----w--w-;目录权限:777=rwxrwxrwx - ----w--w-=rwxr-xr-x=755;文件权限:666=rw-rw-rw- - -----w--w=rw-r--r--=644
四、隐藏权限lsattr-chattr
1、设置隐藏权限的命令:chattr,查看隐藏权限的命令:lsattr。增加了i权限后文件不可做任何更改。
[root@01 ~]# chattr +i 1.txt
[root@01 ~]# lsattr
---------------- ./anaconda-ks.cfg
---------------- ./2
---------------- ./3
---------------- ./passwd
---------------- ./4
----i----------- ./1.txt //带i权限后无法删除。
---------------- ./12345
---------------- ./2.txt
---------------- ./11.txt
[root@01 ~]# ll
total 20
-rw-r--r-- 1 root root 0 Sep 16 02:07 11.txt
drwxr-xr-x 2 root root 6 Sep 16 01:35 12345
-rwxr--r--. 1 root root 11397 Sep 13 09:49 1.txt
drwxrwxrwx. 3 user1 user1 28 Sep 16 01:40 2
-rwxrwxrwx 1 root root 0 Sep 16 01:50 2.txt
drwxr-xr-x. 3 root root 15 Sep 11 09:57 3
drwxr-xr-x. 2 root root 6 Sep 11 09:57 4
-rw-------. 1 root root 1260 Sep 11 16:53 anaconda-ks.cfg
-rw-r--r--. 1 root root 846 Sep 13 08:45 passwd
[root@01 ~]# rm -rf 1.txt
rm: cannot remove ‘1.txt’: Operation not permitted //rm -rf也无法删除
[root@01 ~]# cat /etc/passwd > 1.txt
-bash: 1.txt: Permission denied //重定向也不可以
[root@01 ~]# cat /etc/passwd >> 1.txt
-bash: 1.txt: Permission denied //追加重定向也不可以
[root@01 ~]# chattr -i 1.txt //取消i权限
[root@01 ~]# lsattr
---------------- ./1.txt
[root@01 ~]#
2、chattr +a 权限,只能追加。chattr给一个目录加i或者a权限后,更改目录里的文件不会有限制;chattr -R +ia 1/ 递归更改目录及其内容的属性
[root@01 ~]# mkdir 66
[root@01 ~]# cd 66
[root@01 66]# touch 123.txt
[root@01 66]# touch 345.txt
[root@01 66]# cd
[root@01 ~]# chattr -R +ia 66/
[root@01 ~]# lsattr
----ia---------- ./66
[root@01 ~]# cd 66
[root@01 66]# !l
lsattr
----ia---------- ./123.txt
----ia---------- ./345.txt
[root@01 66]# chattr -i 123.txt
[root@01 66]# lsattr
-----a---------- ./123.txt
----ia---------- ./345.txt
[root@01 66]# cat /etc/passwd > 123.txt //不能重定向
-bash: 123.txt: Operation not permitted
[root@01 66]# cat /etc/passwd >> 123.txt //只能追加重定向
[root@01 66]#
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。