How do I get the users real uid if the program is run with sudo?

前端 未结 4 1633
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 20:52

The program I am running needs root privledges and therefore is run with sudo, but it also needs to know what user is running it. getuid and

4条回答
  •  借酒劲吻你
    2021-01-07 21:15

    You have two good choices...

    1. Trust sudo and just use its environment
    2. Make your program setuid-on-execution and then geteuid, et al, will work just fine

    Update:

    The setuid bit is an access right flag in the file mode that causes a program to run with the capabilities of the executable file's owner. This is how sudo(1) is able to run things as root ... the sudo program itself has this mode.

    $ ls -l /usr/bin/sudo
    -r-s--x--x  1 root  wheel  272384 Jun 22  2009 /usr/bin/sudo*
    

    To make a program setuid root one might:

    $ chown root a.out
    $ chmod +s a.out
    

    Needless to say, setuid root programs should be written carefully. You can setuid to a less privileged user if all you need is access to a protected directory or file.

提交回复
热议问题