Cygwin 中的 root 用户/sudo 等效项?

发布于 2024-09-30 04:07:09 字数 275 浏览 0 评论 0原文

我正在尝试在 Cygwin 中运行 bash 脚本。

我收到 Must run as root, ie sudo ./scriptname 错误。

chmod 777 scriptname 没有任何帮助。

我一直在寻找在 Cygwin 上模仿 sudo 的方法,以添加 root 用户,因为调用“su”会呈现错误 su:用户 root 不存在,任何有用的东西,但一无所获。

有人有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

吲‖鸣 2024-10-07 04:07:09

我在 SuperUser 上回答了这个问题,但只是在OP忽略了当时唯一答案的无用答案之后。

这是在 Cygwin 中提升权限的正确方法,复制自我自己在 SuperUser 上的答案:

我在 Cygwin 邮件列表。要在 Cygwin 中以提升的权限运行命令,请在命令前加上cygstart --action=runas,如下所示:

$ cygstart --action=runas command

这将打开一个 Windows 对话框,询问管理员如果输入了正确的密码,则运行命令。

只要 ~/bin 在您的路径中,这很容易编写脚本。创建一个包含以下内容的文件 ~/bin/sudo

#!/usr/bin/bash
cygstart --action=runas "$@"

现在使该文件可执行:

$ chmod +x ~/bin/sudo

现在您可以使用真正提升的权限运行命令:

$ sudo elevatedCommand

您可能需要添加 ~/bin 到你的路径。您可以在 Cygwin CLI 上运行以下命令,或将其添加到 ~/.bashrc

$ PATH=$HOME/bin:$PATH

在 64 位 Windows 8 上测试。

您还可以将此命令的别名添加到 <代码>~/.bashrc:

# alias to simulate sudo
alias sudo='cygstart --action=runas'

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'
天荒地未老 2024-10-07 04:07:09

您可能需要以管理员身份运行 cygwin shell。您可以右键单击该快捷方式,然后单击以管理员身份运行,或者进入该快捷方式的属性并在兼容性部分中检查它。请注意... root 权限可能很危险。

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.

原来分手还会想你 2024-10-07 04:07:09

基于 dotancohen 的答案 我正在使用别名:

alias sudo="cygstart --action=runas"

作为魅力:

sudo chown User:Group <file>

如果您安装了 SysInternals,您甚至可以开始作为系统用户的命令 shell 非常容易

sudo psexec -i -s -d cmd

Building on dotancohen's answer I'm using an alias:

alias sudo="cygstart --action=runas"

Works as a charm:

sudo chown User:Group <file>

And if you have SysInternals installed you can even start a command shell as the system user very easily

sudo psexec -i -s -d cmd
败给现实 2024-10-07 04:07:09

我发现sudo-for-cygwin,也许这会起作用,它是一个客户端/服务器使用 python 脚本在 Windows (pty) 中生成子进程并桥接用户的 tty 和进程 I/O 的应用程序。

它需要windows中的python和Cygwin中的Python模块greenlet和eventlet。

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.

蛮可爱 2024-10-07 04:07:09

似乎 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

It seems that cygstart/runas does not properly handle "$@" and thus commands that have arguments containing spaces (and perhaps other shell meta-characters -- I didn't check) will not work correctly.

I decided to just write a small sudo script that works by writing a temporary script that does the parameters correctly.

#! /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
深海夜未眠 2024-10-07 04:07:09

或者安装 syswin 软件包,其中包括 cygwin 的 su 端口:http://sourceforge。网/p/manufacture/wiki/syswin-su/

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

雨的味道风的声音 2024-10-07 04:07:09

这个答案基于 另一个答案。首先,请确保您的帐户位于管理员组中。

接下来,创建一个包含以下内容的通用“runas-admin.bat”文件:

@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

然后像这样执行批处理文件:

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

例如:

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

只需确保将每个单独的命令用双引号括起来。使用此方法时,您只会收到 UAC 提示,并且此过程已被通用化,因此您可以使用任何类型的命令。

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.

云裳 2024-10-07 04:07:09

来自 GitHub 的增强 SUDO for CygWin 的新提案,位于 此线程,名为TOUACExt

  • 自动打开sudoserver.py。
  • 超时后自动关闭 sudoserver.py(默认 15 分钟)。
  • 为管理员用户请求 UAC 提升提示“是/否”样式。
  • 为非管理员用户请求管理员用户/密码。
  • 使用管理员帐户远程工作 (SSH)。
  • 创建日志。

仍处于预测试版,但似乎正在发挥作用。

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.

弥枳 2024-10-07 04:07:09

我通过谷歌来到这里,我实际上相信我已经找到了一种在 cygwin 中获得功能齐全的 root 提示的方法。

这是我的步骤。

首先,您需要将Windows管理员帐户重命名为“root”
通过打开开始菜单并输入“gpedit.msc”来执行此

操作编辑下面的条目
本地计算机策略>电脑配置> Windows 设置 >安全设置>地方政策>安全选项>帐户:重命名管理员帐户

然后,如果尚未启用该帐户,则必须启用该帐户。
本地计算机策略>电脑配置> Windows 设置 >安全设置>地方政策>安全选项>帐户:管理员帐户状态

现在注销并登录 root 帐户。

现在为 cygwin 设置环境变量。要做到这一点,简单的方法是:
右键单击我的电脑>属性

单击(左侧边栏上)“高级系统设置”

在底部附近单击“环境变量”按钮

在“系统变量”下单击“新建...”按钮

对于名称,请输入“cygwin”(不带引号)。
对于该值,请输入您的 cygwin 根目录。 (我的是 C:\cygwin )

按“确定”并关闭所有内容以返回桌面。

打开 Cygwin 终端 (cygwin.bat)

编辑文件 /etc/passwd
并改变线路

管理员:未使用:500:503:U-机器\管理员,S-1-5-21-12345678-1234567890- 1234567890-500:/home/管理员:/bin/bash

为此(您的数字和计算机名称将会不同,只需确保将突出显示的数字更改为 0!)

root:未使用:0:0:U-MACHINE\root,S-1-5-21-12345678-1234567890- 1234567890-0:/root:/bin/bash

现在所有工作都已完成,接下来的部分将使“su”命令起作用。 (不完美,但它的功能足以使用。我不认为脚本可以正常工作,但是嘿,你已经走到这一步了,也许你可以找到方法。请分享)

在 cygwin 中运行此命令以完成交易。

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

注销 root 帐户并返回到普通 Windows 用户帐户。

之后,通过在资源管理器中双击新的“su.bat”来手动运行它。输入您的密码,然后关闭窗口。

现在尝试从 cygwin 运行 su 命令,看看一切是否正常。

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

Administrator:unused:500:503:U-MACHINE\Administrator,S-1-5-21-12345678-1234567890-1234567890-500:/home/Administrator:/bin/bash

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

root:unused:0:0:U-MACHINE\root,S-1-5-21-12345678-1234567890-1234567890-0:/root:/bin/bash

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"
@ECHO OFF
RUNAS /savecred /user:root %cygwin%\cygwin.bat
EOF
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.

宛菡 2024-10-07 04:07:09

由于对可用的解决方案不满意,我采用了 nu774 的脚本来增加安全性并使其更容易设置和使用。该项目可在 Github 上找到

要使用它,只需下载 cygwin-sudo.py 并通过 python3 cygwin-sudo.py **你的命令**

为了方便起见,您可以设置别名:

alias sudo="python3 /path-to-cygwin-sudo/cygwin-sudo.py"

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"
格子衫的從容 2024-10-07 04:07:09

使用它可以从任何目录上下文菜单中获取运行 bash 或 cmd 的管理窗口。只需右键单击目录名称,然后选择条目或点击突出显示的按钮即可。

这是基于 chere 工具和来自 link_boy 的不幸的不起作用的答案(对我来说)。它对我使用 Windows 8 来说工作得很好,

副作用是管理 cmd 窗口中的颜色不同。要在 bash 上使用它,您可以更改管理员用户的 .bashrc 文件。

我无法运行“后台”版本(右键单击打开的目录)。请随意添加。

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\""

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\""
情何以堪。 2024-10-07 04:07:09

让 cygwin shell 和相应的子 shell 以管理员权限运行的一个非常简单的方法是更改​​打开初始 shell 的链接的属性。

以下内容适用于 Windows 7+(也许也适用于以前的版本,但我没有检查)

我通常从 开始 按钮(或桌面)中的 cygwin-link 启动 cygwin shell。
中更改了 cygwin-link 的属性

然后,我在选项卡/Compatibility/Privilege Level/

框,

,并选中了“以管理员身份运行此程序”

这允许 cygwin shell 以管理员权限打开以及相应的子 shell。

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.

っ〆星空下的拥抱 2024-10-07 04:07:09

我遇到这个讨论是为了寻找有关不同操作系统中 sudo 实现的一些细节。读它我发现@brian-white的解决方案(https://stackoverflow.com/a/42956057/3627676 )很有用,但可以稍微改进。我避免创建临时文件并实现通过单个脚本执行所有内容。

我还研究了单个窗口/控制台内输出的改进的下一步。不幸的是,没有任何成功。我尝试使用命名管道捕获 STDOUT/STDERR 并在主窗口中打印。但子进程没有写入命名管道。但是写入常规文件效果很好。

我放弃了寻找根本原因的任何尝试,并保留了当前的解决方案。希望我的帖子也能有用。

改进:

  • 没有临时文件,
  • 没有解析和重建命令行选项
  • ,等待提升的命令
  • 使用 minttybash,如果找不到第一个,
  • 则返回命令退出代码
    #!/bin/bash

    # Being Administrators, invoke the command directly
    id -G | grep -qw 544 && {
        "$@"
        exit $?
    }

    # The CYG_SUDO variable is used to control the command invocation
    [ -z "$CYG_SUDO" ] && {
        mintty="$( which mintty 2>/dev/null )"
        export CYG_SUDO="$"
        cygstart --wait --action=runas $mintty /bin/bash "$0" "$@"
        exit $?
    }

    # Now we are able to:
    # -- launch the command
    # -- display the message
    # -- return the exit code
    "$@"
    RETVAL=$?

    echo "$0: Press  to close window..."
    read

    exit $RETVAL

I met this discussion looking for some details on the sudo implementation in different operating systems. Reading it I found that the solution by @brian-white (https://stackoverflow.com/a/42956057/3627676) is useful but can be improved slightly. I avoided creating the temporary file and implemented to execute everything by the single script.

Also I investigated the next step of the improvement to output within the single window/console. Unfortunately, without any success. I tried to use named pipes to capture STDOUT/STDERR and print in the main window. But child process didn't write to named pipes. However writing to a regular file works well.

I dropped any attempts to find the root cause and left the current solution as is. Hope my post can be useful as well.

Improvements:

  • no temporary file
  • no parsing and reconstructing the command line options
  • wait the elevated command
  • use mintty or bash, if the first one not found
  • return the command exit code
    #!/bin/bash

    # Being Administrators, invoke the command directly
    id -G | grep -qw 544 && {
        "$@"
        exit $?
    }

    # The CYG_SUDO variable is used to control the command invocation
    [ -z "$CYG_SUDO" ] && {
        mintty="$( which mintty 2>/dev/null )"
        export CYG_SUDO="$"
        cygstart --wait --action=runas $mintty /bin/bash "$0" "$@"
        exit $?
    }

    # Now we are able to:
    # -- launch the command
    # -- display the message
    # -- return the exit code
    "$@"
    RETVAL=$?

    echo "$0: Press  to close window..."
    read

    exit $RETVAL

心病无药医 2024-10-07 04:07:09

根据@mat-khor的回答,我采用了 syswin su.exe,将其保存为manufacture-syswin-su.exe,并编写此包装器脚本。它处理命令的 stdout 和 stderr 的重定向,因此可以在管道等中使用。此外,脚本退出时会显示给定命令的状态。

限制:

  • syswin-su 选项当前已硬编码为使用当前用户。在脚本调用前面添加 env USERNAME=... 会覆盖它。如果需要其他选项,脚本必须区分 syswin-su 和命令参数,例如在第一个 -- 处分割。
  • 如果取消或拒绝 UAC 提示,脚本将挂起。

#!/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")

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")
烟─花易冷 2024-10-07 04:07:09

我自己无法完全测试这一点,我没有合适的脚本来尝试它,而且我不是 Linux 专家,但你也许能够破解一些足够接近的东西。

我已经尝试过这些步骤,它们“似乎”有效,但不知道它是否足以满足您的需求。

要解决缺少“root”用户的问题:

  • 在本地 Windows 计算机上创建一个名为“root”的用户,使其成为“管理员”组的成员
  • 将 bin/bash.exe 标记为“以管理员身份运行”用户(显然,您必须在需要时打开/关闭此功能)
  • 按住 Windows 资源管理器中的左 Shift 按钮,同时右键单击 Cygwin.bat 文件
  • 选择“以其他用户身份运行”
  • 输入 .\root as用户名,然后是您的密码。

然后,这会以 cygwin 中名为“root”的用户身份运行您,再加上 bash.exe 文件上的“以管理员身份运行”可能就足够了。

但是你仍然需要 sudo。

我通过在 /bin 中创建一个名为“sudo”的文件并使用此命令行将命令发送到 su 来伪造这个(其他具有更多 Linux 知识的人可能可以更好地伪造它):

su -c "$*"

命令行“sudo vim”和其他似乎对我来说效果不错,所以你可能想尝试一下。

有兴趣知道这是否适合您的需求。

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.

只涨不跌 2024-10-07 04:07:09

我通常做的是使用注册表“在此处打开”帮助程序,以便从计算机中的任何位置轻松打开具有管理权限的 cygwin shell。

请注意,您必须安装 cygwin“chere”软件包,请首先从提升的 cygwin shell 中使用“chere -i -m”。

假设您的 cygwin 安装在 < strong>C:\cygwin...

这是注册表代码:

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

[-HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

[-HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

希望这会有所帮助。让我知道它是否适合您。谢谢。

PS:您可以获取此代码,复制并粘贴它并将其保存在 name.reg 文件中以运行它...或者您可以手动添加值。

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

[-HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

[-HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

[-HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash]

[HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash]
@="Open Cygwin Here as Root"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Drive\shell\cygwin_bash\command]
@="c:\\cygwin\\bin\\mintty.exe -i /Cygwin-Terminal.ico -e /bin/xhere /bin/bash.exe"

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.

伏妖词 2024-10-07 04:07:09

只需简化已接受的答案,在 Cygwin 终端中复制以下内容即可完成:

cat <<EOF >> /bin/sudo
#!/usr/bin/bash
cygstart --action=runas "\$@"
EOF
chmod +x /bin/sudo

Just simplifying the accepted answer, copy past the below in a Cygwin terminal and you are done:

cat <<EOF >> /bin/sudo
#!/usr/bin/bash
cygstart --action=runas "\$@"
EOF
chmod +x /bin/sudo

请止步禁区 2024-10-07 04:07:09

尝试:

chmod -R ug+rwx <dir>

其中

是您所在的目录
想要更改权限。

Try:

chmod -R ug+rwx <dir>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文