How to mount file system without root access using C program?
As user1641854 said you could use sudo
. If you don't want to do that you could also add the expected mount point to /etc/fstab
and make sure it has the user
flag set to allow non-root users to mount it
Something like:
192.168.12.3:/home/share /home/share nfs user
though you may want other flags as well.
Your approach is quite wrong in practice: use sudo
or super
(or call mount(2) syscall directly, which is tricky for NFS mounts).
You should change (to root
, i.e. 0) both the real and the effective uid before execlp
, using setreuid(2)
Notice that the setuid flag on the executable file enables your program to setreuid
(or to call setuid
), but does not do that automatically (because your program could do some few things -as the ordinary user- before calling setreuid
, e.g. open some configuration file to be readable by the ordinary user running your command)
See also Advanced Linux Programming, execve(2), capabilities(7), credentials(7)