What does every self respecting *nix person pine for when they’re using Windows? Emacs? vi? grep? Well… apart from those, yeah su(do). Luckily, implementing it is actually very easy and you don’t have to download cygwin to get it to work. Why not make their working landscape a little bit more like home this festive season by doing this small thing for them, I’m sure they’ll be grateful.
Firstly, you need to add the framework of the command to cmd’s registry entry so it’ll be loaded for them everytime they run it. All this consists of is a simple use of the doskey command. Doskey works like C’s macro system with batch file style arguments and expansion, so text substitution on steroids. Leverage it so that ‘sudo’ will expand to the ‘runas’ command launching the supplied command under the admin account. In reg command parlance that’s:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /d "doskey sudo=runas /profile /user:<Admin> \"cmd /C $*\""
The command runas will start breaks down as follows:
cmd – The shell, obviously
/C – Close the shell after the command has finished
$* – Magical incantation meaning ‘all supplied variables’
There we go, you’re now the proud owner of sudo. Restart cmd for the changes to take effect, and test away:
C:\Users\Adrian\desktop>sudo ping google.co.uk Enter the password for root: Attempting to start cmd /C ping google.co.uk as user "root" ... <Meanwhile in another window> Pinging google.co.uk [220.127.116.11] with 32 bytes of data: ...
With more judicious use of argument expansion you can get closer to actual su implementation such as impersonating particular users etc, but that can be your gift from me 🙂