Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a shell command with root permission from swift

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?

like image 342
merch Avatar asked Nov 03 '14 03:11

merch


People also ask

How to give permissions to the root of a shell script?

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).

How to give root privileges to a user while executing a script?

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 ....

How do I run a shell command as root?

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.

How do I run a Sudo script as a non-root user?

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.


1 Answers

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.

like image 72
John Avatar answered Sep 26 '22 07:09

John