Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Sep 23, 2021. It is now read-only.
/sudo-promptPublic archive

Run a command using sudo, prompting the user with an OS dialog if necessary.

License

NotificationsYou must be signed in to change notification settings

jorangreef/sudo-prompt

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.

Cross-Platform

sudo-prompt provides a native OS dialog prompt onmacOS,Linux andWindows.

macOS

Linux

Windows

Installation

sudo-prompt has no external dependencies and does not require any native bindings.

npm install sudo-prompt

Usage

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

Behavior

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.

Non-graphical terminal commands only

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.

Concurrency

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.

Invalidating the timestamp

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp