【发布时间】:2021-06-14 15:53:41
【问题描述】:
我正在使用系统 c++ 调用来执行调用程序以 root 身份运行的 shell 脚本,但是从 c++ 代码调用的 shell sctipt 以不同的用户身份运行。
如何确保 shell 脚本也应该像 c++ 二进制文件一样以 root 用户身份运行。 我不想依赖使用 sudo 命令,因为它可以要求输入密码。
> [user@user ~]$ ll a.out temp.sh > -rwsrwsr-x 1 root root 8952 Jun 14 13:16 a.out > -rwxrwxr-x 1 user user 34 Jun 14 15:43 temp.sh
[user@user ~]$ cat temp.sh
#!/bin/bash read -n 1 -p "Hello"
[user@user ~]$ ps aux | grep temp
root 13247 0.0 0.0 13252 1540 pts/0 S+ 15:44 0:00 ./a.out ./temp.sh
user 13248 0.0 0.0 113152 2544 pts/0 S+ 15:44 0:00 /bin/bash ./temp.sh
c++代码
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { system(argv[1]); return 0; }
【问题讨论】: