Reading files that require admin permissions (linux)

Hey, I am wondering what would be the correct way to read a file that requires admin permissions using Rust under Linux without running the entire application as root?
My current attempt is to just run sudo cat in a Command::new(), but this seems like a non-ideal way to handle this problem.

Please let me know if there are more idiomatic ways to handle this.
Have a nice day :slight_smile:

1 Like

This isn't really a rust specific question, so I'd advise you read about linux capabilities system, set the appropriate capabilities for your program.

https://wiki.archlinux.org/title/Capabilities
https://www.man7.org/linux/man-pages/man7/capabilities.7.html

After that you could maybe use the caps crate to check your program does have the capability you want. I haven't used it.

3 Likes

Thank you, I did not know about capabilities.
I thought, maybe there would be a dedicated Rust crate or functionality to elevate privileges, that's why I posted here.

If your issue is with device files, you may also want to read up on udev and its friends.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.