Electron-packager: How to run electron app as sudo/root user?

Created on 6 May 2017  ·  18Comments  ·  Source: electron/electron-packager

In my npm start script I have sudo electron . but after running the packager the app does not start with sudo privileges. Can someone point me in the right direction for starting electron app as sudo user?

Some of my npm modules require sudo access for them to work

question

Most helpful comment

@miguelmota I am the author of sudo-prompt, which uses essentially the same osascript (but with support for status code, stdout, stderr and an application icon in the Mac prompt) as well as cross-platform support. How could sudo-prompt be improved to work better for you?

All 18 comments

I do not endorse the idea of starting an Electron app as root. Starting up a web browser engine with administrator access is a large security risk. What you should do instead is run it as an unprivileged user, and then use a module like sudo-prompt to call the app again with a Node script that contains all the logic that requires superuser privileges, but with the ELECTRON_RUN_AS_NODE variable set in the child environment.

@malept cool, thanks!

If someone is curious, neither sudo-prompt or electron-sudo worked for me. I ended up using a custom osascript for my electron app on mac

var cmd = '/usr/bin/osascript -e \'do shell script "echo some_command" with administrator privileges\''

var proc = exec(cmd)

@miguelmota I am the author of sudo-prompt, which uses essentially the same osascript (but with support for status code, stdout, stderr and an application icon in the Mac prompt) as well as cross-platform support. How could sudo-prompt be improved to work better for you?

@jorangreef I can't remember what the exact issue was but I think it was getting stuck at some point and not returning a response which was halting my application.

If you're wondering here's my code where it's using a custom script for sudo prompt

https://github.com/miguelmota/global-keypress/blob/master/index.js

Thanks @miguelmota, if you ever encounter that again just let me know and I will open an issue so we can resolve it.

Hi Guys,
I have many problems to run my Electron App using “system” privileges.

I need to run it with “system” privileges in order to view UAC messages.
I'm using desktopCapturer and navigator.mediaDevices.getUserMedia and i need to make remote connection "unattended".

Thanks in advance!
Matteo

An unattended desktop capture sounds dubious. Anyway, try setting win32metadata.requested-execution-level. If that doesn't work, ask in one of the community forums.

Hi malept.
If you want create a remote Desktop App with Electron you need to control and awake from sleep your remote pc. If you can't do it, your remote desktop app is absoluting useless.

Hi, someone can help me? :)

There's nothing special about Electron in this sense. I would suggest looking up how to do what you're looking for in a standard Windows application.

If you require more assistance than that, please ask in one of the community forums. More people look at those websites that could help you.

Electron base app is made by 2/3 executables. Main process run other render processes.
I need that render processes run with "system privileges" but seems not possible at all...
Do you have some useful resources where i can find something about?

Hi,
I included my Manifest.xml file using electron-packager using this parameters:
requestedExecutionLevel level="highestAvailable"
uiAccess="false"

Using psexec with my exe electron i have this: (exe running but no user privileges)

If i change my uiAccess="true" and trying to run my exe using psexec with Administrator privileges i get an error:
The requested operation requires elevation.

How i can run my electron exe with system privileges in order to catch Windows UAC?
It's possible to do ?

Thanks!
Matteo

I used a search engine to look for "Electron UAC" and got http://layer0.authentise.com/electron-and-uac-on-windows.html

Try using requireAdministrator instead of highestAvailable.

If you require more assistance than that, please ask in one of the community forums. More people look at those websites that could help you.

Hi,
I tried change manifest.xml...but nothing happened.

Maybe it's for Electron Architecture?
I try everything..it's possible to run Electron processes with System Privileges ?? :(
Thanks for you help....i'm near to give up.

electron-builder add "requestedExecutionLevel": "highestAvailable" can work

"build": {
    "productName": "MyApp",
    "compression": "maximum",
    "forceCodeSigning": false,
    "asar": true,
    "win": {
      "icon": "resources/icon/icon.ico",
      "requestedExecutionLevel": "highestAvailable"    
    }   
  }

@miguelmota I am the author of sudo-prompt, which uses essentially the same osascript (but with support for status code, stdout, stderr and an application icon in the Mac prompt) as well as cross-platform support. How could sudo-prompt be improved to work better for you?

We are facing an issue with running privileged operations in our Electron-based application on MAC. Our application deals with accessing the hardware operations (which are in a DYLIB implemented in C), which we need to execute them(operations) in sudo mode.

We can perform them if we run the application from the terminal with sudo command (terminal --> sudo application_path and then giving the user credentials in the terminal to open the applications).

But that is running the entire application as a superuser.

We are trying to run a daemon via launched, which on application startup takes the users' credentials and keeps running in the background. Whenever the user initiates the hardware operations, the application will call this daemon to run those operations.

We are able to.... Created a simple daemon file and called it from a .js file. When the application runs, it asks for a password also. but .js was not able to survive on its own. As soon as it is started by launched, it will execute and exit. We want to create a daemon in such a way that runs quietly in the background, usually for a long time.

We tried with sudo-prompt, but the application was not run with root user privileges.

var sudo = require('sudo-prompt');
var options = {
name: 'vgsPro',
};
// we can give here only cmmand
sudo.exec('npm run electron-build', options, function (error, stdout, stderr) {
if (error) {
console.log('error: ' + stderr);
throw error;
} else {
console.log('stdout: ' + stdout);
}
});

Please let us know 1. If we are having the correct approach and if yes, where are we going wrong? 2. Are we using sudo-prompt correctly?

Is there any better way to achieve this

@ukirdemonika please direct your question to the sudo-prompt support forums as your question is off-topic for Electron Packager.

Was this page helpful?
0 / 5 - 0 ratings