Tuesday, January 13, 2015

Windows su and sudo

Wherein I describe a marginal hack for creating su and sudo utilities for Windows.

Frequently I want to get right to work adjusting services configuration with sc.exe.  Or, I might not even need a command prompt in order to figure out what I need -- sometimes I just want to run SysInternals' Process Explorer as an administrator so I can read the thread information and get on with my life.  In these instances, it usually breaks my concentration to now have to go open the system32 folder, locate cmd.exe, right-click it, and click Run as administrator.

Wouldn't it be nice to just be able to hit Win+R (to invoke the Run dialog) and type:

su

...or...

sudo procexp

?

I think so.  And to that end, here is how to set that up.

Elevated Command Prompt (su)


Open a directory that is in your path.  For example, hit Win+R to get the Run dialog, type . (that is a dot), and press return -- most likely, your user profile directory will open.

Right-click an empty area in this directory and click New > Shortcut.

In the location editbox, type %COMSPEC% (which is an environment variable that expands to the location of cmd.exe):


Click Next.  In the name editbox, type su:


Click Finish.  Now, right-click the new file, su.lnk (it will appear as simply su if you do not have Windows Explorer configured to display file extensions).  Under the Start in editbox, you may choose to type %USERPROFILE% or another location so that you start out in the directory of your choosing instead of C:\Windows\System32:


Click the Advanced...  button.  Check the Run as administrator checkbox.  Click OK twice.


Once this is done, if you created the shortcut in a directory that is in your path, then you should be able to hit Win+R, type su, press Enter, acknowledge the UAC dialog, and immediately have an elevated command prompt.

Elevated Single Command (sudo)


For sudo, you will need a shortcut that can accept a parameter to be run as an elevated command.  Copy and paste the shortcut from above, renaming it sudo.  Right-click the shortcut and in the Target editbox, type %COMSPEC% /c start ""


Don't neglect the pair of double quotes -- I'll explain later.  Save the shortcut, and now you can hit Win+R, type sudo procexp (assuming SysInternals' Process Explorer is in your path) and spawn a privileged instance of Process Explorer without having to go find the executable, right-click on it, and select "Run as Administrator".

There you have it.  The su and sudo commands, for Windows.

A Digression


Okay, you might already know that cmd.exe /c will run the command interpreter and execute the command that is specified after the /c switch.  But you might be wondering why the pair of double quotes is necessary after the START command.  As with everything else in Windows, it is because the command interpreter and its minions are made of pain.

If you don't specify these empty quotes, then some day you're going to run sudo followed by something surrounded in quotes.  That day, you will have to go manually start your privileged whatever-the-hell-you-were-doing, because the start command interprets the first quoted string it sees as a title to be displayed in the console window hosting the command interpreter.  So, on that day, start will swallow your argument to sudo and never find any actual command to run.  Then, it will exit immediately, leaving you wondering when the hell tcpview is going to pop up.  Did you have a late night, tcpview?  Are you coming in to work today?  No.  The START command ruined everything.  Because that's what Windows commands do.  They ruin everything.

If you don't believe me, take a look at the first few lines of the help output from the START /? command:


Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE ] [/AFFINITY ] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.

It's subtle, isn't it?  And because the actual help for this command is over a hundred lines long, it might take you a while to put two and two together.

Just be glad we're having this conversation so you can move on with your life.  Speaking of which, I've got some sudo commands to run.  Peace.

No comments:

Post a Comment