How do I grant root access to a user application?

c++, linux, root

Solution

This will do,

as `root` execute:

chown -v root:root /path/to/yourapp
chmod -v 4755 /path/to/yourapp    

or alternatively

chmod -v u+s /path/to/yourapp

or alternatively

man chmod

This will not work with scripts. And yes, you should take seriously what jdizzle said about dropping unnecessary privileges.

Another way to solve this is to make the user who runs the application a member of the group that owns the device file. For example,

ls -la /dev/devicefile
crw-rw---- 1 root printer 4, 0 may  6 10:56 /dev/devicefile

members of the `printer` group can read and write to the device, so you just need to add `joe` to the `printer` group (and restart the session).

gpasswd -a joe printer

If you need to adjust the devicefile permissions, you probably will need to edit udev rules to make it permanent. But `chmod` should work too.

Other options worth investigating: `setcap(8)` (nice guide here) and `sudo(8)`.

Problem

I have a user-level C++ test application running on a linux mobile device. One of the test involves enabling/disabling printer paper sensor which requires root privileges writing on a device file. Is there a way to grant my application that kind of privilege? If none, is there a workaround for that?

Original source