2.14 文件和目录权限chmod 2.15 更改全部者和所属组chown 2.16 umask 2.17 隐藏权限lsattr/chattr

chmodlinux

一个文件有三个权限位  算法

第2位日后看9位是权限 bash

Readeble 可读spa

Writable 可写命令行

Executable 可执行code

(“r”表示可读)  ( “w”表示可写 )    ( “x”表示可执行)  ( “-”表示不可读、不可写、不可执行)ip

 rw-(全部者)   r--(所属组)  r--(其余用户组)  共九位ci

权限用数字表示: r=4   w=2   x=1    同步

举例:rwx=7    rw-=6    --x=1    rw-r--r--=644    rw-r-xr-x=655it

表示: rw- =6 --x =1 -w- =2 组合表示: rwxrwxrwx=777 rw-rw-rw-=666 -wx-wx-wx=333

 

一、命令语法:
chmod xxx 文件名  如:#chmod 700 3.txt

二、命令描述:
chmod = change mode   用于改变用户对文件/目录的读写执行权限。

三、命令选项:
-R 选项的做用等同于chown命令的-R选项,也表示级联更改。

**知识点:注意:在Linux系统中,一个目录的默认权限为755,而一个文件的默认权限为644。 **

**知识点:chmod  a(所有) u(全部者) g(所属组) o(其余用户组) + – (增长或者取消) **

**知识点:若是在selinux开启的环境下,那么所建立的目录或文件权限后面都会有一个点 ‘.’,说明这个目录或文件受制于selinux;如:**

实例: 修改3.txt文件权限

[root@cham2 tmp]# ls -l
总用量 8
-rw-r--r--  1 root root   2 10月 24 21:21 2.txt
-rw-r--r--  1 root root   0 10月 25 15:24 3.txt
drwxrwx---  2 root root  19 10月 24 13:02 cham2
drwxr-xr-x  4 root root  28 10月 24 12:58 chamlinux
-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJk
drwx------  3 root root  17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
-rw-------. 1 root root   0 10月 19 06:55 yum.log
[root@cham2 tmp]# chmod 700 3.txt
[root@cham2 tmp]# ls -l 3.txt
-rwx------ 1 root root 0 10月 25 15:24 3.txt
[root@cham2 tmp]#

把目录下的 子文件 子目录 所有批量的修改权限,能够加一个-R选项

实例: 使用-R选项,把cham2权限修改成770,。

[root@cham2 tmp]# chmod -R 770 cham2
[root@cham2 tmp]# ls -ld cham2
drwxrwx--- 2 root root 19 10月 25 15:41 cham2
[root@cham2 tmp]# ls -l cham2/
总用量 0
-rwxrwx--- 1 root root 0 10月 25 15:41 1.txt

chmod  a(所有) u(全部者) g(所属组) o(其余用户组) + – (增长或者取消)

实例:

[root@cham2 tmp]# chmod u=rwx,g=r,o=r cham2
[root@cham2 tmp]# ls -ld cham2
drwxr--r-- 2 root root 19 10月 25 15:41 cham2
[root@cham2 tmp]# chmod a+x cham2
[root@cham2 tmp]# ls -ld cham2
drwxr-xr-x 2 root root 19 10月 25 15:41 cham2
[root@cham2 tmp]# chmod a-x cham2
[root@cham2 tmp]# ls -ld cham2
drw-r--r-- 2 root root 19 10月 25 15:41 cham2
[root@cham2 tmp]# chmod u-x cham2
[root@cham2 tmp]# ls -ld cham2
drw-r--r-- 2 root root 19 10月 25 15:41 cham2
[root@cham2 tmp]# chmod o+x cham2
[root@cham2 tmp]# ls -ld cham2
drw-r--r-x 2 root root 19 10月 25 15:41 cham2

 

chown

一、命令语法:

#chown  cham  /tmp/yum.log 

chown -R username:group filename 

二、命令描述:
chown = change owner   命令chown 能够更改文件的全部者 也可改所属组。

三、命令选项:
-R R 选项只适用目录,做用是级联更改,即不只更改当前目录,连目录里的目录或者文件也所有更改。

实例:更改yum.log的全部者

[root@cham2 tmp]# ls -l /tmp/yum.log
-rw-------. 1 root root 0 10月 19 06:55 /tmp/yum.log
[root@cham2 tmp]# chown cham /tmp/yum.log 
[root@cham2 tmp]# !ls
ls -l /tmp/yum.log
-rw-------. 1 cham root 0 10月 19 06:55 /tmp/yum.log

chgrp

chgrp=change group的缩写

实例:更改所属组

[root@cham2 tmp]# chgrp    change group  ^C
[root@cham2 tmp]# chgrp user1 /tmp/yum.log 
[root@cham2 tmp]# ls -l /tmp/yum.log
-rw-------. 1 cham user1 0 10月 19 06:55 /tmp/yum.log

实例:更改yum.log.的全部者和所属组

[root@cham2 tmp]# chown user1:cham /tmp/yum.log 
[root@cham2 tmp]# !ls
ls -l /tmp/yum.log
-rw-------. 1 user1 cham 0 10月 19 06:55 /tmp/yum.log

实例:只更改yum.log的所属组 (命令“:”前忽略全部者)

[root@cham2 tmp]# chown :root /tmp/yum.log 
[root@cham2 tmp]# !ls
ls -l /tmp/yum.log
-rw-------. 1 user1 root 0 10月 19 06:55 /tmp/yum.log

实例:-R联级更改/tmp/cham2 以及cham2的文件。

[root@cham2 tmp]# chown -R user1:cham /tmp/cham2
[root@cham2 tmp]# ls -l /tmp/cham2/
总用量 0
-rwxrwx--- 1 user1 cham 0 10月 25 15:41 1.txt
[root@cham2 tmp]# ls -ld /tmp/cham2/
drw-r--r-x 2 user1 cham 19 10月 25 15:41 /tmp/cham2/

linux文件的解释

ls

 -l中显示的内容以下:

-rwxrw-r‐-1 root root 1213 Feb 2 09:39 abc

- 10个字符肯定不一样用户能对文件干什么

- 第一个字符表明文件(-)、目录(d),连接(l)

- 其他字符每3个一组(rwx),读(r)、写(w)、执行(x)

- 第一组rwx:文件全部者的权限是读、写和执行

- 第二组rw-:与文件全部者同一组的用户的权限是读、写但不能执行

- 第三组r--:不与文件全部者同组的其余用户的权限是读不能写和执行

也可用数字表示为:r=4,w=2,x=1  所以rwx=4+2+1=7

- 1 表示链接的文件数

- root 表示用户

- root表示用户所在的组

- 1213 表示文件大小(字节)

- Feb 2 09:39 表示最后修改日期

- abc 表示文件名

 

 

 

umask

默认状况下umask是0022,目录的权限值为755,普通文件的权限值为644。

1.命令语法
umask xxx(这里的xxx表明3个数字)。

2.命令描述
命令umask用于改变文件的默认权限。
若是要查看umask的值,只要在命令行输入umask,而后回车便可。
QQ截图20171024111536.png
这里umask的预设值为0022,规则:

若用户创建普通文件,则预设没有可执行权限,只有r,w两个权限,最大值为666(-rw-rw-rw-)。 若用户创建目录,则预设全部权限均开放,即777(-rwxrwxrwx)。 umask计算法:

例如咱们把umask的值改成003,那么它建立的普通文件(最大值666)的权限是什么呢?
普通文件的最大值-umask的值=将要建立的普通文件权限
(rw-rw-rw-)-(-------wx)=rw-rw-r--
能够看出是 666-003=664。

例如咱们把umask的值改成003,那么它建立的目录(最大值777)的权限是什么呢?
目录文件的最大值-umask的值=将要建立的目录权限
(rwxrwxrwx)-(-------wx)=rwxrwxr--
能够看出777-003=774

**注意在计算umask的时候不能用数字表示,只能用字母。**

chattr

  chattr:设置隐藏权限change file attributes on a Linux file system

  lsattr:查看文件/目录的隐藏属性

1.命令语法
chattr [+-=][Asaci][文件或目录名] ,其中+,-,=分别表示增长,减小和设定。

2.命令描述
命令chattr(chage attribute)改变属性的意思

3.命令选项
A 增长该属性后,表示文件或目录的atime将不可修改。
s 增长该属性后,会将数据同步写入磁盘中。
a 增长该属性后,表示只能追加不能删除,非root用户不能设定该属性。
c 增长该属性后,表示自动压缩该文件,读取时会自动解压。
i 增长该属性后,表示文件不可删除,重命名,设定连接,写入以及新增数据。

lsattr

1.命令语法
lsattr [-aR] [文件/目录名]

2.命令描述
命令lsattr用于读取文件或者目录的特殊权限。

3.命令选项
-a 相似于ls的-a选项,即连同隐藏文件一同列出。
-R 连同子目录的数据一同列出。

实例:给11.txt文件  以及查看 lsattr 11.txt

#chattr +i 11.txt 

[root@cham2 ~]# chattr +i 11.txt
[root@cham2 ~]# touch 11.txt
touch: 没法建立"11.txt": 权限不够
[root@cham2 ~]# mv 11.txt 123.txt
mv: 没法将"11.txt" 移动至"123.txt": 不容许的操做
[root@cham2 ~]# rm -v 11.txt
rm:是否删除普通文件 "11.txt"?y
rm: 没法删除"11.txt": 不容许的操做
[root@cham2 ~]# head -n2 /etc/passwd > 11.txt
-bash: 11.txt: 权限不够

#chattr -i 11.txt  

#chattr +a 11.txt

[root@cham2 ~]# chattr +a 11.txt
[root@cham2 ~]# touch 11.txt
[root@cham2 ~]# ls
111  11.txt  123  12.txt  1.txt  22.txt  234  2.txt  anaconda-ks.cfg.1
[root@cham2 ~]# ls -l
总用量 16
drwxrwxr--  3 root root  113 10月 25 17:20 111
-rw-rw-r--  1 root root   70 10月 25 17:44 11.txt
drwxr-xr-x  2 root root    6 10月 25 16:39 123
-rw-r--r--  1 root root   65 10月 25 17:20 12.txt
-rw-r--r--  1 root root    0 10月 25 17:31 1.txt
-rw-r--r--  1 root root    0 10月 25 17:07 22.txt
drwxrwxr-x  2 root root    6 10月 25 16:41 234
-rwx------  1 root root 1008 10月 25 16:41 2.txt
-rw-------. 1 root root 1422 10月 19 07:00 anaconda-ks.cfg.1
[root@cham2 ~]# head -n2 /etc/passwd > 11.txt
-bash: 11.txt: 不容许的操做
[root@cham2 ~]# head -n2 /etc/passwd >> 11.txt
[root@cham2 ~]# rm -v 11.txt
rm:是否删除普通文件 "11.txt"?y
rm: 没法删除"11.txt": 不容许的操做
[root@cham2 ~]# head -n2 /etc/passwd > 11.txt

#chattr -a 11.txt

#lsattr 11.txt

[root@cham2 ~]# lsattr 11.txt
-----a---------- 11.txt
[root@cham2 ~]#

对于目录的做用与对于文件的做用相同

相关文章
相关标签/搜索