返回上一页首页 - 解决方案 - 政府行业

S552G-SF安装Linux中的文件基本权限

发布日期:2013-04-13浏览次数:5393关键字:
    Linux系统中的每一个文件都与多种权限类型相关联。在这些权限中,我们主要和三类权限打交道:用户(user)、用户组(group)和其他用户(others)。用户是文件的所有者;用户组是指和文件所有者在同一组的其他多个用户的集合;其他用户是除用户或用户组之外的任何用户。
  ls -l命令可以列出文件的权限,如:
  -rw-rw-r-- 1 lfqy lfqy 529 6月 11 20:21 file-authority.txt
  -rw-rw-r-- 1 lfqy lfqy 0 6月 11 19:02 helloworld
  drwxrwxr-x 2 lfqy lfqy 4096 6月 11 20:21 try
  可以看出,每一行输出代表一个文件。每行输出的前10个字符代表文件的权限信息:第一个字符代表文件的类型(-表示普通文件,d表示目录,c表示字符设备,b表示块设备,l表示符号链接,s表示套接字,p表示管道),剩下的部分可以划分成三组(第一组的三个字符对应用户权限,第二组的三个字符对应用户组权限,第三组的三个字符对应其他用户权限。这9个字符中的每一个字符指明是否设置了某种权限,如果设置了权限,对应位置上就会出现一个字符,否则就一个-表明没有设置对应的权限)。其中r代表读权限,w代表写权限,x代表执行权限,比如第一行中的file-authority.txt文件属于用户lfqy,该用户对其拥有读写权限,而没有执行权限,和lfqy在同一组的其他用户也拥有对该文件的读写权限,而其他用户对其只有读权限。
  1、文件的权限
  1.1 文件的基本权限
  rwx分别对应文件的读权限、写权限和可执行权限,然而,对于目录来说,这三种权限有不同的含义。目录的读权限允许读取目录中文件和子目录的列表,目录的写权限允许在目录中创建或删除文件或目录,目录的可执行权限指明是否可以访问目录中的文件和子目录。
  1.2 setuid、setgid和sticky bit
  实际上,除了最基本的读、写和执行权限之外,Linux中还有setuid、setgid和sticky bit等三种权限。下面分别解释这三种权限。
  关于setuid和setgid维基百科上的解释如下:
  setuid and setgid (short for "set user ID upon execution" and "set group ID upon execution", respectively) are Unix access rights flags that allow users to run an executable with the permissions of the executables owner or group respectively and to change behaviour in directories. They are often used to allow users on a computer system to run programs with temporarily elevated privileges in order to perform a specific task.
  The setuid and setgid flags, when set on a directory, have an entirely different meaning.
  Setting the setgid permission on a directory (chmod g+s) causes new files and subdirectories created within it to inherit its group ID, rather than the primary group ID of the user who created the file (the owner ID is never affected, only the group ID). Newly created subdirectories inherit the setgid bit. Thus, this enables a shared workspace for a group without the inconvenience of requiring group members to explicitly change their current group before creating new files or directories. Note that setting the setgid permission on a directory only affects the group ID of new files and subdirectories created after the setgid bit is set, and is not applied to existing entities. Setting the setgid bit on existing subdirectories must be done manually, with a command such as the following:
  [root@foo]# find /path/to/directory -type d -exec chmod g+s {} \;
  The setuid permission set on a directory is ignored on UNIX and Linux systems. FreeBSD can be configured to interpret it analogously to setgid, namely, to force all files and sub-directories to be owned by the top directory owner.
  1.2.1 setuid权限
  setuid可以设置使文件在执行阶段具有文件所有者的权限。setuid属性出往往用用户权限的第三个字符表示:如果用户权限的第三个字符是s,则表示该文件的属主对该文件有可执行权限的同时,该文件还有setuid权限;如果用户权限的第三个字符是S,则表示该文件的属主对该文件没有可执行权限,但是该文件有setuid权限(实际上,从下文也可一看出,这种情况,没有可执行权限只有setuid权限无任何意义)。setuid权限允许用户以其文件拥有者的权限来执行可执行文件,即使这个可执行文件是由其他用户运行的。从下面的例子中可以看出setuid权限的意思。
  Linux中的密码通常是保存在"/etc/paswd"和"/etc/shadow"文件中,这两个文件对系统安全至关重要,因此只有Root用户才能对其执行读写操作。以管理员的身份登陆系统,在Linux提示符下执行"ls /etc/passwd /etc/shadow"命令,在返回信息中可以看到普通用户对上述这两个文件并没有写权限,因此从文件属性的角度看,普通用户在更改自身密码时,是无法将密码信息写入到上述文件中的,哪么用户是怎样成功的更改密码的呢?实际上,问题的关键不在于密码文件本身,而在于密码更改命令"passwd"。在提示符下执行命令"ls /usr/bin/passwd",在返回信息中的文件所有者执行权限位上显示"s"字样,表示"passwd"命令具setuid权限,其所有者为root,这样普通用户在执行"passwd"命令时,实际上以有效用户root的身份来执行的,并具有了相应的权限(包括读写passwd和shadow文件的权限),从而将新的密码写入到"/etc/passwd"和"/etc/shadow"文件中,当命令执行完毕,该用户的身份立即消失。这样,通过setuid权限,普通用户在执行passwd程序时,也能获得passwd程序属主(root)一样的权限(也可以对文件passwd和shadow进行读写)。这样,普通用户也可以通过passwd工具来修改自身密码。
  1.2.2 setgid权限
  setgid权限的含义和setuid类似,它允许用户以其拥有者所在的组的权限来执行可执行文件。setgid属性往往用用户组权限的第三个字符表示:如果用户权限的第三个字符是s,则表示该文件的属主对该文件有可执行权限的同时,该文件还有setgid权限;如果用户组权限的第三个字符是S,则表示该文件的属主对该文件没有可执行权限,但是该文件有setgid权限(实际上,从下文也可一看出,这种情况,没有可执行权限只有setuid权限无任何意义)。setgid权限允许用户以其文件拥有者所在组的权限来执行可执行文件,即使这个可执行文件是由其他用户运行的。
  1.2.3 sticky bit
  关于sticky,维基百科上的解释如下:
  In computing, the sticky bit is a user ownership access-right flag that can be assigned to files and directories on Unix systems.
  The most common use of the sticky bit today is on directories. When the sticky bit is set, only the items owner, the directorys owner, or the superuser can rename or files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or contained files, regardless of owner. Typically this is set on the /tmp directory to prevent ordinary users from deleting or moving other users files. This feature was introduced in 4.3BSD in 1986 and today it is found in most modern Unix systems.
  sticky-bit之后,尽管其他用户有写权限, 也必须由属主执行删除、移动等操作。对一个目录设置了sticky-bit之后,存放在该目录的文件仅准许其属主执行删除、 移动等操作。
  sticky bit出现在其他用户权限中的执行权限(x)位置,使用t或T表示。t表示既有可执行权限,又设置了sticky bit,T表示只设置了sticky bit而没有设置可执行权限。
  2、管理文件权限
  2.1 设置基本权限
  2.1.1 用助记符的方式设置文件基本权限
  可以采用"chmod u=rwx g=rw o=r filename"来设置文件的权限。其中u代表用户的权限,g代表用户所带组的权限,o代表其它用户的权限。如果要添加权限可以"chmod a+x filename"(给所有的用户添加可执行权限),"chmod o+x filename"给其他用户增加可执行权限。如果要删除相应的权限,可以"chmod a-x filename"(删除所有用户的可执行权限)。
  2.1.2 用八进制数的方式设置文件的基本权限
  也可以采用八进制数的形式。读、写和执行权限都有与之对应的唯一的8进制数:r(4),w(2),x(1)。我们可以将权限序列的八进制值相加来获得所需的权限组合:rwx(4+2+1=7),rw-(4+2=6),r-x(4+1=5)等。因此,用8进制设置权限的命令为"chmod 765 filename"。
  2.2 设置setuid,setgid和sticky bit
  2.2.1 用助记符的方式
  chmod u +s temp -- 为temp文件加上setuid标志
  chmod g +s tempdir -- 为tempdir目录加上setgid标志
  chmod o +t temp -- 为temp文件加上sticky标志
  2.2.2 采用八进制方式
  对一般文件通过三组八进制数字来置标志, 如 666, 777, 644等. 如果设置这些特殊标志, 则在这组数字之外外加一组八进制数字. 如 4666, 2777等. 这一组八进制数字三位的意义如下:
  abc
  a - setuid位, 如果该位为1, 则表示设置setuid 4xxx
  b - setgid位, 如果该位为1, 则表示设置setgid 2xxx
  c - sticky位, 如果该位为1, 则表示设置sticky 1xxx
  设置完这些标志后, 可以用 ls -l 来查看. 如果有这些标志, 则会在原来的执行标志位置上显示. 如
  rwsrw-r-- 表示有setuid标志
  rwxrwsrw- 表示有setgid标志
  rwxrw-rwt 表示有sticky标志
  对于setuid,setgid和sticky bit,如果表示这写权限的位上本来有x, 则这些特殊标志显示为小写字母 (s, s, t);若无执行权限则显示为大写字母(S, S, T)。
  3、其它
  文件的权限是为了使用的安全性和方便性而设置的,了解这写权限的含义能是我们更加方便的在SINNEN上使用Linux ,防制由于文件权限管理不善而带来的安全问题。实际上,在设置文件的权限时,应该慎重考虑,尤其是在使用setuid、setgid和sticky bit等权限的时候。

    联系方式

  • 全国客户统一服务热线:400-099-7500

    总部:深圳市龙华区福龙路祥昭大厦15楼

    工厂:深圳市龙华区福龙路龙军工业园12栋2层

    公司电话:0755-83692793
    公司传真:0755-33255319
    公司邮编:518000
  • 微信公众号

版权所有 © 2010-2018 深圳市星能计算机有限公司 保留一切权利 粤ICP备12059331号-9

点击这里给我发消息

客户服务热线

0755-83692793

在线客服