dot*_*hen 214
我在SuperUser上回答了这个问题,但只是在OP无视当时唯一答案的无用答案之后.
以下是提升Cygwin权限的正确方法,从我在SuperUser上的答案中复制:
我在cygwin邮件列表上找到了答案.要command在Cygwin中使用提升的权限运行,请在命令前添加cygstart --action=runas如下:
$ cygstart --action=runas command
Run Code Online (Sandbox Code Playgroud)
这将打开一个Windows对话框,询问管理员密码并在输入正确密码时运行命令.
这很容易编写脚本,只要~/bin在你的路径中.~/bin/sudo使用以下内容创建文件:
#!/usr/bin/bash
cygstart --action=runas "$@"
Run Code Online (Sandbox Code Playgroud)
现在让文件可执行:
$ chmod +x ~/bin/sudo
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用真正的提升权限运行命令:
$ sudo elevatedCommand
Run Code Online (Sandbox Code Playgroud)
您可能需要添加~/bin到您的路径中.您可以在Cygwin CLI上运行以下命令,或将其添加到~/.bashrc:
$ PATH=$HOME/bin:$PATH
Run Code Online (Sandbox Code Playgroud)
在64位Windows 8上测试.
Mat*_*son 48
您可能需要以管理员身份运行cygwin shell.您可以右键单击快捷方式,然后单击以管理员身份运行或进入快捷方式的属性并在兼容性部分中进行检查.请注意...... root权限可能很危险.
tho*_*i56 22
基于dotancohen的回答,我使用的是别名:
alias sudo="cygstart --action=runas"
Run Code Online (Sandbox Code Playgroud)
充当魅力:
sudo chown User:Group <file>
Run Code Online (Sandbox Code Playgroud)
如果安装了SysInternals,您甚至可以非常轻松地以系统用户身份启动命令shell
sudo psexec -i -s -d cmd
Run Code Online (Sandbox Code Playgroud)
小智 9
我找到了sudo-for-cygwin,也许这会起作用,它是一个客户端/服务器应用程序,它使用python脚本在windows(pty)中生成子进程并桥接用户的tty和进程I/O.
它需要Windows中的python和Python模块greenlet,以及Cygwin中的eventlet.
似乎cygstart/runas不能正确处理"$@",因此参数包含空格(也许还有其他 shell 元字符——我没有检查)的命令将无法正常工作。
我决定只编写一个小sudo脚本,通过编写一个正确执行参数的临时脚本来工作。
#! /bin/bash
# If already admin, just run the command in-line.
# This works on my Win10 machine; dunno about others.
if id -G | grep -q ' 544 '; then
"$@"
exit $?
fi
# cygstart/runas doesn't handle arguments with spaces correctly so create
# a script that will do so properly.
tmpfile=$(mktemp /tmp/sudo.XXXXXX)
echo "#! /bin/bash" >>$tmpfile
echo "export PATH=\"$PATH\"" >>$tmpfile
echo "$1 \\" >>$tmpfile
shift
for arg in "$@"; do
qarg=`echo "$arg" | sed -e "s/'/'\\\\\''/g"`
echo " '$qarg' \\" >>$tmpfile
done
echo >>$tmpfile
# cygstart opens a new window which vanishes as soon as the command is complete.
# Give the user a chance to see the output.
echo "echo -ne '\n$0: press <enter> to close window... '" >>$tmpfile
echo "read enter" >>$tmpfile
# Clean up after ourselves.
echo "rm -f $tmpfile" >>$tmpfile
# Do it as Administrator.
cygstart --action=runas /bin/bash $tmpfile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157348 次 |
| 最近记录: |