Root user/sudo equivalent in Cygwin?

后端 未结 17 2205
旧时难觅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:25

    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.

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

    Being unhappy with the available solution, I adopted nu774's script to add security and make it easier to setup and use. The project is available on Github

    To use it, just download cygwin-sudo.py and run it via python3 cygwin-sudo.py **yourcommand**.

    You can set up an alias for convenience:

    alias sudo="python3 /path-to-cygwin-sudo/cygwin-sudo.py"
    
    0 讨论(0)
  • 2020-12-04 05:27

    Try:

    chmod -R ug+rwx <dir>
    

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

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

    Based on @mat-khor's answer, I took the syswin su.exe, saved it as manufacture-syswin-su.exe, and wrote this wrapper script. It handles redirection of the command's stdout and stderr, so it can be used in a pipe, etc. Also, the script exits with the status of the given command.

    Limitations:

    • The syswin-su options are currently hardcoded to use the current user. Prepending env USERNAME=... to the script invocation overrides it. If other options were needed, the script would have to distinguish between syswin-su and command arguments, e.g. splitting at the first --.
    • If the UAC prompt is cancelled or declined, the script hangs.

    .

    #!/bin/bash
    set -e
    
    # join command $@ into a single string with quoting (required for syswin-su)
    cmd=$( ( set -x; set -- "$@"; ) 2>&1 | perl -nle 'print $1 if /\bset -- (.*)/' )
    
    tmpDir=$(mktemp -t -d -- "$(basename "$0")_$(date '+%Y%m%dT%H%M%S')_XXX")
    mkfifo -- "$tmpDir/out"
    mkfifo -- "$tmpDir/err"
    
    cat >> "$tmpDir/script" <<-SCRIPT
        #!/bin/env bash
        $cmd > '$tmpDir/out' 2> '$tmpDir/err'
        echo \$? > '$tmpDir/status'
    SCRIPT
    
    chmod 700 -- "$tmpDir/script"
    
    manufacture-syswin-su -s bash -u "$USERNAME" -m -c "cygstart --showminimized bash -c '$tmpDir/script'" > /dev/null &
    cat -- "$tmpDir/err" >&2 &
    cat -- "$tmpDir/out"
    wait $!
    exit $(<"$tmpDir/status")
    
    0 讨论(0)
  • 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.

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