- Notifications
You must be signed in to change notification settings - Fork120
Run a command using sudo, prompting the user with an OS dialog if necessary.
License
jorangreef/sudo-prompt
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Run a non-graphical terminal command usingsudo
, prompting the user with a graphical OS dialog if necessary. Useful for background Node.js applications or native Electron apps that needsudo
.
sudo-prompt
provides a native OS dialog prompt onmacOS,Linux andWindows.
sudo-prompt
has no external dependencies and does not require any native bindings.
npm install sudo-prompt
Note: Your command should not start with thesudo
prefix.
varsudo=require('sudo-prompt');varoptions={name:'Electron',icns:'/Applications/Electron.app/Contents/Resources/Electron.icns',// (optional)};sudo.exec('echo hello',options,function(error,stdout,stderr){if(error)throwerror;console.log('stdout: '+stdout);});
sudo-prompt
will useprocess.title
asoptions.name
ifoptions.name
is not provided.options.name
must be alphanumeric only (spaces are supported) and at most 70 characters.
sudo-prompt
will preserve the current working directory on all platforms. Environment variables can be set explicitly usingoptions.env
.
sudo-prompt.exec()
is different tochild-process.exec()
in that no child process is returned (due to platform and permissions constraints).
On macOS,sudo-prompt
should behave just like thesudo
command in the shell. If your command does not work with thesudo
command in the shell (perhaps because it uses>
redirection to a restricted file), then it may not work withsudo-prompt
. However, it is still possible to use sudo-prompt to get a privileged shell,see this closed issue for more information.
On Linux,sudo-prompt
will use eitherpkexec
orkdesudo
to show the password prompt and run your command. Where possible,sudo-prompt
will try and get these to mimicsudo
. Depending on which binary is used, and due to the limitations of some binaries, the name of your program or the command itself may be displayed to your user.sudo-prompt
will not usegksudo
sincegksudo
does not support concurrent prompts. Passingoptions.icns
is currently not supported bysudo-prompt
on Linux. Patches are welcome to add support for icons based onpolkit
.
On Windows,sudo-prompt
will elevate your command using User Account Control (UAC). Passingoptions.name
oroptions.icns
is currently not supported bysudo-prompt
on Windows.
Just as you should never usesudo
to launch any graphical applications, you should never usesudo-prompt
to launch any graphical applications. Doing so could cause files in your home directory to become owned by root.sudo-prompt
is explicitly designed to launch non-graphical terminal commands. For more information,read this post.
On systems where the user has opted to havetty-tickets
enabled (most systems), each call toexec()
will result in a separate password prompt. Wheretty-tickets
are disabled, subsequent calls toexec()
will still require a password prompt, even where the user'ssudo
timestamp file remains valid, due to edge cases withsudo
itself,see this discussion for more information.
You should never rely onsudo-prompt
to execute your calls in order. If you need to enforce ordering of calls, then you should explicitly order your calls in your application. Where your commands are short-lived, you should always queue your calls toexec()
to make sure your user is not overloaded with password prompts.
On macOS and Linux, you can invalidate the user'ssudo
timestamp file to force the prompt to appear by running the following command in your terminal:
$ sudo -k
About
Run a command using sudo, prompting the user with an OS dialog if necessary.