king

Linux常用命令之权限管理命令chmod

king linux 2018-05-11 2105浏览 0

Linux常用命令之权限管理命令chmod

1chmod[change the permissions mode of a file] : /bin/chmod

语法: chmod [{ugo}{+-=}{rwx}] [文件或目录名] // 直观

      e.g.     chmod u+wx filename   所有者

            chmod g=rwx filename  所属组

                chmod o-x filename      其他人

      eg:    chmod g+w,o-r filename   

1.  [root@localhost tmp]# ls -l study.list  

2.  -rw-r--r--. 1 root root 0 May  9 18:56 study.list  

3.  [root@localhost tmp]# chmod u+x study.list  

4.  [root@localhost tmp]# ls -l study.list  

5.  -rwxr--r--. 1 root root 0 May  9 18:56 study.list  

[root@localhost tmp]#  

或者: chmod [mode=421] [文件或目录] // 推荐

权限所对数字:

      r-4

      w-2

      x-1

 

e.g.  rwxr-xr--    754

      rw-r-x--x   651

      rwxr-x-w-  752 



chmod 777 filename //设定文件为所有用户具有全部权限 

1.  [root@localhost tmp]# chmod 777  study.list  

2.  [root@localhost tmp]# ls -l study.list       

3.  -rwxrwxrwx. 1 root root 0 May  9 18:56 study.list  

[root@localhost tmp]#


-R  递归修改

chmod -R 777 testdir   //  修改目录testdir及其目录下文件为所有文件具有全部权限

附:useradd  名字  //添加用户

        su  username //可切换用户 

      退出: exit

 

总结:

R---读取权限

W---写入权限

X---执行权限 

 

 

2、chown[change file ownership]  /bin/chown

      chown [用户] [文件名或目录名]            //改变文件所有者,root才能做,前提是用户存在 

e.g.  chown nobody file1  #改变文件file1的所有者为nobody,nobody为系统默认存在的用户。

 

附-添加用户:

      1useradd afang

      2passwd afang  #为其设置密码

 

3chgrp [change file group ownership ]  /bin/chgrp

      chgrp [用户组] [文件或目录 #改变文件或目录的所属组

            #必须为系统已经存在的组

 

E.g. chgrp adm file1  #改变文件file1的所属组为adm 

      #其中adm是系统中已经有的一个组。

附:groupadd 组名  //添加组  

 

4umask     /bin/umask       //显示、设置文件的缺省权限

      umask [-S] //-S rwx形式显示新建文件或目录缺省权限,更直观一些 

 

e.g.  umask  //查看默认权限,比较传统

            #显示 0022 0-特殊权限位,022-用户权限位 --- -w- -w-

那么缺省文件或目录权限怎么计算:

首先在linux在默认创建的文件没有可执行权限,即没有x

计算:       777 - 022  =  755  // 这是创建目录的默认权限

            666 - 022 = 644  // 是创建文件的默认权限,都没有x,文件权限都比目录权限少一个X

 

附1:很多UNIX系统中并没有-S这个选项,所以只能按照上面计算!

附2-Linux权限规则:默认创建的文件,不能授予可执行权限(X)!好处是屏蔽了很多攻击与病毒。

附3-改变缺省权限值:umask [掩码值]  注意umask的值可以更改,但不推荐

 


继续浏览有关 Linuxchmod 的文章
发表评论