Sunday, May 8, 2016

Be the master : Creating a sudo look-a-like command for Windows

It's been quite some time that I have written a blog for Windows or related technologies. Though most of my time these days is spent on Linux, Fedora to be specific, it's not that I have joined the Windows haters club. I love technology in all shapes and sizes, platform no bar.

But being a Linux person at heart now, whenever I pay a visit to my Windows 10 Pro installation, somethings just hit me hard in the face. There are commands in Linux that don't exist in Windows, and which I rely on so much in my day to day work that I find it hard to live without them. Of course, some of these like vim, emacs, nano etc can be installed and with the Cygwin environment plus talks of Ubuntu over Windows, we will have enough of Linux within Windows pretty soon. But still, Windows in itself is a quite powerful OS and many of the Unix like functionalities are hidden beneath its surface (that actually is the problem, hidden beneath, out of normal user's reach).

Anyways, one such important command that I miss is sudo. Sudo is the command used to temporarily elevate the authorization level of a normal user to administrator for running a command in Linux. An equivalent command exists in Windows (well, kind of) which can do the same task but has a bit cryptic syntax. That command is RunAs command, which allows an executable to run with different user credentials. The below syntax makes this command work almost like sudo:

runas /user:Administrator /savecred /env "%*"
This will run the command passed to it with administrative privileges and preserve the current users environment variables etc. It will also save the credentials, so the command will not ask for admin password again till the command window is closed. If you want to run the command with Administrator user's environment (i.e. like "sudo - " command), just remove the /env flag. The %* at the end passes all the parameters passed to sudo.bat to the runas command.



To make it easier to use, copy paste this command in notepad and save it as sudo.bat or sudo.cmd somewhere. Then using file explorer, move it to your %WINDIR% (usually, C:\Windows) directory. On Windows 10, there is one more additional step to do. The administrator account is disabled by default under Windows 10, and for successfully running this command, you need that enabled. In order to do that:
  • Go to Start>Run. Type lusrmgr.msc and press enter to run Local Users and Groups management console.
  • Go to Users. Right Click Administrator and click properties. Clear the checkbox "Account is disabled". Click OK to close the properties dialog.
  • Again right click Administrator > Set Password... and set a password for the user. Please use a strong password otherwise the security of your system can be compromised. This is a no-questions-asked kind of admin account so handle with care.

Post this, you can run sudo <command> from any cmd or powershell window to elevate the privileges of the command you are running.



Happy computing!
Post a Comment