Cygwin 中的 root 用户/sudo 等效项?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我在 SuperUser 上回答了这个问题,但只是在OP忽略了当时唯一答案的无用答案之后。
这是在 Cygwin 中提升权限的正确方法,复制自我自己在 SuperUser 上的答案:
我在 Cygwin 邮件列表。要在 Cygwin 中以提升的权限运行
命令
,请在命令前加上cygstart --action=runas
,如下所示:这将打开一个 Windows 对话框,询问管理员如果输入了正确的密码,则运行命令。
只要
~/bin
在您的路径中,这很容易编写脚本。创建一个包含以下内容的文件~/bin/sudo
:现在使该文件可执行:
现在您可以使用真正提升的权限运行命令:
您可能需要添加
~/bin
到你的路径。您可以在 Cygwin CLI 上运行以下命令,或将其添加到~/.bashrc
:在 64 位 Windows 8 上测试。
您还可以将此命令的别名添加到 <代码>~/.bashrc:
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 withcygstart --action=runas
like this: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:Now make the file executable:
Now you can run commands with real elevated privileges:
You may need to add
~/bin
to your path. You can run the following command on the Cygwin CLI, or add it to~/.bashrc
:Tested on 64-bit Windows 8.
You could also instead of above steps add an alias for this command to
~/.bashrc
:您可能需要以管理员身份运行 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.
基于 dotancohen 的答案 我正在使用别名:
作为魅力:
如果您安装了 SysInternals,您甚至可以开始作为系统用户的命令 shell 非常容易
Building on dotancohen's answer I'm using an alias:
Works as a charm:
And if you have SysInternals installed you can even start a command shell as the system user very easily
我发现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.
似乎 cygstart/runas 无法正确处理
"$@"
,因此命令的参数包含空格(也许还有其他 shell 元字符 - 我没有检查)将无法正常工作。我决定只编写一个小的
sudo
脚本,该脚本通过编写一个正确执行参数的临时脚本来工作。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.或者安装 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/
这个答案基于 另一个答案。首先,请确保您的帐户位于管理员组中。
接下来,创建一个包含以下内容的通用“runas-admin.bat”文件:
然后像这样执行批处理文件:
例如:
只需确保将每个单独的命令用双引号括起来。使用此方法时,您只会收到 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:
Then execute the batch file like this:
For exaxmple:
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.
来自 GitHub 的增强 SUDO for CygWin 的新提案,位于 此线程,名为TOUACExt:
仍处于预测试版,但似乎正在发挥作用。
A new proposal to enhance SUDO for CygWin from GitHub in this thread, named TOUACExt:
Still in Pre-Beta, but seems to be working.
我通过谷歌来到这里,我实际上相信我已经找到了一种在 cygwin 中获得功能齐全的 root 提示的方法。
这是我的步骤。
首先,您需要将Windows管理员帐户重命名为“root”
通过打开开始菜单并输入“gpedit.msc”来执行此
操作编辑下面的条目
本地计算机策略>电脑配置> Windows 设置 >安全设置>地方政策>安全选项>帐户:重命名管理员帐户
然后,如果尚未启用该帐户,则必须启用该帐户。
本地计算机策略>电脑配置> Windows 设置 >安全设置>地方政策>安全选项>帐户:管理员帐户状态
现在注销并登录 root 帐户。
现在为 cygwin 设置环境变量。要做到这一点,简单的方法是:
右键单击我的电脑>属性
单击(左侧边栏上)“高级系统设置”
在底部附近单击“环境变量”按钮
在“系统变量”下单击“新建...”按钮
对于名称,请输入“cygwin”(不带引号)。
对于该值,请输入您的 cygwin 根目录。 (我的是 C:\cygwin )
按“确定”并关闭所有内容以返回桌面。
打开 Cygwin 终端 (cygwin.bat)
编辑文件 /etc/passwd
并改变线路
为此(您的数字和计算机名称将会不同,只需确保将突出显示的数字更改为 0!)
现在所有工作都已完成,接下来的部分将使“su”命令起作用。 (不完美,但它的功能足以使用。我不认为脚本可以正常工作,但是嘿,你已经走到这一步了,也许你可以找到方法。请分享)
在 cygwin 中运行此命令以完成交易。
注销 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
To this (your numbers, and machine name will be different, just make sure you change the highlighted numbers to 0!)
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.
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.
由于对可用的解决方案不满意,我采用了 nu774 的脚本来增加安全性并使其更容易设置和使用。该项目可在 Github 上找到。
要使用它,只需下载
cygwin-sudo.py
并通过python3 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 viapython3 cygwin-sudo.py **yourcommand**
.You can set up an alias for convenience:
使用它可以从任何目录上下文菜单中获取运行 bash 或 cmd 的管理窗口。只需右键单击目录名称,然后选择条目或点击突出显示的按钮即可。
这是基于 chere 工具和来自 link_boy 的不幸的不起作用的答案(对我来说)。它对我使用 Windows 8 来说工作得很好,
副作用是管理 cmd 窗口中的颜色不同。要在 bash 上使用它,您可以更改管理员用户的 .bashrc 文件。
我无法运行“后台”版本(右键单击打开的目录)。请随意添加。
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.
让 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.
我遇到这个讨论是为了寻找有关不同操作系统中 sudo 实现的一些细节。读它我发现@brian-white的解决方案(https://stackoverflow.com/a/42956057/3627676 )很有用,但可以稍微改进。我避免创建临时文件并实现通过单个脚本执行所有内容。
我还研究了单个窗口/控制台内输出的改进的下一步。不幸的是,没有任何成功。我尝试使用命名管道捕获 STDOUT/STDERR 并在主窗口中打印。但子进程没有写入命名管道。但是写入常规文件效果很好。
我放弃了寻找根本原因的任何尝试,并保留了当前的解决方案。希望我的帖子也能有用。
改进:
mintty
或bash
,如果找不到第一个,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:
mintty
orbash
, if the first one not found根据@mat-khor的回答,我采用了 syswin
su.exe
,将其保存为manufacture-syswin-su.exe
,并编写此包装器脚本。它处理命令的 stdout 和 stderr 的重定向,因此可以在管道等中使用。此外,脚本退出时会显示给定命令的状态。限制:
env USERNAME=...
会覆盖它。如果需要其他选项,脚本必须区分 syswin-su 和命令参数,例如在第一个--
处分割。。
Based on @mat-khor's answer, I took the syswin
su.exe
, saved it asmanufacture-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:
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--
..
我自己无法完全测试这一点,我没有合适的脚本来尝试它,而且我不是 Linux 专家,但你也许能够破解一些足够接近的东西。
我已经尝试过这些步骤,它们“似乎”有效,但不知道它是否足以满足您的需求。
要解决缺少“root”用户的问题:
然后,这会以 cygwin 中名为“root”的用户身份运行您,再加上 bash.exe 文件上的“以管理员身份运行”可能就足够了。
但是你仍然需要 sudo。
我通过在 /bin 中创建一个名为“sudo”的文件并使用此命令行将命令发送到 su 来伪造这个(其他具有更多 Linux 知识的人可能可以更好地伪造它):
命令行“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:
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:
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.
我通常做的是使用注册表“在此处打开”帮助程序,以便从计算机中的任何位置轻松打开具有管理权限的 cygwin shell。
请注意,您必须安装 cygwin“chere”软件包,请首先从提升的 cygwin shell 中使用“
chere -i -m
”。假设您的 cygwin 安装在 < strong>C:\cygwin...
这是注册表代码:
希望这会有所帮助。让我知道它是否适合您。谢谢。
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:
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.
只需简化已接受的答案,在 Cygwin 终端中复制以下内容即可完成:
Just simplifying the accepted answer, copy past the below in a Cygwin terminal and you are done:
尝试:
其中
是您所在的目录
想要更改权限。
Try:
where
<dir>
is the directory on which youwant to change permissions.