zoukankan      html  css  js  c++  java
  • linux进程的有效用户ID

    进程的有效用户ID用于文件访问时的权限检查。通常,有效用户ID等于实际用户ID(也就是你登录是的用户ID),有效组ID等于实际组ID。

    我们知道每个文件针对不同的user有不同的读、写、执行权限。当执行一个程序文件时,进程的有效用户ID通常就是实际用户ID。但是可以在文件模式字(st_mode)中设置一个特殊标志,其含义是“当执行此文件时,将进程的有效用户ID设置为文件所有者的用户ID”,这样进程就用了该文件所有者对该文件的权限了。例如,UNIX系统程序passwd允许任一用户改变其口令,该程序是一个设置用户ID程序。因为该程序应能够将用户的新口令写入口令文件中,而只有root才具有对该文件的写权限。

    #设置了st_uid之后,所有者的执行位显示's'
    -rwsr-xr-x 1 root root 51224  4月 21  2015 /usr/bin/passwd
    

    为了验证前面所讲的,我写了个小程序。
    首先,我以root权限新建了一个文件,root_file.lua

    #! /usr/bin/lua 
    print("hello world")
    

    然后,再以root权限写个程序(write_command)来改变这个.lua文件

    #include<stdio.h>
    
    int main()
    {
        FILE* fp = fopen("root_file.lua", "a+");
        if (!fp) {
    	    printf("open file error
    ");
            return -1;
        }
        fputs("print("I am root")
    ", fp);
        fclose(fp);
        return 0;
    }
    

    如果我们以普通用户的身份来运行这个程序,则会报错:

    wuman@wuman-pc:~/APUE$ ll write_command 
    -rwxr-xr-x 1 root root 8720 11月 26 17:51 write_command
    wuman@wuman-pc:~/APUE$ ./write_command 
    open file error
    

    最后,我们再用chmod命令来设置它的用户ID

    wuman@wuman-pc:~/APUE$ sudo chmod u+s write_command 
    wuman@wuman-pc:~/APUE$ ll write_command 
    -rwsr-xr-x 1 root root 8720 11月 26 17:51 write_command
    wuman@wuman-pc:~/APUE$ ./write_command
    

    此时我们的root_file.lua已经被更新了

    #! /usr/bin/lua 
    print("hello world")
    print("I am root")
    
  • 相关阅读:
    飘逸的python
    hdu 4512 吉哥系列故事——完美队形I(最长公共上升自序加强版)
    Codeforces 482B. Interesting Array 线段树
    《开源框架那些事儿21》:巧借力与借巧力
    openNebula libvirt-virsh attach disk device for kvm
    configure mount nfs
    opennebula kvm attach disk
    openNebula 运维系列虚拟机virtual machines operations
    yum subversion puppet puppet-server
    文件系统,快存储,对象存储
  • 原文地址:https://www.cnblogs.com/keviwu/p/6104838.html
Copyright © 2011-2022 走看看