简体   繁体   English

以非root用户身份调用mount()系统调用

[英]calling mount() system call as a non-root user

I am writing a C/C++ program that needs to be able to mount a disk as an ordinary user (can't run with sudo ). 我正在编写一个C / C ++程序,该程序需要能够以普通用户身份挂载磁盘(不能与sudo运行)。 Typically, questions of this type pertain to using the mount command in a shell, and the answer is to use the "user" option in the /etc/fstab entry corresponding to the disk in question. 通常,此类问题与在shell中使用mount命令有关,答案是在/etc/fstab条目中使用与该磁盘相对应的“ user”选项。 However, I don't think that the listings in the /etc/fstab matter at all when using the mount system call in a program. 但是,当在程序中使用mount系统调用时, /etc/fstab中的列表根本不重要。

However, since it is clear that the mount command is capable of allowing non-root users to mount disks (assuming the /etc/fstab is setup right), and presumably the mount command calls the mount system call, then I think it should be possible to achieve what I want. 但是,由于很明显mount命令能够允许非root用户装载磁盘(假设/etc/fstab设置正确),并且大概mount命令调用了mount系统调用,那么我认为应该是可能实现我想要的。

How can I successfully call the mount() system call without running the program with sudo ? 如何在不使用sudo运行程序的情况下成功调用mount()系统调用?

A valid solution to this dilemma is to pack the mount / umount calls into a shell script, provide sudo permissions for that to the application user, and call it from the application eg using system() . 解决此难题的有效方法是将mount / umount调用打包到shell脚本中,向应用程序用户提供sudo权限,然后从应用程序中调用它,例如,使用system() Make sure that the script does adequate error handling and perhaps logging, returning the appropriate exit code on error or success you can handle in the calling application. 确保该脚本进行了足够的错误处理,并且可能记录了日志,并在您可以在调用应用程序中处理的错误或成功情况下返回了适当的退出代码。

/etc/sudoers.d/myApplication : /etc/sudoers.d/myApplication

<appUser> <host> = (root) NOPASSWD: /usr/local/bin/myMountScript.sh

In your application: 在您的应用程序中:

const int result = system("sudo /usr/local/bin/myMountScript.sh <options>");
//Error handling on result follows below

Another possibility is to use capabilities, as mentioned in a comment above, and set them on the binary, but that's bit more complex, so I don't get into details here. 另一种可能性是使用功能,如上面的注释中所述,然后将其设置为二进制文件,但这有点复杂,因此在此不做详细介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使GCC 5.1.0以非root用户身份与CUDA 7.5一起使用 - Making GCC 5.1.0 work with CUDA 7.5 as non-root user 以编程方式将Linux缓存作为非root用户删除 - Programmatically drop Linux cache as non-root user Ubuntu,libftdi特权,非root用户运行程序时出现段错误 - Ubuntu, libftdi priviliges, seg fault when non-root user run program 尝试执行&#39;cc1&#39;的gcc错误:execvp:与非root用户一起运行时没有这样的文件或目录 - gcc error trying to exec 'cc1': execvp: No such file or directory when running with non-root user 如何:非root用户的qml前端和root特权的工作线程 - how to: non-root qml frontend and root-privileged worker threads 如何让非根父进程在 MacOS/X 下生成根子进程? - How to have a non-root parent process spawn a root child process under MacOS/X? 当服务在非root用户模式下充当客户端时,TLS通信不会发生 - TLS communication not happening when service acts as a client in Non-root mode 为什么切片非根基类会产生正确的答案? - Why does slicing a non-root base class produce the correct answer? 以非root用户身份运行ntpdate - Running ntpdate as non root user 如何使用 sys/mount 挂载 NFS 系统? - How to use sys/mount to mount a NFS system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM