Linux2.4chomd、chown、umask

文件或者目录权限chmod

  每个文件或者目录都有自己的权限。如下图从第二列开始九个权限位,划分为三段。分别为r=4 w=2 x=1,可以用数字表示。 rwx=7   rw-=6  --x=1   rw-r--r--=644

[root@chy002 tmp]# ls -l 1.txt
-rw-r--r--. 1 root root 0 10月 25 06:08 1.txt
[root@chy002 tmp]# rw-r--r--=600^C
[root@chy002 tmp]# chmod 666 1.txt
[root@chy002 tmp]# !l
ls -l 1.txt
-rw-rw-rw-. 1 root root 0 10月 25 06:08 1.txt

  我们可以发现,九个权限位后面有一个点。说明这个文件受制于selinux,如果selinux开启生效,创建的文件都有这样一个点。

[root@chy002 tmp]# getenforce
Disabled
[root@chy002 tmp]# touch 3.txt
[root@chy002 tmp]# ls -l 3.txt
-rw-r--r-- 1 root root 0 10月 25 06:15 3.txt

  将selinux关闭后发现没有这个点标识。

批量修改权限-R

[root@chy002 tmp]# chmod 777 chy         #修改目录权限
[root@chy002 tmp]# ls -l ./chy/1.txt           #目录内的文件权限并没有修改
-rw-r--r-- 1 root root 0 10月 25 06:18 ./chy/1.txt
[root@chy002 tmp]# ls -ld chy/                   #目录权限变化
drwxrwxrwx 2 root root 18 10月 25 06:18 chy/
[root@chy002 tmp]# chmod -R 770 chy         #-R批量修改该目录及以下所有权限
[root@chy002 tmp]# ls -l chy/
总用量 0
-rwxrwx--- 1 root root 0 10月 25 06:18 1.txt
[root@chy002 tmp]# ls -ld chy/
drwxrwx--- 2 root root 18 10月 25 06:18 chy/  

u  g   o   a,可以+   =  用减号的时候要注意,必须有该权限位才可以,比如  rw-   -   x  这样就不规范。

[root@chy002 tmp]# chmod u+x,g+w,o=r chy
[root@chy002 tmp]# chmod a+x chy
[root@chy002 tmp]# chmod a-x chy

更改所有者和所属组chown

[root@chy002 tmp]# ls -l 1.txt
-rw-rw-rw-. 1 root root 0 10月 25 06:08 1.txt
[root@chy002 tmp]# chown chy002 1.txt          #更改所有者
[root@chy002 tmp]# ls -l 1.txt
-rw-rw-rw-. 1 chy002 root 0 10月 25 06:08 1.txt
[root@chy002 tmp]# chgrp chy002 1.txt            #更改所属组
[root@chy002 tmp]# !l
ls -l 1.txt
-rw-rw-rw-. 1 chy002 chy002 0 10月 25 06:08 1.txt

  其中chown也可以更改所属组。

[root@chy002 tmp]# chown chy002:user chy/
[root@chy002 tmp]# ls -ld chy/
drw-rw-r-- 2 chy002 user 18 10月 25 06:18 chy/

  -R可以修改文件下所有文件所属关系

[root@chy002 tmp]# chown -R user:chy002 /tmp/chy/
[root@chy002 tmp]# ls -ld chy
drw-rw-r-- 2 user chy002 18 10月 25 06:18 chy
[root@chy002 tmp]# ls -ls chy/1.txt
0 -rwxrwx--- 1 user chy002 0 10月 25 06:18 chy/1.txt

umask

  在默认情况下,目录权限为755,普通文件权限为644。这个值是由umask所决定。如果没有x权限,目录是不能够打开浏览。

[root@chy002 tmp]# mkdir chy2
[root@chy002 tmp]# touch chy2.txt
[root@chy002 tmp]# ls -lst
总用量 4
0 -rw-r--r--  1 root   root     0 10月 25 06:45 chy2.txt
0 drwxr-xr-x  2 root   root     6 10月 25 06:45 chy2

  若用户建立为普通文件,预设是没有可执行权限的。只有rw两个权限,最大为666.

  若用户建立为目录,最大权限为777.

不要用数字去直接减umask。如果umask为003怎么办?

rwxrwxrwx - -------wx = rwxrwxr--      空位减去x仍然是空位。

umask可以去配置文件/etc/bashrc中修改

原文地址:https://www.cnblogs.com/chyuanliu/p/7726329.html