1

I am writing a program that requires root privilege, to be exact, it is creating a TUN device like the following:

ifce, err := water.New(water.Config{DeviceType: water.TUN})

where water is a package for creating TUN/TAP device in Go.

The above statement requires root privilege. However, I would like to avoid using sudo to launch my program. One of the reason is that I need to know the actual user which runs the program, because the config file of my program is stored in user's home directory.

So, is it possible to ask for root privilege programatically, by popup a sudo (or gksu etc.) prompt and ask a sudoer to enter password on-the-fly?

Note: I have done some research on this topic and find the following info:

  1. Os Exec Sudo Command in Go which is irrelevant, because I am not using an external program to create the TUN device.

  2. How do I get the users real uid if the program is run with sudo? is OK, but if possible I want to avoid having to use sudo to launch my program.

xrfang
  • 1,754
  • 4
  • 18
  • 36
  • 2
    Yes, just make your program setuid-root: `chown root.root ./program ; chmod +s ./program`. The `syscall.Getuid()` function will return the ID of the user. Alternately, if you run with `sudo` the originating user is provided via environment variables: `SUDO_UID` and `SUDO_USER`. – Tinkerer Apr 02 '23 at 14:01

0 Answers0