bash Cygwin 中的 root 用户/sudo 等效项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4090301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Root user/sudo equivalent in Cygwin?
提问by Ken Bellows
I'm trying to run a bash script in Cygwin.
我正在尝试在 Cygwin 中运行 bash 脚本。
I get Must run as root, i.e. sudo ./scriptname
errors.
我收到Must run as root, i.e. sudo ./scriptname
错误。
chmod 777 scriptname
does nothing to help.
chmod 777 scriptname
没有任何帮助。
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.
我一直在寻找在 Cygwin 上模仿 sudo 的方法,添加一个 root 用户,因为调用“su”会呈现错误su: user root does not exist
,任何有用的东西,但一无所获。
Anyone have any suggestions?
有人有什么建议吗?
回答by dotancohen
I answered this question on SuperUserbut only after the OP disregarded the unhelpful answer that was at the time the only answer to the question.
我在SuperUser上回答了这个问题,但只有在 OP 无视当时是该问题唯一答案的无益答案之后。
Here is the proper way to elevate permissions in Cygwin, copied from my own answer on SuperUser:
这是在 Cygwin 中提升权限的正确方法,复制自我在 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:
我在 Cygwin 邮件列表中找到了答案。要command
在 Cygwin 中以提升的权限运行,请在命令之前添加cygstart --action=runas
如下内容:
$ 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.
这将打开一个要求输入管理员密码的 Windows 对话框,如果输入了正确的密码,则运行该命令。
This is easily scripted, so long as ~/bin
is in your path. Create a file ~/bin/sudo
with the following content:
只要~/bin
在您的路径中,这很容易编写脚本。创建一个~/bin/sudo
包含以下内容的文件:
#!/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
:
您可能需要添加~/bin
到您的路径中。您可以在 Cygwin CLI 上运行以下命令,或将其添加到~/.bashrc
:
$ PATH=$HOME/bin:$PATH
Tested on 64-bit Windows 8.
在 64 位 Windows 8 上测试。
You could also instead of above steps add an alias for this command to ~/.bashrc
:
您也可以代替上述步骤为此命令添加别名~/.bashrc
:
# alias to simulate sudo
alias sudo='cygstart --action=runas'
回答by Matt Williamson
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.
您可能需要以管理员身份运行 cygwin shell。您可以右键单击快捷方式并单击以管理员身份运行或进入快捷方式的属性并在兼容性部分中检查它。请注意.... root 权限可能很危险。
回答by thoni56
Building on dotancohen's answerI'm using an alias:
基于dotancohen 的回答,我使用了别名:
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
如果您安装了 SysInternals,您甚至可以非常轻松地以系统用户身份启动命令外壳
sudo psexec -i -s -d cmd
回答by AdamTheWebMan
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.
我发现sudo-for-cygwin,也许这会起作用,它是一个客户端/服务器应用程序,它使用 python 脚本在 windows (pty) 中生成子进程并桥接用户的 tty 和进程 I/O。
It requires python in windows and Python modules greenlet, and eventlet in Cygwin.
它需要 Windows 中的 python 和 Python 模块 greenlet,以及 Cygwin 中的 eventlet。
回答by Mat Khor
Or install syswin package, which includes a port of su for cygwin: http://sourceforge.net/p/manufacture/wiki/syswin-su/
或者安装 syswin 包,其中包括一个用于 cygwin 的 su 端口:http: //sourceforge.net/p/manufacture/wiki/syswin-su/
回答by Brian White
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.
似乎cygstart/runas
没有正确处理"$@"
,因此参数包含空格(可能还有其他 shell 元字符——我没有检查)的命令将无法正常工作。
I decided to just write a small sudo
script that works by writing a temporary script that does the parameters correctly.
我决定只编写一个小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 " \" >>$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@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
: 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
回答by Sopalajo de Arrierez
A new proposal to enhance SUDO for CygWin from GitHub in this thread, named TOUACExt:
在此线程中为来自 GitHub 的 CygWin 增强 SUDO 的新提案,名为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.
- 自动打开 sudoserver.py。
- 超时后自动关闭 sudoserver.py(默认 15 分钟)。
- 为管理员用户请求 UAC 提升提示是/否样式。
- 为非管理员用户请求管理员用户/密码。
- 使用管理员帐户远程工作 (SSH)。
- 创建日志。
Still in Pre-Beta, but seems to be working.
仍在Pre-Beta 中,但似乎正在运行。
回答by user2618594
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:
接下来,使用以下内容创建一个通用的“runas-admin.bat”文件:
./runas-admin.bat "<command1> [parm1, parm2, ...]" "<command2> [parm1, parm2, ...]"
Then execute the batch file like this:
然后像这样执行批处理文件:
./runas-admin.bat "net localgroup newgroup1 /add" "net localgroup newgroup2 /add"
For exaxmple:
例如:
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'
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.
只需确保将每个单独的命令用双引号括起来。使用此方法您只会获得一次 UAC 提示,并且此过程已被通用化,因此您可以使用任何类型的命令。
回答by Hydranix
I landed here through google, and I actually believe I've found a way to gain a fully functioning root promt in cygwin.
我是通过 google 来到这里的,实际上我相信我已经找到了一种在 cygwin 中获得功能齐全的 root promt 的方法。
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"
首先,您需要将 Windows 管理员帐户重命名为“root”,方法是打开开始菜单并输入“gpedit.msc”
Edit the entry under Local Computer Policy > Computer Configuration > Windows Settings > Security Settings > Local Policies > Security Options > Accounts: Rename administrator account
编辑本地计算机策略 > 计算机配置 > Windows 设置 > 安全设置 > 本地策略 > 安全选项 > 帐户:重命名管理员帐户下的条目
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
然后,如果尚未启用该帐户,则必须启用该帐户。本地计算机策略 > 计算机配置 > Windows 设置 > 安全设置 > 本地策略 > 安全选项 > 帐户:管理员帐户状态
Now log out and log into the root account.
现在注销并登录到 root 帐户。
Now set an environment variable for cygwin. To do that the easy way: Right Click My Computer > Properties
现在为 cygwin 设置一个环境变量。要做到这一点,方法很简单:右键单击我的电脑 > 属性
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 )
对于名称,请输入不带引号的“cygwin”。对于该值,请在您的 cygwin 根目录中输入。(我的是 C:\cygwin )
Press OK and close all of that to get back to the desktop.
按确定并关闭所有这些以返回桌面。
Open a Cygwin terminal (cygwin.bat)
打开 Cygwin 终端 (cygwin.bat)
Edit the file /etc/passwd and change the line
编辑文件 /etc/passwd 并更改行
Administrator:unused:500:503:U-MACHINE\Administrator,S-1-5-21-12345678-1234567890-1234567890-500:/home/Administrator:/bin/bash
管理员: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!)
为此(您的号码和机器名称会有所不同,只需确保将突出显示的数字更改为 0!)
root:unused:0:0:U-MACHINE\root,S-1-5-21-12345678-1234567890-1234567890-0:/root:/bin/bash
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)
现在一切都完成了,接下来的一点将使“su”命令起作用。(不完美,但它的功能足以使用。我认为脚本不会正常运行,但是,嘿,你已经走了这么远,也许你能找到方法。请分享)
Run this command in cygwin to finalize the deal.
在 cygwin 中运行此命令以完成交易。
alias sudo="python3 /path-to-cygwin-sudo/cygwin-sudo.py"
Log out of the root account and back into your normal windows user account.
注销 root 帐户并重新登录到您的普通 Windows 用户帐户。
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.
毕竟,通过在资源管理器中双击它来手动运行新的“su.bat”。输入您的密码,然后关闭窗口。
Now try running the su command from cygwin and see if everything worked out alright.
现在尝试从 cygwin 运行 su 命令,看看是否一切正常。
回答by Chronial
Being unhappy with the available solution, I adopted nu774's scriptto add security and make it easier to setup and use. The project is available on Github
由于对可用的解决方案不满意,我采用了nu774 的脚本来增加安全性并使其更易于设置和使用。该项目在 Github上可用
To use it, just download cygwin-sudo.py
and run it via python3 cygwin-sudo.py **yourcommand**
.
要使用它,只需下载cygwin-sudo.py
并通过python3 cygwin-sudo.py **yourcommand**
.
You can set up an alias for convenience:
为方便起见,您可以设置别名:
##代码##