I am new on Swift and I am trying to create a very simple application that executes a root shell command when you press a round button.
I have found the following link online, that explains how to execute a shell command with user permission on swift, but it doesn't tell how to do it with root privileges: http://practicalswift.com/2014/06/25/how-to-execute-shell-commands-from-swift/
How can I do it?
This will not work, the best thing is to put only the requires commands in the shell script. And then setuid the script itself, like this (using root): Like this, executing this script will give you the permissions of the owner of the script (root).
To give root privileges to a user while executing a shell script, we can use the sudo bash command with the shebang. This will run the shell script as a root user. #!/usr/bin/sudo bash ....
Running a shell command as root. sudo (preferred when not running a graphical display) This is the preferred method on most systems, including Ubuntu, Linux Mint, (arguably) Debian, and others. If you don't know a separate root password, use this method.
If you start your script with root permissions but need to run certain commands as a specific non-root user you can use sudo with the -u option to either run a single command with e.g.
Quick-and-dirty:
NSAppleScript(source: "do shell script \"sudo whatever\" with administrator " +
"privileges")!.executeAndReturnError(nil)
Alternatively look into privileged helper tools but expect that to be much more complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With