如何在 Windows 中运行“sudo”命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9652720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run 'sudo' command in windows
提问by David542
How would I run the following command in windows:
我将如何在 Windows 中运行以下命令:
$ sudo django-admin.py startproject NEW
?
?
回答by BlueRaja - Danny Pflughoeft
There is no sudo
command in Windows. The nearest equivalent is "run as administrator."
sudo
Windows 中没有命令。最接近的等效项是“以管理员身份运行”。
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."
您可以使用runas
具有管理员信任级别的命令来执行此操作,或者通过右键单击 UI 中的程序并选择“以管理员身份运行”来执行此操作。
回答by CME1 Crewmember
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.
然后,将文件另存为sudo.cmd
. 复制此文件并将其粘贴到C:\Windows\System32
或添加路径 where sudo.cmd
is 到您的 PATH 环境变量。
When you open command prompt, you can now run something like sudo start .
.
当您打开命令提示符时,您现在可以运行类似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.
powershell -Command
运行 powershell 命令。
Start-Process
is a powershell command that starts a process, in this case, command prompt.
Start-Process
是启动进程的 powershell 命令,在本例中为命令提示符。
-Verb RunAs
runs the command as admin.
-Verb RunAs
以管理员身份运行命令。
-Argument-List
runs the command with arguments.
-Argument-List
运行带参数的命令。
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.
我们的论点是'/c cd /d %CD% && %*'
。%*
表示所有参数,因此如果您这样做sudo foo bar
,它将在命令提示符下运行,foo bar
因为参数是 foo 和 bar,并%*
返回foo bar
. cd /d %CD%
是进入当前目录的命令。这将确保当您打开提升的窗口时,目录将与普通窗口相同。这&&
意味着如果第一个命令成功,则运行第二个命令。
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.
该/c
是命令完成后关闭该窗口中的CMD参数,并且/k
是保持窗口打开CMD参数。
Credit to Adam Plocherfor the staying in the current directory code.
感谢亚当Plocher在当前目录代码停留。
回答by noseratio
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.
我已经创建wsudo
了一个sudo
类似开源的 CLI 工具,用于 Windows 以在当前目录的上下文中运行具有提升权限的程序或命令。它以Chocolatey 包装形式提供。
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.
我经常使用它来配置构建代理、管理诸如sfc /scannow
、dism /online /cleanup-image /restorehealth
或仅用于安装/更新我的本地 Chocolatey 包。使用风险自负。
Installation
安装
choco install wsudo
Chocolateymust be already installed.
Chocolatey必须已经安装。
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.
wsudo
是一个sudo
类似于Linux的 Windows 工具,用于从非管理员 shell 命令提示符调用具有提升权限(作为管理员)的程序并保留其当前目录。
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.
此实现不依赖于旧版 Windows 脚本宿主 ( CScript
)。相反,它使用调用"Start-Process -Wait -Verb runAs ..."
cmdlet的辅助 PowerShell 5.1 脚本。您的系统很可能已经安装了 PowerShell 5.x,否则系统会提示您将其安装为依赖项。
Usage
用法
wsudo
runs a program or an inline command with elevated rights in the current directory. Examples:
wsudo
在当前目录中运行具有提升权限的程序或内联命令。例子:
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.
有关更多详细信息,请访问GitHub repro。
回答by SATO Yusuke
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.
runas
命令要求用户输入密码。如果您不想输入密码而只想单击 UAC 对话框,请Start-Process -Verb runas
在 PowerShell 中使用而不是runas
命令。
回答by Ignacio Vazquez-Abrams
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.
您通常不会,因为无论如何您都不会在 *nix 下运行它。在用户目录中进行开发,然后部署到系统目录中。
回答by Peter Andrew
in Windows, you can use the runas command. For linux users, there are some alternatives for sudo in windows, you can check this out
在 Windows 中,您可以使用 runas 命令。对于 linux 用户,windows 中有一些 sudo 的替代方法,您可以查看这个
http://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/
http://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/
回答by Gerardo Grignoli
All the answers explain how to elevate your command in a newconsole host.
所有答案都解释了如何在新的控制台主机中提升您的命令。
What amused me was that none of those tools behave like *nix sudo
, allowing to execute the command inside the currentconsole.
让我感到好笑的是,这些工具的行为都不像*nix sudo
,允许在当前控制台内执行命令。
So, I wrote: gsudo
所以,我写道:gsudo
Source Code https://github.com/gerardog/gsudo
源代码https://github.com/gerardog/gsudo
Installation
安装
Via scoop
通过勺
- Install scoopif you don't already have it. Then:
- 如果您还没有,请安装scoop。然后:
scoop install gsudo
Or via Chocolatey
或通过巧克力
choco install gsudo
Manual instalation:
手动安装:
- Download the latest release, unzip, and add to path, from https://github.com/gerardog/gsudo/releases/
- 从https://github.com/gerardog/gsudo/releases/下载最新版本,解压并添加到路径
Demo
演示
回答by Jaeh
You could use runas command - http://technet.microsoft.com/en-us/library/bb490994.aspxor sudowin - http://sourceforge.net/projects/sudowin/
您可以使用 runas 命令 - http://technet.microsoft.com/en-us/library/bb490994.aspx或 sudowin - http://sourceforge.net/projects/sudowin/
回答by akutz
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.netstill works too.
有一种。我早在 2007 年就为 Windows 创建了 Sudo?08? 这是我写的安全论文 - https://www.sans.org/reading-room/whitepapers/bestprac/sudo-windows-sudowin-1726。很确定http://sudowin.sf.net仍然有效。
回答by ceztko
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
.
以下 vbs 脚本允许使用带有提升的参数启动给定命令,并针对一组有限的用例模拟原始 unix sudo 命令的行为(它不会缓存凭据,也不允许真正执行具有不同凭据的命令)。我戴上了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
在命令提示符下使用示例 sudo net start service