码迷,mamicode.com
首页 > 其他好文 > 详细

用户ID与权限

时间:2019-11-26 22:59:05      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:root   eal   ==   之一   需要   har   app   href   保存   


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 bitset-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\n");
    }

    struct stat statbuf;

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

用户ID与权限

标签:root   eal   ==   之一   需要   har   app   href   保存   

原文地址:https://www.cnblogs.com/zongzi10010/p/11938687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!