在 Windows 上启动 Git Bash 时运行 SSH 代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18404272/
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
Running SSH Agent when starting Git Bash on Windows
提问by zchholmes
I am using git bash. I have to use
我正在使用 git bash。我必须使用
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? Or does windows has a good way to manage the ssh keys?
有没有办法永久设置 ssh 代理?或者windows有管理ssh密钥的好方法吗?
I'm a new guy, please give me detailed tutorial, thanks!
我是新手,请给我详细的教程,谢谢!
采纳答案by VonC
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
. If the file doesn't exist, just create it.
在一个Git bash命令,你可以添加一个脚本~/.profile
或~/.bashrc
(与~
被通常设置为%USERPROFILE%
)自动,为了表示会话启动ssh-agent
。如果文件不存在,只需创建它。
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. Below is just a snippet, see the GitHub article for the full solution.
该文章的“在 Git for Windows 上自动启动 ssh-agent”部分有一个强大的脚本,用于检查代理是否正在运行。下面只是一个片段,完整的解决方案请参见 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.
“让 ssh-agent 与 git run from windows command shell 一起工作”有一个类似的脚本,但我主要参考上面的 GitHub 文章,它更强大和最新。
回答by Jignesh Gohel
P.S: 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 中生成的 SSH 密钥与 Windows 上的 Ubuntu 上的 Bash 进行符号链接
1) Update your .bashrcby 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. Use following command for creating a new one: .ssh$ touch config
3) 创建 SSH 配置文件(如果不存在)。使用以下命令创建一个新的:.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. For e.g. in context of above code examples:
5) 使用命令将您的密钥添加到 SSH 代理$ ssh-add ~/.ssh/id_work_gmail
,然后您应该能够使用 ssh 连接到您的 github 帐户或远程主机。例如在上述代码示例的上下文中:
$ 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 i.e. 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:
笔记:
If you face
Bad owner or permissions on ~/.ssh/config
then update the permissions using the commandchmod 600 ~/.ssh/config
. Reference: https://serverfault.com/a/253314/98910For the above steps to work you will need OpenSSH v 7.2 and newer. If you have older one you can upgrade it using the steps mentioned at https://stackoverflow.com/a/41555393/936494
The same details can be found in the gist Windows 10 Linux Subsystem SSH-agent issues
如果您遇到,
Bad owner or permissions on ~/.ssh/config
则使用命令更新权限chmod 600 ~/.ssh/config
。参考:https: //serverfault.com/a/253314/98910要使上述步骤起作用,您将需要OpenSSH v 7.2 和更新版本。如果您有旧的,您可以使用https://stackoverflow.com/a/41555393/936494 中提到的步骤升级它
相同的细节可以在 gist Windows 10 Linux Subsystem SSH-agent issues 中找到
Thanks.
谢谢。
回答by Alasdair
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. I have a shortcut to pageant in my startup folder that loads my SSH key when I log in.
并且您需要在加载密钥的情况下运行 Pageant。我的启动文件夹中有一个选美的快捷方式,可以在我登录时加载我的 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. It makes life a hell of a lot easier (and secure).
Same applies with putty and WinSCP sessions when pageant has your key loaded. 它使生活变得更加轻松(和安全)。
回答by Erez A. Korn
As I don't like using putty in Windows as a workaround, I created a very simple utility ssh-agent-wrapper. It scans your .ssh folders and adds all your keys to the agent. You simply need to put it into Windows startup folder for it to work.
由于我不喜欢在 Windows 中使用腻子作为解决方法,因此我创建了一个非常简单的实用程序ssh-agent-wrapper。它会扫描您的 .ssh 文件夹并将您的所有密钥添加到代理中。您只需将其放入 Windows 启动文件夹中即可工作。
Assumptions:
假设:
- ssh-agent in path
- shh-add in path (both by choosing the "RED" option when installing git
- private keys are in %USERPROFILE%/.ssh folder
- private keys names start with id (e.g. id_rsa)
- 路径中的 ssh-agent
- shh-add in path(安装git时都选择“RED”选项
- 私钥在 %USERPROFILE%/.ssh 文件夹中
- 私钥名称以 id 开头(例如 id_rsa)
回答by Ambrown
I could not get this to work based off the best answer, probably because I'm such a PC noob and missing something obvious. 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
:
根据最佳答案,我无法让它发挥作用,可能是因为我是一个 PC 菜鸟,并且遗漏了一些明显的东西。但仅供参考,以防它可以帮助像我这样有挑战性的人,最终起作用的是通过此处的链接之一(在答案中引用)。这涉及简单地将以下内容粘贴到我的.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
. 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.
我可能配置了一些奇怪的东西,但是当我将它添加到我的.profile
或.bashrc
. 我遇到的另一个真正的挑战是我不是这台计算机的管理员,不能在没有 IT 批准的情况下更改环境变量,因此对于那些无法访问它的人来说,这是一个解决方案。
You know it's working if you're prompted for your ssh password when you open git bash. Hallelujah something finally worked.
如果在您打开 git bash 时提示您输入 ssh 密码,您就知道它正在工作。哈利路亚终于有事了。
回答by oklas
Simple two string solutionfrom this answer:
来自这个答案的简单的两个字符串解决方案:
For sh, bash, etc:
对于sh、bash等:
# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh
For csh, tcsh, etc:
对于csh、tcsh等:
# ~/.schrc
sh -c 'if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -c > ~/.ssh-agent.tcsh; fi'
eval `cat ~/.ssh-agent.tcsh`