Root user/sudo equivalent in Cygwin?

后端 未结 17 2206
旧时难觅i
旧时难觅i 2020-12-04 04:23

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 no

相关标签:
17条回答
  • 2020-12-04 05:13

    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.

    0 讨论(0)
  • 2020-12-04 05:13

    A very simple way to have a cygwin shell and corresponding subshells to operate with administrator privileges is to change the properties of the link which opens the initial shell.

    The following is valid for Windows 7+ (perhaps for previous versions too, but I've not checked)

    I usually start the cygwin shell from a cygwin-link in the start button (or desktop). Then, I changed the properties of the cygwin-link in the tabs

    /Compatibility/Privilege Level/

    and checked the box,

    "Run this program as an administrator"

    This allows the cygwin shell to open with administrator privileges and the corresponding subshells too.

    0 讨论(0)
  • 2020-12-04 05:16

    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:

    #!/usr/bin/bash
    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:

    $ PATH=$HOME/bin:$PATH
    

    Tested on 64-bit Windows 8.

    You could also instead of above steps add an alias for this command to ~/.bashrc:

    # alias to simulate sudo
    alias sudo='cygstart --action=runas'
    
    0 讨论(0)
  • 2020-12-04 05:16

    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
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]
    @="&Bash Prompt Here"
    "Icon"="C:\\cygwin\\Cygwin.ico"
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash\command]
    @="C:\\cygwin\\bin\\bash -c \"/bin/xhere /bin/bash.exe '%L'\""
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash_root]
    @="&Root Bash Prompt Here"
    "Icon"="C:\\cygwin\\Cygwin.ico"
    "HasLUAShield"=""
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash_root\command]
    @="runas /savecred /user:administrator \"C:\\cygwin\\bin\\bash -c \\\"/bin/xhere /bin/bash.exe '%L'\\\"\""
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_cmd]
    @="&Command Prompt Here"
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_cmd\command]
    @="cmd.exe /k cd %L"
    "HasLUAShield"=""
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_cmd_root]
    @="Roo&t Command Prompt Here"
    "HasLUAShield"=""
    
    [HKEY_CLASSES_ROOT\Directory\shell\cygwin_cmd_root\command]
    @="runas /savecred /user:administrator \"cmd.exe /t:1E /k cd %L\""
    
    0 讨论(0)
  • 2020-12-04 05:21

    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
    @echo off&SETLOCAL ENABLEEXTENSIONS
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
        cscript //E:JScript //nologo "%~f0" %*
        @goto :EOF
    )
    FOR %%A IN (%*) DO (
        "%%A"
    )
    @goto :EOF
    @end @ELSE
    args = WScript.Arguments;
    newargs = "";
    for (var i = 0; i < args.length; i++) {
        newargs += "\"" + args(i) + "\" ";
    }
    ShA=new ActiveXObject("Shell.Application");
    ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+" "+newargs+"\"","","runas",5);
    @end
    

    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.

    0 讨论(0)
  • 2020-12-04 05:22

    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.

    0 讨论(0)
提交回复
热议问题