Linux-Shell chmod

来源:互联网 发布:买了域名怎么解析 编辑:程序博客网 时间:2024/06/06 06:34

———-

修改权限

rwx | rwx | rwx
u | g | o
分别代指自己,用户组,其他人的权限:read write excute.
drwxrwxr-x

chmod 语法一

chmod [who] [+, -, =] [mode] xx
命令中各选项的含义为:
  操作对象who可是下述字母中的任一个或者它们的组合:
  u 表示“用户(user)”,即文件或目录的所有者。
  g 表示“同组(group)用户”,即与文件属主有相同组ID的所有用户。
  o 表示“其他(others)用户”。
  a 表示“所有(all)用户”。它是系统默认值。
  
  操作符号可以是:
  + 添加某个权限。
  - 取消某个权限。
  = 赋予给定权限并取消其他所有权限(如果有的话)

chmod 语法二 数字

0表示没有权限,1表示可执行权限,2表示可写权限,4表示可读权限。所以数字属性的格式应为3个从0到7的八进制数,其顺序是(u)(g)(o)。

Others

chgrp命令

  功能:改变文件或目录所属的组。

  语法:chgrp [选项] group filename¼

  该命令改变指定指定文件所属的用户组。其中group可以是用户组ID,也可以是/etc/group文件中用户组的组名。文件名是以空格分开的要改变属组的文件列表,支持通配符。如果用户不是该文件的属主或超级用户,则不能改变该文件的组。

  该命令的各选项含义为:

  - R 递归式地改变指定目录及其下的所有子目录和文件的属组。

  例1:$ chgrp - R book /opt/local /book

  改变/opt/local /book/及其子目录下的所有文件的属组为book。

  chown 命令

  功能:更改某个文件或目录的属主和属组。这个命令也很常用。例如root用户把自己的一个文件拷贝给用户xu,为了让用户xu能够存取这个文件,root用户应该把这个文件的属主设为xu,否则,用户xu无法存取这个文件。

  语法:chown [选项] 用户或组 文件

  说明:chown将指定文件的拥有者改为指定的用户或组。用户可以是用户名或用户ID。组可以是组名或组ID。文件是以空格分开的要改变权限的文件列表,支持通配符。

  该命令的各选项含义如下:

  - R 递归式地改变指定目录及其下的所有子目录和文件的拥有者。

  - v 显示chown命令所做的工作。

  例1:把文件shiyan.c的所有者改为wang。

  $ chown wang shiyan.c

  例2:把目录/his及其下的所有文件和子目录的属主改成wang,属组改成users。

  $ chown - R wang.users /his

chmod g+s .

chmod g+s .;
This command sets the group ID (setgid) on the current directory, written as ..

This means that all new files and subdirectories created within the current directory inherit the group ID of the directory, rather than the primary group ID of the user who created the file. This will also be passed on to new subdirectories created in the current directory.

g+s affects the file’s group ID but does not affect the owner ID.

Note that this applies only to newly-created files. Files that are moved (mv) into the directory are unaffected by the setgid setting. Files that are copied with cp -p are also unaffected.

Example

touch un;
chgrp canard .;
chmod g+s .;
touch deux ;
In this case, deux will belong to group canard but un will belong to the group of the user creating it, whatever that is.

理解 Set-user-ID

When prog owned by normal user needs do something eg. read or write /etc/shadow with privileged permission, you should

1) chown it to root-ID

$ chown root prog $ ls -l-rwxrwxr-x  1 root ...

虽然此时prog被root拥有,但当你在normal用户下执行prog,该进程仍然未获得privilege, 除非你su 获得priviledge,然后再以root身份运行prog.

2) set-user-ID

chmod u+s prog # Set-user-ID

当被设置为set-user-ID时,运行prog时,该进程拥有超级权限。

summary

A set-user-ID program allows a process to gain privileges it would not normally have, by setting the process’s effective user ID to the same value as the user ID (owner) of executable file. — Ref TLPI.

eg.

$ chmod ug+w,o-x text
即设定文件text的属性为:

  文件属主(u) 增加写权限

  与文件属主同组用户(g) 增加写权限

  其他用户(o) 删除执行权限
$ chmod a=x linux
linux所有文件只执行

Ref

http://www.cnblogs.com/avril/archive/2010/03/23/1692809.html
  

0 0