Is there a "sudo" for Windows?

Asked

Viewed 26,646 times

28

Usually it is easy to open a program/script .bat with upgrade using the option "Run as Administrator" right-click.

But what if I want to run something with elevation from the cmd, a kind of sudo, has as?

5 answers

24


The tool runas Windows is equivalent to sudo of Unix/Linux. Your syntax is:

runas [/profile | /noprofile] [/env] [/netonly | /savecred] [/showtrustlevels] [/trustlevel] /user:<UserName> "<PathToProgramFile>"

Examples

The command below executes Command Prompt as administrator(root).

runas /user:Administrator cmd

In the Unix/Linux is simply equivalent to:

sudo /bin/sh

Or through the command below:

su -

Note: If you do not use Bash you can know the shell you use from the command which sh.

If you need to load the user profile you can do:

runas /profile /env /user:<Usuario> "notepad"

In the Unix/Linux may amount to:

sudo -u <Usuario> -i

On the other hand, if you do not want to load the user profile you can use:

runas /noprofile /user:<Usuario> "notepad"

With the sudo we could use:

sudo -u root vi /etc/sudoers    

There are also external tools that can do lifting, for example the Sudo for Windows.

If you use the Conemu(is a great alternative to Windows Command Prompt) you can use the command csudo to perform a privilege action.

inserir a descrição da imagem aqui

Other alternatives can be seen here.

We could create an archive batch to accomplish this task through runas:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set "ARG=%*"

if !ARG! == "" (
  echo.Sem argumentos validos
  goto :NOARGS 
) ELSE (
  goto :EXECRUNAS
  exit /b 0
)

:NOARGS
  echo.Modo de uso:  %~n0  -u [usuario] [comando1, comando2, comandoN]
  exit /b 0

:EXECRUNAS:
if "%1" == "-u" (
  set USER=%2
  SHIFT
) ELSE (
  echo.Especifique o usuario & echo.
  goto :NOARGS
)

SHIFT
set "skip=2"

for %%I IN (!ARG!) DO IF !skip! LEQ 0 ( 
  set args=!args! %%I
) ELSE set /a skip-= 1 


for %%i IN (!args!) do (
  runas /env /user:!USER! "cmd /k \"%%i\"" 
)

echo.Tarefa Finalizada & echo.
exit /b 0

Just save the file with a name of your choice, with the extension .bat and call you through the prompt thus:

bat_sudo  -u [usuario] [comando1, comando2, comandoN]

For example:

bat_sudo -u Administrator gpedit.msc mmc regedit

Recalling that the script separates commands across spaces, so you pass a command like at 1 the script will interpret as two destintos commands.

18

By default the local user (Dominio = .) high in windows in English is Administrator.

In this way:

runes /user:. Administrator "Program path.exe"

13

Create a file sudo.bat and paste in it the following content:

@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript %temp%\sudo.tmp.vbs

If you want you can put it in the folder C:\Windows or another folder that is in the environment PATH variable, so that Windows automatically recognizes it.

Utilizing: sudo <executável> (example: sudo notepad), and he will open the window of WOW for you.

1

We know that the "runes" cannot upgrade the privilege to a specific command on the command line. What it does is execute a command with another user, in which case it may be the Administrator user.

It turns out that in the scenario, where the user just wants to raise his process level so that it is equivalent to "Run as Administrator", that exists in the context menu of each executable file, without changing the user.

In case you are using Windows within a company, and although my user is in the Local Administrators group, I don’t have the user password "Administrator", the use of "runes" does not solve. So it is no longer equivalent to "sudo".

Other alternative than the "Sudo for Windows" is to use the Elevate. It is a small utility, which does exactly what "sudo command" does.

Thus the following lines are equivalent:

sudo vim /etc/hosts

elevate notepad.exe c:\windows\system32\drivers\etc\hosts

-3

    runas /user:administrador "time 12"
  • 3

    It seems the same answer from other users, but has no explanation, could say what your answer has different?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.