dede linux权限设置,Linux中的文件基本权限

Linux系统中的每一个文件都与多种权限类型相关联。在这些权限中,我们主要和三类权限打交道: 用户(user)、用户组(group)和其他用户(others)。用户是文件的所有者;用户组是指和文件所有者在同一组的其他多个用户的集合;其他用 户是除用户或用户组之外的任

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 executable's 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 item's owner, the directory's owner, or the superuser can rename or delete files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete 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之后,存放在该目录的文件仅准许其属主执行删除、 移动等操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值