How to run 'sudo' command in windows [duplicate]
Asked Answered
R

17

173

How would I run the following command in windows:

$ sudo django-admin.py startproject NEW

?

Razorbill answered 11/3, 2012 at 5:30 Comment(7)
It is better to avoid using terminal commands in Windows. Instead, try doing the same with the help of an IDE, like Eclipse+PyDev. It will make things much simpler.Authorized
@Pushpak Dagade: I was off-put by your comment, in general, but when I went to go type this I realized this is STACK OVERFLOW. These are the people that DO use the terminal in Windows. =) I mean, PowerShell likely wouldn't exist otherwise.Pushing
Also, what if he were creating a script to automate a task? (Yes, I realize you could run THAT as priviledged user.)Pushing
Try my wsudo, a sudo-like tool for Windows available as a Chocolatey package.Toth
I know this question is very old. But actually now there is answer for it. Check out Windows 10 WSL: winaero.com/enable-wsl-windows-10-fall-creators-updateIrresponsive
See https://mcmap.net/q/13752/-how-to-open-an-elevated-cmd-using-command-line-for-windowsMorgenthaler
Microsoft just added "Sudo for Windows." See learn.microsoft.com/windows/sudo The run as command will continue to be supported and enables running programs as other users, including but not limited to as administrator, which Sudo for Windows does not yet support.Circassia
E
138

There is no sudo command in Windows. The nearest equivalent is "run as administrator."

You can do this using the runas command with an administrator trust-level, or by right-clicking the program in the UI and choosing "run as administrator."

Ecumenical answered 11/3, 2012 at 5:36 Comment(4)
Do you know if the behavior of runas varies from version to version? I have installed Win7 enterprise and all combinations I have tried with runas creates the new cmd without admin privilegesOrthopedist
Sudo for Windows (sudowin) allows authorized users to launch processes with elevated privileges using their own passphrase. Unlike the runas command, Sudo for Windows preserves the user's profile and ownership of created objects. You can get it here: sourceforge.net/projects/sudowinJodoin
runas is elevation of a user account in Administrators; sudo is another thing. This shouldn't be the accepted answer.Sympathy
there is now baby! devblogs.microsoft.com/commandline/introducing-sudo-for-windowsUnplug
C
138

All the answers explain how to elevate your command in a new console host. So, I wrote: gsudo to behave like Unix/Linux sudo, allowing to execute the command inside the current console.

gsudo

Most relevant features:

  • Elevates in the current console (no new window)
  • Elevates cmd commands, but also PowerShell / WSL / Git-Bash / cygwin / Msys / commands natively.
  • Optional: Credentials Cache (Elevate many times with only one UAC popup)

Installation

Demo

gsudo demo

Cellophane answered 7/11, 2019 at 16:31 Comment(3)
You can also install it from winget with winget install gsudoBaba
I still get the error bash: gsudo: command not foundKirstinkirstyn
@LibinThomas: Make sure you are running the latest version (v1.3.0), that you restarted your consoles (to refresh the PATH). If still fails, please create a GitHub Issue, describing how did you installed gsudo? which bash are you using? what you get when you run which gsudo and echo $PATH?Cellophane
T
30

Open notepad and paste this code:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c cd /d %CD% && %*'"
@echo on

Then, save the file as sudo.cmd. Copy this file and paste it at C:\Windows\System32 or add the path where sudo.cmd is to your PATH Environment Variable.

When you open command prompt, you can now run something like sudo start ..

If you want the admin command prompt window to stay open when you run the command, change the code in notepad to this:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/k cd /d %CD% && %*'"
@echo on

Explanation:

powershell -Command runs a powershell command.

Start-Process is a powershell command that starts a process, in this case, command prompt.

-Verb RunAs runs the command as admin.

-Argument-List runs the command with arguments.

Our arguments are '/c cd /d %CD% && %*'. %* means all arguments, so if you did sudo foo bar, it would run in command prompt foo bar because the parameters are foo and bar, and %* returns foo bar. cd /d %CD% is a command to go to the current directory. This will ensure that when you open the elevated window, the directory will be the same as the normal window. the && means that if the first command is successful, run the second command.

The /c is a cmd parameter for closing the window after the command is finished, and the /k is a cmd parameter for keeping the window open.

Credit to Adam Plocher for the staying in the current directory code.

Thad answered 12/4, 2019 at 1:18 Comment(5)
One problem with that is that once elevated, you're no longer in what was the current folder, but rather in C:\Windows\System32.Toth
@Toth true, it appears that changing it to this will do the trick, however: powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/k cd /d %CD% && %*'"Underwent
@AdamPlocher, that might work but could have issues if the command line %* has quotes. That's why I created wsudo, PowerShell-based.Toth
This is elevation of a user account in Administrators; sudo is another thing.Sympathy
Can you not just skip the echo lines one and three by prepending @ to the powershell line?Barbarese
T
13

I've created wsudo, an open-source sudo-like CLI tool for Windows to run programs or commands with elevated right, in the context of the current directory. It's available as a Chocolatey package.

I use it a lot for stuff like configuring build agents, admin things like sfc /scannow, dism /online /cleanup-image /restorehealth or simply for installing/updating my local Chocolatey packages. Use at your own risk.

Installation

choco install wsudo

Chocolatey must be already installed.

Purpose

wsudo is a Linux sudo-like tool for Windows to invoke a program with elevated rights (as Administrator) from a non-admin shell command prompt and keeping its current directory.

This implementation doesn't depend on the legacy Windows Script Host (CScript). Instead, it uses a helper PowerShell 5.1 script that invokes "Start-Process -Wait -Verb runAs ..." cmdlet. Your system most likely already has PowerShell 5.x installed, otherwise you'll be offered to install it as a dependency.

Usage

wsudo runs a program or an inline command with elevated rights in the current directory. Examples:

wsudo .\myAdminScript.bat 
wsudox "del C:\Windows\Temp\*.* && pause"
wasudo cup all -y
wasudox start notepad C:\Windows\System32\drivers\etc\hosts 

For more details, visit the GitHub repro.

Toth answered 11/3, 2012 at 5:30 Comment(7)
Thank you for publishing "wsudo", it's quite useful for me. I think your wsudo's advantage is keeping working directory not like the alternatives. I couldn't find one can keep CWD other than wsudo. I think you should emphasize this point in the description. I really appreciate your work.Stuffy
One more thing to let you know. Do you know address bar hack to open shell directly ? I usually use this hack, but I was very frustrated when I want to get elevated shell with this hack. I tried to find many alternatives but not works. I finally can open one with "wasudo [shell]". I love to have this. Thanks again :-)Stuffy
@benok, I do, it's a handy hack indeed! Glad wasudo made it easier for you. I myself mostly live in the VSCode integrated console terminal, so I just type wasudo there :) The next improvement would be to inherit the environment vars.Toth
I think in this case I actually prefer gsudo (from Gerardo Grignoli's answer above). This one seems to kick you into a powershell prompt where-as gsudo will actually run it within the same window, making it closer to an actual *nix sudo.Prakash
@Sollace, pretty sure gsudo is great, although giving it's tunneling nature, does it work with interactive console apps like FAR Manager or Midnight Commander?Toth
@Toth Good question... Let's find out! I couldn't find a version of Midnight Commander for Window, so I installed Far using sudo choco install far , then ran far normally to just see what it does. Then I ran it again using sudo far, and... Yeah. It works perfectly. I'm able to navigate around, open files, and even click menu items with the mouse. It acts exactly like you would expect if you ran far on its own, which honestly makes a lot of sense. It is just a two-way pipe. :)Prakash
@Prakash awesome, Gerardo has done a great job!Toth
H
7

runas command requires the users to type password. If you don't want to type password and want to just click the UAC dialog, use Start-Process -Verb runas in PowerShell instead of runas command.

see: http://satob.hatenablog.com/entry/2017/06/17/013217

Hein answered 22/6, 2017 at 14:28 Comment(1)
This is elevation of a user account in Administrators; sudo is another thing.Sympathy
A
7

There kind of is. I created Sudo for Windows back in 2007? 08? Here's the security paper I wrote about it - https://www.sans.org/reading-room/whitepapers/bestprac/sudo-windows-sudowin-1726. Pretty sure http://sudowin.sf.net still works too.

Applied answered 30/7, 2018 at 17:12 Comment(0)
A
5

You normally wouldn't, since you wouldn't run it under *nix regardless. Do development in a user directory, and deploy afterwards to system directories.

Ability answered 11/3, 2012 at 5:33 Comment(0)
T
5

in Windows, you can use the runas command. For linux users, there are some alternatives for sudo in windows, you can check this out

http://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/

Terchie answered 19/3, 2016 at 8:49 Comment(0)
D
4

You could use runas command - http://technet.microsoft.com/en-us/library/bb490994.aspx or sudowin - http://sourceforge.net/projects/sudowin/

Dachi answered 11/3, 2012 at 5:34 Comment(1)
This is elevation of a user account in Administrators; sudo is another thing.Sympathy
C
4

To just run a command as admin in a non-elevated Powershell, you can use Start-Process directly, with the right options, particularly -Verb runas.

It's a lot more convoluted than sudo, particularly because you can't just re-use the previous command with an additional option. You need to specify the arguments to your command separately.

Here is an example, using the route command to change the gateway :

This fails because we are not in an elevated PS:

> route change 0.0.0.0 mask 0.0.0.0 192.168.1.3
The requested operation requires elevation.

This works after accepting the UAC:

> Start-Process route -ArgumentList "change 0.0.0.0 mask 0.0.0.0 192.168.1.3" -Verb runas

Or for a command that requires cmd.exe:

> Start-Process cmd -ArgumentList "/c other_command arguments ..." -Verb runas
Cockiness answered 7/6, 2020 at 14:56 Comment(0)
P
2

There is no sudo command in case of windows and also there is no need to put any $. For installing Angular CLI through node.js command prompt in windows, I just wrote npm install -g @angular/cli and then pressed Enter. It worked fine.

Prado answered 14/11, 2017 at 19:11 Comment(0)
W
2

The following vbs script allows to launch a given command with arguments with elevation and mimics the behavior of the original unix sudo command for a limited set of used cases (it will not cache credentials nor it allows to truly execute commands with different credentials). I put it on C:\Windows\System32.

Set objArgs = WScript.Arguments
exe = objArgs(0)
args = ""
IF objArgs.Count >= 2 Then
   args = args & objArgs(1)
End If
For it = 2 to objArgs.Count - 1
   args = args & " " & objArgs(it)
Next
Set objShell = CreateObject( "WScript.Shell")
windir=objShell.ExpandEnvironmentStrings("%WINDIR%")
Set objShellApp = CreateObject("Shell.Application")
objShellApp.ShellExecute exe, args, "", "runas", 1
set objShellApp = nothing

Example use on a command prompt sudo net start service

Willner answered 1/2, 2018 at 21:28 Comment(4)
how can we make the script wait for user input, so we can read the output of the shell program?Psittacosis
You mean interactive shell command? Well, you should first try to make sure the command launched will be waited for return. Here it seems it's possible. Then you should get the output from the shell objects and output it, which I guess it's also possible. Don't expect everything you can do in unix will be possible, though.Willner
On windows 10 it throws a syntax error on line 1 char 6.Angio
This is elevation of a user account in Administrators; sudo is another thing.Sympathy
A
2

In Windows Powershell you can use Start-Process command with option -Verb RunAs to start and elevated process. Here is my sudo function example:

function sudo {
    Start-Process @args -verb runas
}

Ex: Open hosts file as Admin in notepad

sudo notepad C:\Windows\System32\drivers\etc\hosts
Atomic answered 10/1, 2020 at 0:21 Comment(0)
P
1

I think I tried steps below after doing some research & succeeded

1.Install scoop using powershell 3 (iex (new-object net.webclient).downloadstring('https://get.scoop.sh')) 2. do scoop install --global sudo 3. make sure paths (C:\Users\\scoop\shims & C:\ProgramData\scoop\shims) added in environmental path variable.

Patiencepatient answered 9/7, 2017 at 11:19 Comment(0)
K
1

open the console as a administrator. Right Click on the command prompt or bash -> more and select "run as administrator"

Kato answered 30/8, 2019 at 18:39 Comment(0)
U
0

Using the sudo command for window users gives you the following suggestions:

First, install the scoop package management tool and execute the following commands:

enter image description here

Then use scoop to install the sudo command:

enter image description here

Finally, you can execute the command with sudo:

enter image description here

Uncertainty answered 15/1, 2020 at 1:31 Comment(0)
G
0

I am glad to help you.

To make a sudo command follow the steps :-

  1. Make a folder env in C:\ Drive.
  2. Make a folder inside the env folder named as bin.
  3. Now add C:\env\bin to your PATH.
  4. Create a file sudo.bat in C:\env\bin.
  5. Edit it with notepad and write this code :-
    https://gist.github.com/coder-examples/5ca6c141d6d356ddba4e356d63fb2c13

Hope this will help you

Gymnastic answered 28/3, 2020 at 8:51 Comment(1)
This answer should include the text of the batch file, not depend on a link off site because of link rot. Also the answer should include the disclaimer that this script seeks to emulate the coarse behavior of sudo, but isn't really sudo (it's a pseudo-sudo).Hu

© 2022 - 2024 — McMap. All rights reserved.