如果您曾经使用过Linux或认识使用Linux的人,那么您可能听说过 sudo 命令。该命令是几乎每个Linux发行版的关键组件,它的作用是让您以不同的用户身份运行命令,尤其是 root 用户。在某些Linux发行版上,您可以使用su命令以 root 用户身份登录,但这被认为是高风险的,没有人这样做过。
实际上,它非常危险,以至于在Linux发行版(如Ubuntu )上默认禁用它。相反,如果您想以 root 用户身份运行命令,则必须使用sudo命令。(sudo)那么Windows呢?好吧(Well),不幸的是,大多数人都以管理员(Administrators)身份登录Windows ,这与(Windows)Linux中的 root 用户相同。但是,Microsoft已尝试通过启用(Microsoft)用户帐户控制(User Account Control) 或UAC来减轻以管理员身份登录的危险影响。
这样,即使用户具有管理权限,在该帐户下运行的应用程序也不会继承这些权限,除非用户手动授权。这有助于防止恶意软件和间谍软件感染以管理员(Administrator)身份登录的Windows用户。
因此,虽然Linux用户有 sudo 命令,但Windows用户有什么?Windows用户(Windows)是否(Are)可以使用其他替代方法来运行具有提升权限的命令?是否有适用于Windows的 sudo 命令?在本文中,我将讨论针对Windows用户的 sudo 命令的五种替代方法。
注意:其中一些工具现在已经很老了,因此它们可能适用于也可能不适用于最新版本的 Windows。 (Note: Some of these tools are quite old now, so they may or may not work with the latest versions of Windows. )
Windows Runas 命令
Windows 有runas命令,它与(runas)Linux上的 sudo 直接对应。使用 runas 命令,您可以以不同的用户或管理员的身份执行脚本、程序或命令。runas 命令的完整语法是:
runas [{/profile|/noprofile}] [/env] [/netonly] [/smartcard] [/showtrustlevels] [/trustlevel] /user:UserAccountName program
如果要打开管理命令提示符,可以键入以下内容:
runas /noprofile /user:Administrator cmd
/noprofile 不会加载当前用户配置文件。如果您需要访问用户环境变量,您可以删除它。如果要使用具有管理员权限的记事本(Notepad)打开文本文件,可以使用以下命令:
runas /user:Administrator "notepad my_file.txt"
您可以查看 runas 上的Technet 页面(Technet page)以获取有关如何使用它的更多信息。
请注意,在使用 runas 命令时,如果您安装程序或更改设置等,这些更改将针对您正在运行该命令的用户帐户进行。例如,假设您有普通用户用户 X 和管理员用户 Y。如果您登录 X,然后使用管理员(Administrator)凭据执行 runas,则会更改管理员(Administrator)的设置,而不是用户 X。
因此,如果您通过右键单击EXE文件并选择Run as Administrator来安装应用程序,它将被安装到内置的管理员(Administrator)用户配置文件中,而不是您登录的那个。如果您想要像 sudo 这样的真正提升权限而没有配置文件问题,请查看下面的下一个替代方案。
适用于 Windows 的 Sudo – Sourceforge
Sudo for Windows是一个免费程序,您可以安装它,它会给您与Linux for Windows上的 sudo 命令相同的体验。唯一的区别是Sudo for Windows “保留用户的配置文件和创建对象的所有权”(“preserves the user’s profile and ownership of created objects”),正如开发人员所说。如果您想使用提升的权限来安装应用程序或更改用户位置(如My Documents等),这真的很方便。
它会为您提供管理权限,但会将所有更改保留在当前配置文件中,而不是您用于运行命令的帐户中。该程序需要 .NET 2.0 版,您无法单独下载。为了获得 2.0,您必须安装.NET Framework 3.5,其中包括 2.0。
安装Sudo for Windows后,您需要将允许提升权限的用户帐户添加到由名为 S udoers的程序创建的特定组。右键单击我的电脑(My Computer)或这台电脑,然后单击管理(Manage)。然后展开用户和组(Users and Groups)并单击组(Groups)。你应该看到一个叫做Sudoers 的(Sudoers)。
双击Sudoers并单击添加(Add)按钮。
在下一个对话框中,单击Advanced按钮,然后单击Find Now。这将列出系统上的所有用户和组。双击(Double-click)要添加的用户。
对要添加的用户重复此步骤。然后单击“确定”,您应该会看到上面显示的“成员”列表框中列出的成员(Members)。单击确定(Click OK),现在您应该可以使用 sudo GUI和命令了。如果您右键单击某个程序,您将看到Sudo选项。
您还可以打开命令提示符并键入 sudo 以提升权限运行命令。
总的来说,它非常漂亮并且效果很好。但是,请注意,此特定程序对于通过右键单击或通过命令提示符启动(launching)程序或进程非常有用,但它不适用于运行命令行应用程序。例如,如果您想执行 sudo mkdir “c:Program Files ew”,则使用Sudo for Windows将无法使用。对于该功能,有另一个程序称为相同的东西,但由不同的开发人员。阅读(Read)下文。
适用于 Windows 的 Sudo – 卢克·桑普森
还有另一位开发人员为 Windows(Sudo for Windows)编写了另一个Sudo,它也可以让您执行命令行应用程序。那么让我们回到关于在 C:Program Files 中创建新文件夹的示例。默认情况下你不能真正做到这一点。
上面我使用的是PowerShell,但使用命令提示符也会出现同样的错误。但是,一旦您安装了Sudo for Windows,只需将 sudo 这个词添加到命令的前面,它就可以完美运行,没有错误!
要安装它,您需要打开PowerShell,然后按顺序键入以下命令:
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
set-executionpolicy unrestricted -s cu -f
scoop install sudo
如果一切正常,您应该在每个命令之后在PowerShell中看到以下输出:
就是这样!现在您可以开始输入命令并在前面添加 sudo。这个程序唯一令人讨厌的地方是UAC窗口仍然会弹出,并且您单击“是”(Yes)以使其工作。即使有那么一点点烦恼,这些好处也是值得的。
提升
Elevate是一个与(Elevate)UAC一起工作的程序,与sudo 的工作方式不同。使用Elevate,它将像 runas 命令一样将执行用户更改为管理员。(Administrator)但是,它对于在命令行或批处理文件中工作很有用。
elevate 的主要目的不是绕过UAC,而是从非提升的 shell 启动处于提升状态的进程,然后即使在命令完成后也照常继续。Elevate对编写脚本很有用,因为您不必担心尝试编写整个右键单击脚本并以管理员(Administrator)进程运行命令提示符。
适用于 Windows 的 Elevation PowerToys
对于那些在命令行上做大量工作或使用脚本和批处理文件的人,Elevation PowerToys for Windows页面有很多有用的工具和脚本。
当试图从命令行提升程序或以管理员身份运行脚本时,创建脚本提升玩具(script elevation power toys)是为了克服UAC令人沮丧的方面。(UAC)
希望(Hopefully)这些工具和程序足以让您感觉自己实际上是在Windows上使用 sudo 。它没有完美的替代品,但有很多选项可以接近。如果您使用其他东西来提升Windows中的程序、命令或脚本,请在评论中告诉我们。享受!
5 Windows Alternatives to the Linux sudo Command
If you’ve ever υѕed Linux or know someone who υses Linux, then you’ve probably heard of the sυdo command. The command is a critical component tо just about еvery Linux distribution and what it doеs is let yоu run a сommand as a different user, most notably the roоt user. On certain Linux distributiоns, yоu can log in as the root user by using the su command, but this is considered highly risky and no one ever does it.
Actually, it’s so dangerous that it’s disabled by default on Linux distributions like Ubuntu. Instead, you have to use the sudo command if you want to run a command as the root user. So what about Windows? Well, unfortunately, most people are logged into Windows as Administrators, which is the same as root user in Linux. However, Microsoft has tried to lessen the dangerous effects of being logged in as an Administrator by enabling User Account Control or UAC.
That way, even if the user has Administrative privileges, applications running under that account will not inherit those privileges unless it’s manually authorized by the user. This helps prevent malware and spyware from infecting a Windows user who is logged in as an Administrator.
So while Linux users have the sudo command, what do Windows user have? Are there alternatives that Windows users can use to run commands with elevated privileges? Is there a sudo command for Windows? In this article, I’ll talk about five alternatives to the sudo command for Windows users.
Note: Some of these tools are quite old now, so they may or may not work with the latest versions of Windows.
Windows Runas Command
Windows has the runas command, which is the direct counterpart to sudo on Linux. Using the runas command, you can execute a script, program or command as a different user or as an administrator. The full syntax for the runas command is:
runas [{/profile|/noprofile}] [/env] [/netonly] [/smartcard] [/showtrustlevels] [/trustlevel] /user:UserAccountName program
If you wanted to open an administrative command prompt, you could type the following:
runas /noprofile /user:Administrator cmd
/noprofile will not load the current user profile. You can remove that if you need access to the user environment variables. If you wanted to open a text file using Notepad with administrator privileges, you could use this command:
runas /user:Administrator "notepad my_file.txt"
You can checkout the Technet page on runas for more information on how to use it.
Note that when using runas command, if you install a program or make changes to settings, etc., the changes will be made to the user account that you are running the command on. For example, let’s say you have user X who is a normal user and user Y, who is an administrator. If you log into X and then do a runas using the Administrator credentials, changes will be made to the Administrator’s settings, not user X.
So if you install an application by right-clicking on the EXE file and choosing Run as Administrator, it’ll get installed to the built-in Administrator user profile, not the one you are logged into. If you want true elevated privileges like sudo without the profile issues, check out the next alternative below.
Sudo for Windows – Sourceforge
Sudo for Windows is a free program you can install that will give you the same experience of the sudo command on Linux for Windows. The only difference is that Sudo for Windows “preserves the user’s profile and ownership of created objects” as stated by the developer. This is really handy if you like to use elevated permissions for installing apps or making changes to user locations like My Documents, etc.
It’ll give you Administrative privileges, but will keep all the changes in the current profile instead of the account you’re using to run the command with. The program requires .NET version 2.0, which you can’t download individually. In order to get 2.0, you have to install .NET Framework 3.5, which include 2.0.
Once you install Sudo for Windows, you need to add the user accounts that you to allow to have elevated privileges to a specific group created by the program called Sudoers. Right-click on My Computer or This PC and click on Manage. Then expand Users and Groups and click on Groups. You should see one called Sudoers.
Double-click on Sudoers and click on the Add button.
On the next dialog, click the Advanced button and then click Find Now. This will list out all users and groups on the system. Double-click the user you want to add.
Repeat this step for however many users you want to add. Then click OK and you should see the members listed in the Members listbox shown above. Click OK and now you should be able to use the sudo GUI and command. If you right-click on a program, you’ll see the Sudo option.
You can also open a command prompt and type sudo to run command with elevated permissions.
Overall, it’s pretty nifty and works very well. However, note that this particular program is really useful for launching programs or processes either via right-click or via the command prompt, but it’s not meant for running command line apps. For example, if you wanted to do sudo mkdir “c:\Program Files\new”, this won’t work using Sudo for Windows. For that functionality, there is another program called the same thing, but by a different developer. Read below.
Sudo for Windows – Luke Sampson
There’s another developer who wrote another Sudo for Windows that lets you execute command line apps too. So let’s go back to the example about creating a new folder in C:\Program Files. You can’t really do this by default.
Above I’m using PowerShell, but you’ll get the same error using the command prompt also. However, once you install Sudo for Windows, just add the word sudo to the front of the command and it works perfectly with no errors!
To install it, you need to open PowerShell and then type in the following commands in order:
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
set-executionpolicy unrestricted -s cu -f
scoop install sudo
If everything works OK, you should see the following output in PowerShell after each command:
That’s it! Now you can start typing in commands and adding sudo in front. The only annoying thing about this program is that the UAC window still pops up and you have click Yes for it to work. Even with that slight annoyance, the benefits are well worth it.
Elevate
Elevate is a program that works with UAC and doesn’t work exactly like sudo. With Elevate, it will change the executing user to Administrator like the runas command does. However, it’s useful for working in the command line or with batch files.
The main purpose of elevate is not to get around UAC, but to start a process in an elevated state from a non-elevated shell and then continuing on as normal even after the command has completed. Elevate is useful for scripting because you don’t have to worry about trying to script the whole right-clicking and running a command prompt as Administrator process.
Elevation PowerToys for Windows
For those of you who do a lot of work on the command line or work with scripts and batch files, then the Elevation PowerToys for Windows page has quite a few useful tools and scripts.
The script elevation power toys were created to overcome the frustrating aspects of UAC when trying to elevate a program from the command line or running scripts as administrators.
Hopefully, that’s enough tools and programs for you to make you feel like you’re actually using sudo on Windows. There’s no perfect replacement for it, but there are quite a few options that come close. If you use something else to elevate programs, commands or scripts in Windows, let us know in the comments. Enjoy!