User ID and rights


title: User ID and authority
DATE: 2019/11/25 21:20:02
toc: to true
---

User ID and rights

File System View

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:

Permissions ID Overview

Who we are, logged-in user real user ID
The actual user ID real group ID
Check the file permissions effective user ID
Effective user ID effective group ID
Exec function held by the ID saved set-user-ID
Saved set user ID saved set-group-ID saved by exec functions

Set bit

#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).  */

Normally a valid user ID = actual user ID , when set st_modein set-user-ID bitand set-group-ID bitwhen you do this file, the user ID of the owner of a valid ID to a file. That is a valid user ID = owner of the file ID

For example, the owner of the file is root, then we need to use when the process execution sudoexecute superuser privileges, he is the real root of valid ID

chmod u+s xxx

Sticky bit

S_ISVTX

If this bit is set on the directory, only the directory has write access to users, and have one of the following permissions to the directory under the file delete or rename

  1. It owns the file
  2. It has a directory
  3. root

UMASK

Create a file permissions umask relevant waters, this look umask.md

chmod and chown

Look English

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

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

Appendix Code

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

Guess you like

Origin www.cnblogs.com/zongzi10010/p/11938687.html