简体   繁体   English

在 Windows 上启动 Git Bash 时运行 SSH 代理

[英]Running SSH Agent when starting Git Bash on Windows

I am using git bash.我正在使用 git bash。 I have to use我必须使用

eval `ssh-agent.exe`
ssh-add /my/ssh/location/

every time when I start a new git bash.每次我开始一个新的 git bash 时。

Is there a way to set ssh agent permanently?有没有办法永久设置 ssh 代理? Or does windows has a good way to manage the ssh keys?或者 Windows 是否有管理 ssh 密钥的好方法?

I'm a new guy, please give me detailed tutorial, thanks!我是新手,请给我详细的教程,谢谢!

2013: In a git bash session, you can add a script to ~/.profile or ~/.bashrc ( with ~ being usually set to %USERPROFILE% ), in order for said session to launch automatically the ssh-agent . 2013:在git bash会话中,您可以将脚本添加到~/.profile~/.bashrc~通常设置为%USERPROFILE% ),以便所述会话自动启动ssh-agent
If the file doesn't exist, just create it.如果文件不存在,只需创建它。

This is what GitHub describes in " Working with SSH key passphrases ".这就是 GitHub 在“ 使用 SSH 密钥密码短语”中描述的内容。

The " Auto-launching ssh-agent on Git for Windows " section of that article has a robust script that checks if the agent is running or not.该文章的“ 在 Git for Windows 上自动启动 ssh-agent ”部分有一个健壮的脚本,用于检查代理是否正在运行。
Below is just a snippet, see the GitHub article for the full solution.下面只是一个片段,完整的解决方案请参见 GitHub 文章。

# This is just a snippet. See the article above.
if ! agent_is_running; then
    agent_start
    ssh-add
elif ! agent_has_keys; then
    ssh-add
fi

Other Resources:其他资源:

" Getting ssh-agent to work with git run from windows command shell " has a similar script, but I'd refer to the GitHub article above primarily, which is more robust and up to date. Getting ssh-agent to work with git run from windows command shell ”有一个类似的脚本,但我主要参考上面的 GitHub 文章,它更健壮且最新。


hardsetting adds in the comments (2018): hardsetting评论中添加(2018 年):

If you want to enter the passphrase the first time you need it, and not when opening a shell, the cleanest way to me is:如果您想在第一次需要时输入密码,而不是在打开 shell 时输入,对我来说最干净的方法是:

This way you don't even have to remember running ssh-add .这样您甚至不必记住运行ssh-add


And Tao adds in the comments (2022):评论中补充说(2022):

It's worth noting why this script makes particular sense in Windows, vs (for example) the more standard linuxey script noted by @JigneshGohel in another answer :值得注意的是,为什么这个脚本在 Windows 中特别有意义,而不是(例如) @JigneshGohel另一个答案中提到的更标准的 linuxey 脚本:

By not relying on the SSH_AGENT_PID at all, this script works across different msys & cygwin environments.通过完全不依赖SSH_AGENT_PID ,这个脚本可以在不同的 msys 和 cygwin 环境中工作。
An agent can be started in msys2, and still used in git bash , as the SSH_AUTH_SOCK path can be reached in either environment.代理可以在 msys2 中启动,并且仍然在git bash中使用,因为在任一环境中都可以访问SSH_AUTH_SOCK路径。
The PID from one environment cannot be queried in the other, so a PID-based approach keeps resetting/creating new ssh-agent processes on each switch.无法在另一个环境中查询来自一个环境的 PID,因此基于 PID 的方法会在每个交换机上不断重置/创建新ssh-agent进程。

PS: These instructions are in context of a Bash shell opened in Windows 10 Linux Subsystem and doesn't mention about sym-linking SSH keys generated in Windows with Bash on Ubuntu on Windows PS:这些说明是在 Windows 10 Linux 子系统中打开的 Bash shell 的上下文中,并没有提到在 Windows 上使用 Bash 在 Windows 上的 Ubuntu 上生成的符号链接 SSH 密钥

1) Update your .bashrc by adding following in it 1)通过在其中添加以下内容来更新您的.bashrc

# Set up ssh-agent
SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initializing new SSH agent..."
    touch $SSH_ENV
    chmod 600 "${SSH_ENV}"
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' >> "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    kill -0 $SSH_AGENT_PID 2>/dev/null || {
        start_agent
    }
else
    start_agent
fi

2) Then run $ source ~/.bashrc to reload your config. 2) 然后运行$ source ~/.bashrc重新加载你的配置。

The above steps have been taken from https://github.com/abergs/ubuntuonwindows#2-start-an-bash-ssh-agent-on-launch以上步骤取自https://github.com/abergs/ubuntuonwindows#2-start-an-bash-ssh-agent-on-launch

3) Create a SSH config file, if not present. 3) 创建一个 SSH 配置文件(如果不存在)。 Use following command for creating a new one: .ssh$ touch config使用以下命令创建一个新的: .ssh$ touch config

4) Add following to ~/.ssh/config 4) 将以下内容添加到~/.ssh/config

Host github.com-<YOUR_GITHUB_USERNAME> 
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_work_gmail # path to your private key
AddKeysToAgent yes


Host csexperimental.abc.com
IdentityFile ~/.ssh/id_work_gmail # path to your private key
AddKeysToAgent yes

<More hosts and github configs can be added in similar manner mentioned above>

5) Add your key to SSH agent using command $ ssh-add ~/.ssh/id_work_gmail and then you should be able to connect to your github account or remote host using ssh. 5) 使用命令$ ssh-add ~/.ssh/id_work_gmail将您的密钥添加到 SSH 代理,然后您应该能够使用 ssh 连接到您的 github 帐户或远程主机。 For eg in context of above code examples:例如,在上述代码示例的上下文中:

$ ssh github.com-<YOUR_GITHUB_USERNAME>

or或者

$ ssh <USER>@csexperimental.abc.com

This adding of key to the SSH agent should be required to be performed only one-time.这种向 SSH 代理添加密钥应该只需要执行一次。

6) Now logout of your Bash session on Windows Linux Subsystem ie exit all the Bash consoles again and start a new console again and try to SSH to your Github Host or other host as configured in SSH config file and it should work without needing any extra steps. 6) 现在注销您在 Windows Linux 子系统上的 Bash 会话,即再次退出所有 Bash 控制台并再次启动一个新控制台并尝试通过 SSH 连接到您的 Github 主机或 SSH 配置文件中配置的其他主机,它应该可以正常工作而无需任何额外脚步。

Note:笔记:

Thanks.谢谢。

If the goal is to be able to push to a GitHub repo whenever you want to, then in Windows under C:\Users\tiago\.ssh where the keys are stored (at least in my case), create a file named config and add the following in it如果目标是能够随时推送到 GitHub 存储库,那么在 Windows 中存储密钥的C:\Users\tiago\.ssh下(至少在我的情况下),创建一个名为 config 和在其中添加以下内容

Host github.com
    HostName github.com
    User your_user_name
    IdentityFile ~/.ssh/your_file_name

Then simply open Git Bash and you'll be able to push without having to manually start the ssh-agent and adding the key.然后只需打开 Git Bash,您就可以推送而无需手动启动 ssh-agent 并添加密钥。

I found the smoothest way to achieve this was using Pageant as the SSH agent and plink.我发现实现这一点的最流畅的方法是使用 Pageant 作为 SSH 代理和 plink。

You need to have a putty session configured for the hostname that is used in your remote.您需要为远程中使用的主机名配置一个腻子会话。

You will also need plink.exe which can be downloaded from the same site as putty.您还需要 plink.exe,它可以从与 putty 相同的站点下载。

And you need Pageant running with your key loaded.而且您需要在加载密钥的情况下运行 Pageant。 I have a shortcut to pageant in my startup folder that loads my SSH key when I log in.我的启动文件夹中有一个选美的快捷方式,当我登录时它会加载我的 SSH 密钥。

When you install git-scm you can then specify it to use tortoise/plink rather than OpenSSH.当您安装 git-scm 时,您可以指定它使用 tortoise/plink 而不是 OpenSSH。

The net effect is you can open git-bash whenever you like and push/pull without being challenged for passphrases.最终效果是您可以随时打开 git-bash 并推/拉,而不会受到密码短语的挑战。

Same applies with putty and WinSCP sessions when pageant has your key loaded. Same applies with putty and WinSCP sessions when pageant has your key loaded. It makes life a hell of a lot easier (and secure).它使生活变得更加轻松(和安全)。

I could not get this to work based off the best answer, probably because I'm such a PC noob and missing something obvious.根据最佳答案,我无法让它工作,可能是因为我是一个 PC 菜鸟,并且缺少一些明显的东西。 But just FYI in case it helps someone as challenged as me, what has FINALLY worked was through one of the links here (referenced in the answers).但仅供参考,以防它帮助像我一样受到挑战的人,最终起作用的是通过此处的链接之一(在答案中引用)。 This involved simply pasting the following to my .bash_profile :这涉及简单地将以下内容粘贴到我的.bash_profile

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

I probably have something configured weird, but was not successful when I added it to my .profile or .bashrc .我可能配置了一些奇怪的东西,但是当我将它添加到我的.profile.bashrc时没有成功。 The other real challenge I've run into is I'm not an admin on this computer and can't change the environment variables without getting it approved by IT, so this is a solution for those that can't access that.我遇到的另一个真正的挑战是我不是这台计算机上的管理员,并且在没有得到 IT 部门批准的情况下无法更改环境变量,因此对于那些无法访问的人来说,这是一个解决方案。

You know it's working if you're prompted for your ssh password when you open git bash.如果在打开 git bash 时系统提示您输入 ssh 密码,您就知道它正在工作。 Hallelujah something finally worked.哈利路亚终于奏效了。

Put this in your ~/.bashrc (or a file that's source'd from it) which will stop it from being run multiple times unnecessarily per shell:把它放在你的 ~/.bashrc (或一个来自它的文件)中,这将阻止它在每个 shell 中不必要地运行多次:

if [ -z "$SSH_AGENT_PID" ]; then
        eval `ssh-agent -s`
fi

And then add "AddKeysToAgent yes" to ~/.ssh/config:然后将“AddKeysToAgent yes”添加到~/.ssh/config:

Host *
    AddKeysToAgent yes

ssh to your server (or git pull) normally and you'll only be asked for password/passphrase once per session.通常通过 ssh 连接到您的服务器(或 git pull),并且每次会话只会要求您输入一次密码/密码。

As I don't like using putty in Windows as a workaround, I created a very simple utility ssh-agent-wrapper .由于我不喜欢在 Windows 中使用 putty 作为解决方法,因此我创建了一个非常简单的实用程序ssh-agent-wrapper It scans your .ssh folders and adds all your keys to the agent.它会扫描您的 .ssh 文件夹并将您的所有密钥添加到代理。 You simply need to put it into Windows startup folder for it to work.您只需将其放入 Windows 启动文件夹即可运行。

Assumptions :假设

  • ssh-agent in path路径中的 ssh 代理
  • shh-add in path (both by choosing the "RED" option when installing git shh-add in path(安装 git 时都选择“RED”选项)
  • private keys are in %USERPROFILE%/.ssh folder私钥在 %USERPROFILE%/.ssh 文件夹中
  • private keys names start with id (eg id_rsa)私钥名称以 id 开头(例如 id_rsa)

I wrote a script and created a git repository, which solves this issue here: https://github.com/Cazaimi/boot-github-shell-win .我编写了一个脚本并创建了一个 git 存储库,它在这里解决了这个问题: https ://github.com/Cazaimi/boot-github-shell-win。

The readme contains instructions on how to set the script up, so that each time you open a new window/tab the private key is added to ssh-agent automatically, and you don't have to worry about this, if you're working with remote git repositories.自述文件包含有关如何设置脚本的说明,以便每次打开新窗口/选项卡时,私钥都会自动添加到ssh-agent ,如果您正在工作,则不必担心使用远程 git 存储库。

Create a new .bashrc file in your ~ directory.在 ~ 目录中创建一个新的.bashrc文件。

There you can put your commands that you want executed everytime you start the bash每次启动 bash 时,您都可以在此处放置要执行的命令

Simple two string solution from this answer :此答案中的简单两个字符串解决方案

For sh , bash , etc:对于shbash等:

# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh

For csh , tcsh , etc:对于cshtcsh等:

# ~/.schrc
sh -c 'if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -c > ~/.ssh-agent.tcsh; fi'
eval `cat ~/.ssh-agent.tcsh`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Windows 10 上使用 Git Bash 启动 SSH 代理 - Can't start SSH Agent with Git Bash on Windows 10 sh脚本不会将ssh密钥添加到ssh-agent(windows git bash) - sh script doesn't add ssh key to ssh-agent (windows git bash) 在Windows 7中启动Git Bash时出错 - Error starting Git Bash in windows 7 Git Bash 在 ZAEA23489E8AA9B6406EBB2 上运行 bash 脚本时与斜杠“/”混淆 - Git Bash confuse with Slash '/' when running bash script on Windows 如何在 VS Code 中使用 Windows 内置的 OpenSSH ssh-agent,而不是 Git bash? - How can I use Windows' built-in OpenSSH ssh-agent in VS Code, instead of Git bash's? 如何在 Windows 上删除 git bash ssh 身份验证 - How to remove git bash ssh authentication on windows 使用 cygwin ssh-agent 正在运行,但 git 仍在提示输入密码 - Using cygwin ssh-agent is running but git is still prompting for passphrase 保持SSH在Windows 10 Bash上运行 - Keep SSH running on Windows 10 Bash Windows Git-bash致命:无法从远程存储库读取。 当推SSH - Windows Git-bash fatal: Could not read from remote repository. when pushing through ssh 在使用本机 OpenSSH 的 ssh-agent 配置良好的 Windows 10 上,如何使用配置的 ssh-agent 实现 git? - On Windows 10 that is well configured with native OpenSSH's ssh-agent, how to have an implementation of git use the ssh-agent configured?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM