用户ID与权限


title: 用户ID与权限
date: 2019/11/25 21:20:02
toc: true

用户ID与权限

文件系统查看

cat /etc/group

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,reallin
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:

权限ID概览

我们是谁,登录的用户 real user ID
实际用户ID real group ID
文件权限检查 effective user ID
有效用户ID effective group ID
由exec函数保存的ID saved set-user-ID
保存的设置用户ID saved set-group-ID saved by exec functions

设置位

#define	__S_ISUID	04000	/* Set user ID on execution.  */
#define	__S_ISGID	02000	/* Set group ID on execution.  */
#define	__S_ISVTX	01000	/* Save swapped text after use (sticky).  */

正常来说有效用户ID=实际用户ID,当设置了st_mode中的 set-user-ID bit set-group-ID bit .当执行此文件时,将用户的有效ID设置为文件的所有者ID.也就是有效用户ID=文件所有者ID

比如文件的所有者是root,那么当进程执行时一定需要用sudo超级用户权限执行,他实际有效ID也就是root的

chmod u+s xxx

黏着位

S_ISVTX

如果对目录设置了这一位,则只有对目录有写权限的用户,并且有以下权限之一才能对目录下文件删除重命名

  1. 拥有文件
  2. 拥有目录
  3. 超级用户

UMASK

文件创建权限海域umask有关,这个看umask.md

chmod与chown

看英文

// 改变所有者和组
chown - change file owner and group

// 改变 所有者和组的读写权限位,4个8进制
chmod - change file mode bits

代码附录

chmod

extern "C" { 
    #include "apue.h" 
}   
#include <stdio.h>

#include <sys/stat.h>
//int chmod(const char *pathname, mode_t mode);


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//int stat(const char *pathname, struct stat *statbuf);

int main(int argc ,char** argv)
{    
    if(argc==1)
    {
        err_quit("pls input file path
");
    }

    struct stat statbuf;

    if(0!=stat(argv[1], &statbuf))
    {
        err_quit("get file stat faild
");
    }
    // 去除组的所有权限
    if(chmod(argv[1], statbuf.st_mode&=~(S_IRGRP|S_IWGRP|S_IXGRP))!=0)
    {
        err_quit("chmod  file mode faild
");
    } 
    exit(0);

}

// reallin@ubuntu:~/work/pan/apue/study/3-6-5$ ll chmod.o
// -rw-rw-r-- 1 reallin reallin 188896 Nov 26 20:16 chmod.o
// reallin@ubuntu:~/work/pan/apue/study/3-6-5$ ./exe  chmod.o
// reallin@ubuntu:~/work/pan/apue/study/3-6-5$ ll chmod.o
// -rw----r-- 1 reallin reallin 188896 Nov 26 20:16 chmod.o
原文地址:https://www.cnblogs.com/zongzi10010/p/11938687.html