You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows Server 2012 R2的PowerShell中运行Cygwin配置脚本?

Can You Run Cygwin Expect Scripts Directly in PowerShell?

Yes, you absolutely can run your expect cygwin_config command directly from PowerShell without launching the full Cygwin environment—you just need to reference the Cygwin binaries explicitly and ensure the environment is properly initialized. Here's how to do it, plus fixes for common issues like the mintty closing problem:

1. Directly Execute the Expect Script from PowerShell

Cygwin installs its binaries (like expect) in a directory typically located at C:\cygwin64\bin (or C:\cygwin\bin for 32-bit installations). To run your script, use the full path to the expect executable and pass your config script as an argument.

Example Command:

& "C:\cygwin64\bin\expect.exe" "C:\path\to\your\cygwin_config"
  • Replace C:\cygwin64\bin with your actual Cygwin bin directory.
  • Replace C:\path\to\your\cygwin_config with the full path to your expect script (Windows-style paths work fine here, or you can use Cygwin-style paths like /home/youruser/cygwin_config if preferred).

Pro Tip for Environment Dependencies:

If your script relies on Cygwin-specific environment variables (like PATH including Cygwin tools), call Cygwin's bash.exe to wrap the command—this ensures all necessary variables are set correctly:

& "C:\cygwin64\bin\bash.exe" -c "expect /path/to/cygwin_config"

2. Fixing the Mintty Closing Issue

When you launch mintty.exe directly, it exits immediately because it’s a terminal emulator that needs a persistent command or shell to run. To keep it open while executing your script and view output/errors, use the -e flag to pass the expect command, plus a pause:

& "C:\cygwin64\bin\mintty.exe" -e bash -c "expect /path/to/cygwin_config; read -p 'Press Enter to exit...'"

This runs your script and waits for you to press Enter before closing the window, so you can see what’s happening under the hood.

3. If All Else Fails: Manual User Creation for ssh-host-config

If the script still won’t run, you can manually create the sshd user required by ssh-host-config with these steps (run PowerShell as administrator):

  • Add the sshd user to Cygwin’s passwd file:
    & "C:\cygwin64\bin\mkpasswd.exe" -l -u sshd >> "C:\cygwin64\etc\passwd"
    
  • Add the user to the corresponding group:
    & "C:\cygwin64\bin\mkgroup.exe" -l -g sshd >> "C:\cygwin64\etc\group"
    
  • Set correct permissions on critical directories:
    & "C:\cygwin64\bin\chmod.exe" 755 /var/empty
    & "C:\cygwin64\bin\chown.exe" sshd:sshd /var/empty
    

After this, you can automate the SSH host configuration directly from PowerShell:

& "C:\cygwin64\bin\bash.exe" -c "ssh-host-config -y"

The -y flag answers yes to all prompts, skipping manual input.


内容的提问来源于stack exchange,提问作者Mira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:26:02