linux学习(八)chmod、chown、umask、lsattr、chattr

一、权限位

权限位分为两个部分,第一个部分是谁的权限,第二部分是权限是多少。其中第一个部分一般分为:用户,用户组,其他用户。第二部分分为r:读权限,w:写权限,x:执行权限。可读,可写,可执行的权限,用数字表示分别是:421。

用一段代码来说明:

[root@iZ25lzba47vZ ~]# ls -ld 1
drwxr-xr-x 3 root root 4096 Oct 24 22:01 1

rwxr-xr-x:表示权限位的值。

root root 分别代表文件的所有者,和所属组。那么他们的权限分别是多少呢?只需要把rwxr-xr-x每三个字母切割开来就是了:

root:rwx root用户的权限是可读可写可执行。

root:r-x root组织有读和执行的权限。

other:r-x 其他用户只有读和执行的权限。

上面这个目录1的权限用数字表示是:755。

可读,可写,可执行对目录的意义:

[ruanwenwu@iZ25lzba47vZ ~]$ mkdir test
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch /test/1.txt
touch: cannot touch ‘/test/1.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ touch test/1.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
1.txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod 666 test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
ls: cannot access test/1.txt: Permission denied
1.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
drw-rw-rw- 2 ruanwenwu ruanwenwu 4096 Oct 25 20:52 test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l test
ls: cannot access test/1.txt: Permission denied
total 0
-????????? ? ? ? ?            ? 1.txt
[ruanwenwu@iZ25lzba47vZ ~]$ cd test/
bash: cd: test/: Permission denied
[ruanwenwu@iZ25lzba47vZ ~]$ chmod 777 test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test/
1.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
drwxrwxrwx 2 ruanwenwu ruanwenwu 4096 Oct 25 20:52 test/

以上代码说明:

1.切换到ruanwenwu普通用户。

2.在家目录下建立test目录。

3.在test目录下简历1.txt文件。

4.修改test目录为644的权限。

5.ls test发现下面的1.txt为乱码,切提示“Permission denied”。这里说明,我们已经不能看到test目录下的1.txt了。

6.ls -ld test发现一切正常。说明有r的权限,目录就可以用ls -ld看的。

7.cd test/ 发现也是不允许的。

可执行,对于目录的意义,就是可不可以往目录里增减文件:

[ruanwenwu@iZ25lzba47vZ ~]$ chmod 555 test
[ruanwenwu@iZ25lzba47vZ ~]$ mv 2.txt test/
mv: cannot stat ‘2.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch 3.txt
[ruanwenwu@iZ25lzba47vZ ~]$ mv 3.txt test/
mv: cannot move ‘3.txt’ to ‘test/3.txt’: Permission denied

可读,可写,可执行对文件的意义比较好理解:

可读就是可以查看,可写就是可修改,可执行就是能执行。

二、chmod

chmod用来修改文件的权限位。

使用数字的方法修改权限(建议使用):

[ruanwenwu@iZ25lzba47vZ ~]$ chmod 555 test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:52 test/

使用针对用户的方法:

修改所有用户:

[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
-rw-rw-r-- 1 ruanwenwu ruanwenwu    0 Oct 25 20:59 3.txt
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:59 test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod a=rwx 3.txt 
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
-rwxrwxrwx 1 ruanwenwu ruanwenwu    0 Oct 25 20:59 3.txt
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:59 test

分别写:

[ruanwenwu@iZ25lzba47vZ ~]$ chmod u=rx,g=rwx,o=x 3.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
-r-xrwx--x 1 ruanwenwu ruanwenwu    0 Oct 25 20:59 3.txt
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:59 test

使用增加和减少的办法:

[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
-r-xrwx--x 1 ruanwenwu ruanwenwu    0 Oct 25 20:59 3.txt
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:59 test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod u+w 3.txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod g-w 3.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total 4
-rwxr-x--x 1 ruanwenwu ruanwenwu    0 Oct 25 20:59 3.txt
dr-xr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 25 20:59 test

三、chown

修改文件的所属者或者所属组。

修改文件的所有者:

[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 ruanwenwu root 4096 Oct 24 22:01 1
[root@iZ25lzba47vZ ~]# chown root 1
[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 root root 4096 Oct 24 22:01 1

修改文件的所属组:

[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 root root 4096 Oct 24 22:01 1
[root@iZ25lzba47vZ ~]# chown :ruanwenwu 1
[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 root ruanwenwu 4096 Oct 24 22:01 1

同时修改所有者和所属组:

[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 root root 4096 Oct 24 22:01 1
[root@iZ25lzba47vZ ~]# chown ruanwenwu:ruanwenwu 1
[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 ruanwenwu ruanwenwu 4096 Oct 24 22:01 1

如果目录下有文件需要修改权限,可以用-R参数,级联修改:

[root@iZ25lzba47vZ ~]# ls -ld 1
drw-rw-rw- 3 ruanwenwu ruanwenwu 4096 Oct 24 22:01 1
[root@iZ25lzba47vZ ~]# cd 1
[root@iZ25lzba47vZ 1]# ls
4
[root@iZ25lzba47vZ 1]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Oct 24 22:00 4
[root@iZ25lzba47vZ 1]# cd 4
[root@iZ25lzba47vZ 4]# ls
33.txt
[root@iZ25lzba47vZ 4]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 26  2017 33.txt
[root@iZ25lzba47vZ 4]# cd ..
[root@iZ25lzba47vZ 1]# cd ..
[root@iZ25lzba47vZ ~]# ls
1      1.ipt  5      Application  Document.pdf  npm-debug.log  ruanwenwu     syncwithgit.sh
1.cap  2      a.php  a.txt        iptables.bak  oneinstack     shellscripts
[root@iZ25lzba47vZ ~]# chown -R ruanwenwu.ruanwenwu 1
[root@iZ25lzba47vZ ~]# cd 1
[root@iZ25lzba47vZ 1]# ls -l
total 4
drwxr-xr-x 2 ruanwenwu ruanwenwu 4096 Oct 24 22:00 4
[root@iZ25lzba47vZ 1]# cd 4
[root@iZ25lzba47vZ 4]# ls -l
total 0
-rw-r--r-- 1 ruanwenwu ruanwenwu 0 Mar 26  2017 33.txt

四、umask

umask决定了我们默认建立一个文件或者目录的默认权限。

查看当前umask:

[root@iZ25lzba47vZ 4]# umask
0022

那么在当前0022的umask下新建目录或者文件的权限是多少呢?我们看看:

[root@iZ25lzba47vZ 4]# umask
0022
[root@iZ25lzba47vZ 4]# touch b.txt
[root@iZ25lzba47vZ 4]# ls -l b.txt 
-rw-r--r-- 1 root root 0 Oct 25 23:52 b.txt
[root@iZ25lzba47vZ 4]# mkdir 6
[root@iZ25lzba47vZ 4]# ls -ld 6
drwxr-xr-x 2 root root 4096 Oct 25 23:53 6

可见,文件是644,目录是755。那么,这两组权限是怎么算出来的呢?

正确的算法:

目录:rwxrwxrwx 减去 ----w--w- = rwxr-xr-x。

文件没有执行权限,为rw-r--r--所以就是644了。

修改umask:

[root@iZ25lzba47vZ 4]# umask 0003
[root@iZ25lzba47vZ 4]# umask
0003
[root@iZ25lzba47vZ 4]# touch 4.txt
[root@iZ25lzba47vZ 4]# ls -l 4.txt 
-rw-rw-r-- 1 root root 0 Oct 26 00:01 4.txt

五、lsattr,chattr

lsattr用来查看文件的隐藏权限。

chattr用来修改隐藏权限。

那么隐藏权限可以用来干嘛呢?比方说,你有一个文件谁都不让动,连root用户都不让动,也就是不让修改,删除等任何操作。这个时候就可以用到隐藏权限。常用的隐藏权限有i和a。

i属性的特征是不让修改,删除,也不能touch。a属性的特征是可以追加和touch,别的操作不允许。

添加i属性:

[root@iZ25lzba47vZ 4]# lsattr 4.txt
-------------e-- 4.txt
[root@iZ25lzba47vZ 4]# chattr +i 4.txt
[root@iZ25lzba47vZ 4]# lsattr 4.txt 
----i--------e-- 4.txt

现在我们尝试对它进行删除,修改,touch操作:

[root@iZ25lzba47vZ 4]# lsattr 4.txt 
----i--------e-- 4.txt
[root@iZ25lzba47vZ 4]# touch 4.txt
touch: cannot touch ‘4.txt’: Permission denied
[root@iZ25lzba47vZ 4]# cat 333 > 4.txt
-bash: 4.txt: Permission denied
[root@iZ25lzba47vZ 4]# mv 4.txt 44.txt
mv: cannot move ‘4.txt’ to ‘44.txt’: Operation not permitted
[root@iZ25lzba47vZ 4]# ls -l 4.txt
-rw-rw-r-- 1 root root 0 Oct 26 00:01 4.txt

刚才试了文件。如果是对一个目录添加i属性呢?

首先看一下,如何看目录的隐藏属性:

[root@iZ25lzba47vZ 1]# lsattr -d 4
-------------e-- 4
[root@iZ25lzba47vZ 1]# lsattr 4
-------------e-- 4/6
----i--------e-- 4/4.txt
-------------e-- 4/b.txt
-------------e-- 4/33.txt

发现,加-d属性是看目录的,如果不加-d,可以看到目录下所有文件的隐藏权限。

如果要看到子目录下的隐藏属性呢?

[root@iZ25lzba47vZ 1]# lsattr -R 4
-------------e-- 4/6

4/6:
-------------e-- 4/6/b.txt

----i--------e-- 4/4.txt
-------------e-- 4/b.txt
-------------e-- 4/33.txt

好了,现在我们给目录4添加i属性:

[root@iZ25lzba47vZ 1]# chattr +i 4
[root@iZ25lzba47vZ 1]# lsattr -d 4
----i--------e-- 4
[root@iZ25lzba47vZ 1]# ls
4
[root@iZ25lzba47vZ 1]# touch 5.txt
[root@iZ25lzba47vZ 1]# mv 5.txt 4/
mv: cannot move ‘5.txt’ to ‘4/5.txt’: Permission denied
[root@iZ25lzba47vZ 1]# mv 4 3/
mv: cannot move ‘4’ to ‘3/’: Operation not permitted
[root@iZ25lzba47vZ 1]# echo 3 >> 4/b.txt 
[root@iZ25lzba47vZ 1]# cat 4/b.txt 
3

通过上面这段代码我们发现,给目录加了i属性后,目录不能重命名,也不能往里面放新的文件。但是,并不妨碍我们修改里面的文件4/b.txt,但是不能删除:

[root@iZ25lzba47vZ 1]# rm -rf 4/b.txt
rm: cannot remove ‘4/b.txt’: Permission denied
[root@iZ25lzba47vZ 1]# echo 444 >> 4/b.txt 

我们再来看一下a属性:

[root@iZ25lzba47vZ 1]# lsattr 5.txt
-------------e-- 5.txt
[root@iZ25lzba47vZ 1]# chattr +a 5.txt 
[root@iZ25lzba47vZ 1]# lsattr 5.txt 
-----a-------e-- 5.txt
[root@iZ25lzba47vZ 1]# touch 5.txt 
[root@iZ25lzba47vZ 1]# echo 3 > 5.txt 
-bash: 5.txt: Operation not permitted
[root@iZ25lzba47vZ 1]# echo 3 >> 5.txt 
[root@iZ25lzba47vZ 1]# mv 5.txt 6.txt
mv: cannot move ‘5.txt’ to ‘6.txt’: Operation not permitted
[root@iZ25lzba47vZ 1]# rm -rf 5.txt 
rm: cannot remove ‘5.txt’: Operation not permitted

上面的代码说明,有a属性的文件,同样是不能删除,修改,但是可以追加和touch的。那么对于有a属性的目录呢?

[root@iZ25lzba47vZ 1]# lsattr -d 5
-------------e-- 5
[root@iZ25lzba47vZ 1]# chattr +a 5
[root@iZ25lzba47vZ 1]# lsattr -d 5
-----a-------e-- 5
[root@iZ25lzba47vZ 1]# ls
4  5  5.txt
[root@iZ25lzba47vZ 1]# mv 5.txt 5/
mv: cannot move ‘5.txt’ to ‘5/5.txt’: Operation not permitted

删除对应的隐藏属性:

[root@iZ25lzba47vZ 1]# chattr -a 5
[root@iZ25lzba47vZ 1]# lsattr -d 5
-------------e-- 5

同样,修改目录的隐藏属性,并不影响我们对目录下面的文件的修改。

坚持!
原文地址:https://www.cnblogs.com/doubilaile/p/7732402.html