Take the 2-minute tour ×

I'm trying to run a bash script in Cygwin.

I get Must run as root, i.e. sudo ./scriptname errors.

chmod 777 scriptname does nothing to help.

I've looked for ways to imitate sudo on Cygwin, to add a root user, since calling "su" renders the error su: user root does not exist, anything useful, and have found nothing.

Anyone have any suggestions?

Edit: I thought I should close this off, since it's still getting so many views. Basically, as far as I have found through quite a bit of searching, there is no effective way at this point to run a script as a sudo user in Cygwin. I'm hoping it's added in a later version of Cygwin, but for now there's nothing out there.

Much Later Edit: AdamTheWebMan recently posted an answer that looks quite promising involving sudo-for-cygwin. I haven't accepted because I haven't had a chance to really check it out yet. But I might. I might actually accept an answer after all this time.

share|improve this question
hello KenB, could you give us more detail on what script you are trying to run? There is no equivalent to 'sudo' inside a cygwin shell - the rights are the ones from the win user that launched the cygwin shell, so KyleWpppd link is good to avoid errors such as "sudo unknown command". In your case sounds like it's a specific issue with the script you want to execute. –  Stefano Feb 4 '11 at 13:02
Honestly, this is an issue long past, and I don't actually remember what the script was. Thanks for the interest, though. –  KenB Feb 15 '11 at 2:14

11 Answers 11

I answered this question on SuperUser but only after the OP disregarded the unhelpful answer that was at the time the only answer to the question.

Here is the proper way to elevate permissions in Cygwin, copied from my own answer on SuperUser:

I found the answer on the cygwin mailing list. To run command with elevated privileges in Cygwin, precede the command with cygstart --action=runas like this:

$ cygstart --action=runas command

This will open a Windows dialogue box asking for the Admin password and run the command if the proper password is entered.

This is easily scripted, so long as ~/bin is in your path. Create a file ~/bin/sudo with the following content:

cygstart --action=runas "$@"

Now make the file executable:

$ chmod +x ~/bin/sudo

Now you can run commands with real elevated privileges:

$ sudo elevatedCommand

You may need to add ~/bin to your path. You can run the following command on the Cygwin CLI, or add it to ~/.bashrc:


Tested on 64-bit Windows 8.

share|improve this answer
Excellent answer. With some tweaks to your bash code (below), I was able to run an elevated command from the command line. Here's the change I needed to make your code work: cat << 'EOF' > ~/bin/sudo\n #!/usr/bin/bash\n cygstart --action=runas "$@"\n EOF (I can't figure out how to insert newlines in this comment, so I've added '\n's to the code) The rest from the PATH=... onward is fine. –  josmith42 Jan 10 at 14:22
@josmith42: Thank you! I don't see the difference between my script and yours. What is the difference? –  dotancohen Jan 10 at 14:27
It's mainly the first line: I have cat << 'EOF' > ~/bin/sudo. And I added EOF below the cygstart line to close off the here document. –  josmith42 Jan 10 at 14:31
Oh, I see! If I'm not mistaken, that is your way of writing the file to disk, instead of opening a text editor. Nice, thank you! I did not know how to use heredocs in Bash! –  dotancohen Jan 10 at 15:46
$ cat ~/bin/sudo cat: /home/MyName/bin/sudo: No such file or directory What could be the issue there? I have a usr/local/bin and usr/bin in my path –  user1530318 Jan 31 at 19:45

You probably need to run the cygwin shell as Administrator. You can right click the shortcut and click run as administrator or go into the properties of the shortcut and check it in the compatability section. Just beware.... root permissions can be dangerous.

share|improve this answer
I tried that, too. No dice. –  KenB Nov 3 '10 at 18:29
worked for me on Win 7 with NTFS file system –  JeffG Nov 13 '12 at 12:59
In the three years since this question has been asked without a good answer as opposed to this workaround, I've found the correct way to raise permissions for a single command in the extant Cygwin window in the answer below. Thus, it functions exactly like sudo in Linux. –  dotancohen Jan 9 at 15:44
Didn't work on Win7 –  harvest316 Nov 10 at 22:04

I found sudo-for-cygwin, maybe this would work, it is a client/server application that uses a python script to spawn a child process in windows (pty) and bridges user's tty and the process I/O.

It requires python in windows and Python modules greenlet, and eventlet in Cygwin.

share|improve this answer
This actually looks rather promising. –  KenB Sep 12 '12 at 11:37

I landed here through google, and I actually believe I've found a way to gain a fully functioning root promt in cygwin.

Here are my steps.

First you need to rename the Windows Administrator account to "root" Do this by opening start manu and typing "gpedit.msc"

Edit the entry under Local Computer Policy > Computer Configuration > Windows Settings > Security Settings > Local Policies > Security Options > Accounts: Rename administrator account

Then you'll have to enable the account if it isn't yet enabled. Local Computer Policy > Computer Configuration > Windows Settings > Security Settings > Local Policies > Security Options > Accounts: Administrator account status

Now log out and log into the root account.

Now set an environment variable for cygwin. To do that the easy way: Right Click My Computer > Properties

Click (on the left sidebar) "Advanced system settings"

Near the bottom click the "Enviroment Variables" button

Under "System Variables" click the "New..." button

For the name put "cygwin" without the quotes. For the value, enter in your cygwin root directory. ( Mine was C:\cygwin )

Press OK and close all of that to get back to the desktop.

Open a Cygwin terminal (cygwin.bat)

Edit the file /etc/passwd and change the line


To this (your numbers, and machine name will be different, just make sure you change the highlighted numbers to 0!)


Now that all that is finished, this next bit will make the "su" command work. (Not perfectly, but it will function enough to use. I don't think scripts will function correctly, but hey, you got this far, maybe you can find the way. And please share)

Run this command in cygwin to finalize the deal.

mv /bin/su.exe /bin/_su.exe_backup
cat > /bin/su.bat << "EOF"
RUNAS /savecred /user:root %cygwin%\cygwin.bat
ln -s /bin/su.bat /bin/su
echo ''
echo 'All finished'

Log out of the root account and back into your normal windows user account.

After all of that, run the new "su.bat" manually by double clicking it in explorer. Enter in your password and go ahead and close the window.

Now try running the su command from cygwin and see if everything worked out alright.

share|improve this answer

A new proposal to enhance SUDO for CygWin from GitHub in this thread, named TOUACExt:

  • Automatically opens sudoserver.py.
  • Automatically closes sudoserver.py after timeout (15 minutes default).
  • Request UAC elevation prompt Yes/No style for admin users.
  • Request Admin user/password for non-admin users.
  • Works remotely (SSH) with admin accounts.
  • Creates log.

Still in Pre-Beta, but seems to be working.

share|improve this answer

Or install syswin package, which includes a port of su for cygwin: http://sourceforge.net/p/manufacture/wiki/syswin-su/

share|improve this answer

This answer is based off of another answer. First of all, make sure your account is in the Administrators group.

Next, create a generic "runas-admin.bat" file with the following content:

@if (1==1) @if(1==0) @ELSE
    cscript //E:JScript //nologo "%~f0" %*
    @goto :EOF
FOR %%A IN (%*) DO (
@goto :EOF
@end @ELSE
args = WScript.Arguments;
newargs = "";
    newargs += "\"" + args(i) + "\" ";
ShA=new ActiveXObject("Shell.Application");
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+" "+newargs+"\"","","runas",5);

Then execute the batch file like this:

./runas-admin.bat "<command1> [parm1, parm2, ...]" "<command2> [parm1, parm2, ...]"

For exaxmple:

./runas-admin.bat "net localgroup newgroup1 /add" "net localgroup newgroup2 /add"

Just make sure to enclose each separate command in double quotes. You will only get the UAC prompt once using this method and this procedure has been generalized so you could use any kind of command.

share|improve this answer

Use this to get an admin window with either bash or cmd running, from any directories context menue. Just right click on a directory name, and select the entry or hit the highlited button.

This is based on the chere tool and the unfortunately not working answer (for me) from link_boy. It works fine for me using Windows 8,

A side effect is the different color in the admin cmd window. To use this on bash, you can change the .bashrc file of the admin user.

I coudln't get the "background" version (right click into an open directory) to run. Feel free to add it.

Windows Registry Editor Version 5.00

@="&Bash Prompt Here"

@="C:\\cygwin\\bin\\bash -c \"/bin/xhere /bin/bash.exe '%L'\""

@="&Root Bash Prompt Here"

@="runas /savecred /user:administrator \"C:\\cygwin\\bin\\bash -c \\\"/bin/xhere /bin/bash.exe '%L'\\\"\""

@="&Command Prompt Here"

@="cmd.exe /k cd %L"

@="Roo&t Command Prompt Here"

@="runas /savecred /user:administrator \"cmd.exe /t:1E /k cd %L\""
share|improve this answer

Can't fully test this myself, I don't have a suitable script to try it out on, and I'm no Linux expert, but you might be able to hack something close enough.

I've tried these steps out, and they 'seem' to work, but don't know if it will suffice for your needs.

To get round the lack of a 'root' user:

  • Create a user on the LOCAL windows machine called 'root', make it a member of the 'Administrators' group
  • Mark the bin/bash.exe as 'Run as administrator' for all users (obviously you will have to turn this on/off as and when you need it)
  • Hold down the left shift button in windows explorer while right clicking on the Cygwin.bat file
  • Select 'Run as a different user'
  • Enter .\root as the username and then your password.

This then runs you as a user called 'root' in cygwin, which coupled with the 'Run as administrator' on the bash.exe file might be enough.

However you still need a sudo.

I faked this (and someone else with more linux knowledge can probably fake it better) by creating a file called 'sudo' in /bin and using this command line to send the command to su instead:

su -c "$*"

The command line 'sudo vim' and others seem to work ok for me, so you might want to try it out.

Be interested to know if this works for your needs or not.

share|improve this answer

What I usually do is have a registry "Open Here" helper in order to open a cygwin shell with administrative privileges quite easy from anywhere in my computer.

Be aware you have to have the cygwin "chere" package installed, use "chere -i -m" from an elevated cygwin shell first.

Assuming your cygwin installation is in C:\cygwin...

Here's the registry code:

Windows Registry Editor Version 5.00


@="Open Cygwin Here as Root"

@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"


@="Open Cygwin Here as Root"



@="Open Cygwin Here as Root"


Hope this helps. Let me know if it works for you. Thanks.

PS: You can grab this code, copy and paste it and save it in a name.reg file to run it... or you can manually add the values.

share|improve this answer


chmod -R ug+rwx <dir>

where <dir> is the directory on which you want to change permissions.

share|improve this answer
How will that make him root, as he asks ? –  Nikana Reklawyks Oct 19 '12 at 23:58

Your Answer


