欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

linux的chmod与chown

发布时间:2023/12/14 linux 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux的chmod与chown 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

linux的文件根据文件及目录的权限范围,包括:

  • u:USER,文件或目录的拥有者
  • g:Group,文件或目录的所属群组
  • o:Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围
  • a:all,全部的用户,包括拥有者、所属群组及其他用户

权限包阔:

  • r:读取权限,数字为4
  • w:写入权限,数字为2
  • x:执行或切换权限,代号为1

chmod的使用说明

SYNOPSIS

  •        chmod [OPTION]... MODE[,MODE]... FILE...
  •        chmod [OPTION]... OCTAL-MODE FILE...
  •        chmod [OPTION]... --reference=RFILE FILE...
  • DESCRIPTION
           This  manual  page  documents the GNU version of chmod.  chmod changes
           the file mode bits of each given file according to mode, which can  be
           either  a symbolic representation of changes to make, or an octal num‐
           ber representing the bit pattern for the new mode bits.

           The format of a symbolic mode is [ugoa...][[-+=][perms...]...],  where
           perms  is either zero or more letters from the set rwxXst, or a single
           letter from the set ugo.  Multiple symbolic modes can be given,  sepa‐
           rated by commas.

           A  combination of the letters ugoa controls which users' access to the
           file will be changed: the user who owns it (u),  other  users  in  the
           file's  group  (g),  other  users  not in the file's group (o), or all
           users (a).  If none of these are given, the effect is as if  (a)  were
           given, but bits that are set in the umask are not affected.

           The  operator  + causes the selected file mode bits to be added to the
           existing file mode bits of each file; - causes them to be removed; and
           =  causes  them  to be added and causes unmentioned bits to be removed
           except that a directory's unmentioned set user and group ID  bits  are
           not affected.

           The  letters rwxXst select file mode bits for the affected users: read
           (r),  write  (w),  execute  (or  search  for  directories)  (x),  exe‐
           cute/search  only  if  the  file is a directory or already has execute
           permission for some user (X), set user or group ID on  execution  (s),
           restricted deletion flag or sticky bit (t).  Instead of one or more of
           these letters, you can specify exactly one of  the  letters  ugo:  the
           permissions granted to the user who owns the file (u), the permissions
           granted to other users who are members of the file's  group  (g),  and
           the  permissions  granted to users that are in neither of the two pre‐
           ceding categories (o).

            

           A numeric mode is from one to four  octal  digits  (0-7),  derived  by
           adding  up  the  bits with values 4, 2, and 1.  Omitted digits are as‐
           sumed to be leading zeros.  The first digit selects the  set  user  ID
           (4)  and  set  group  ID (2) and restricted deletion or sticky (1) at‐
           tributes.  The second digit selects permissions for the user who  owns
           the file: read (4), write (2), and execute (1); the third selects per‐
           missions for other users in the file's group, with  the  same  values;
           and  the fourth for other users not in the file's group, with the same
           values.

           chmod never changes the permissions of symbolic links; the chmod  sys‐
           tem call cannot change their permissions.  This is not a problem since
           the permissions of symbolic links are never used.  However,  for  each
           symbolic  link  listed  on the command line, chmod changes the permis‐
           sions of the pointed-to file.  In  contrast,  chmod  ignores  symbolic
           links encountered during recursive directory traversals.

            

    SETUID AND SETGID BITS
           chmod  clears  the  set-group-ID  bit  of a regular file if the file's
           group ID does not match the user's effective group ID or  one  of  the
           user's supplementary group IDs, unless the user has appropriate privi‐
           leges.  Additional restrictions may cause  the  set-user-ID  and  set-
           group-ID  bits  of MODE or RFILE to be ignored.  This behavior depends
           on the policy and functionality of the underlying chmod  system  call.
           When in doubt, check the underlying system behavior.

           For  directories chmod preserves set-user-ID and set-group-ID bits un‐
           less you explicitly specify otherwise.  You can set or clear the  bits
           with  symbolic modes like u+s and g-s.  To clear these bits for direc‐
           tories with a numeric mode requires an  additional  leading  zero,  or
           leading = like 00755 , or =755

    RESTRICTED DELETION FLAG OR STICKY BIT
           The  restricted deletion flag or sticky bit is a single bit, whose in‐
           terpretation depends on the file type.  For directories,  it  prevents
           unprivileged  users  from removing or renaming a file in the directory
           unless they own the file or the directory;  this  is  called  the  re‐
           stricted  deletion  flag  for  the directory, and is commonly found on
           world-writable directories like /tmp.  For regular files on some older
           systems,  the bit saves the program's text image on the swap device so
           it will load more quickly when run; this is called the sticky bit.

    OPTIONSChange the mode of each FILE to MODE. With --reference, change themode of each FILE to that of RFILE.-c, --changeslike verbose but report only when a change is made-f, --silent, --quietsuppress most error messages-v, --verboseoutput a diagnostic for every file processed--no-preserve-rootdo not treat '/' specially (the default)--preserve-rootfail to operate recursively on '/'--reference=RFILEuse RFILE's mode instead of MODE values-R, --recursivechange files and directories recursively--help display this help and exit--versionoutput version information and exitEach MODE is of the form'[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

    chown的使用说明

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID;组可以是组名或者组ID;文件是以空格分开的要改变权限的文件列表,支持通配符。系统管理员经常使用chown命令,在将文件拷贝到另一个用户的名录下之后,让用户拥有使用该文件的权限。 

    chown 命令用于更改文件的所有者为指定用户,其语法格式如下:
     

    chown [选项] [ower:group] 文件...用户名:组名

    命令参数

    -c显示更改的部分的信息
    -f忽略错误信息
    -h修复符号链接
    -R处理指定目录以及其子目录下的所有文件
    -v显示详细的处理信息
    -deference作用与符号链接的指定,而不是链接文件本身

    例如:

    sudo chown cx:cx .file/ sudo chown cx:cx -R file/

    区别:

    第一、指令名称 : chmod

    使用权限 : 所有使用者

    使用方式 : chmod [-cfvR] [--help] [--version] mode file...

    说明 : Linux/Unix 的档案存取权限分为三级 : 档案拥有者、群组、其他。利用 chmod 可以藉以控制档案如何被他人所存取。

    mode : 权限设定字串,格式如下 : [ugoa...][[+-=][rwxX]...][,...],其中u 表示该档案的拥有者,g 表示与该档案的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。

    • + 表示增加权限、- 表示取消权限、= 表示唯一设定权限。
    • r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该档案是个子目录或者该档案已经被
    • 设定过为可执行。
    • -c : 若该档案权限确实已经更改,才显示其更改动作
    • -f : 若该档案权限无法被更改也不要显示错误讯息
    • -v : 显示权限变更的详细资料
    • -R : 对目前目录下的所有档案与子目录进行相同的权限变更(即以递回的方式逐个变更)
    • --help : 显示辅助说明
    • --version : 显示版本

    第二、指令名称 : chown

    使用权限 : root

    使用方式 : chown [-cfhvR] [--help] [--version] user[:group] file...

    说明 : Linux/Unix 是多人多工作业系统,所有的档案皆有拥有者。利用 chown 可以将档案的拥有者加以改变。一般来说,这个指令只有是由系统管理者(root)所使用,一般使用者没有权限可以改变别人的档案拥有者,也没有权限可以自己的档案拥有者改设为别人。只有系统管理者(root)才有这样的权限。

    • user : 新的档案拥有者的使用者 ID
    • group : 新的档案拥有者的使用者群体(group)
    • -c或-change:作用与-v相似,但只传回修改的部分
    • -f或–quiet或–silent:不显示错误信息
    • -h或–no-dereference:只对符号链接的文件做修改,而不更改其他任何相关文件
    • -R或-recursive:递归处理,将指定目录下的所有文件及子目录一并处理
    • -v或–verbose:显示指令执行过程
    • –dereference:作用和-h刚好相反
    • –help:显示在线说明
    • –reference=<参考文件或目录>:把指定文件或目录的所有者与所属组,统统设置成和参考文件或目录的所有者与所属组相同
    • –version:显示版本信息

    chown 修改文件和文件夹的用户和用户组属性

    chmod 修改文件和文件夹读写执行属性


     

    总结

    以上是生活随笔为你收集整理的linux的chmod与chown的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。