当我克隆一个存储库时,我一直有点“忍受”Github总是问我的用户名和密码。我想绕过这一步,因为这是我的工作流中的一个烦恼。

我尝试使用本指南设置SSH密钥(我成功地做到了)。https://help.github.com/articles/generating-ssh-keys,我成功了。

我的问题是,当克隆一个存储库(使用SSH)时,我仍然被要求输入我的github密码和密码短语。我的理解是,在我设置了这个SSH密钥之后,我就不再需要这样做了。

我不知道该问什么,所以我就说说我的目标吧。

我希望能够克隆存储库,而不必一直把我的Github信息。

我的SSH密钥丢失了什么?如果有人能提供一些指导或资源,我会很感激,因为当涉及到GitHub中的SSH身份验证时,我总是感到有点失落。

据我所知,这是一个测试事情是否正常工作的命令,下面是来自我的控制台的输出:

~ $ ssh -T git@github.com
Saving password to keychain failed
Enter passphrase for key '/Users/MYNAME/.ssh/id_rsa':
Hi MYNAME! You've successfully authenticated, but GitHub does not provide shell access.

当我输入密码时,应该先失败吗?然后,当我输入密码时,它就通过了。


当前回答

SSH密钥-仍然要求密码和密码短语

如果在Windows上使用PuTTY作为SSH密钥生成器,这个快速而简单的解决方案是我使用普通Windows命令行的唯一工作解决方案:

Your PuTTY installation should come with several executable, among others, pageant.exe and plink.exe When generating a SSH key with PuttyGen, the key is stored with the .ppk extension Run "full\path\to\your\pageant.exe" "full\path\to\your\key.ppk" (must be quoted). This will execute the pageant service and register your key (after entering the password). Set environment variable GIT_SSH=full\path\to\plink.exe (must not be quoted). This will redirect git ssh-communication-related commands to plink that will use the pageantservice for authentication without asking for the password again.

完成了!

注1:本文档警告使用GIT_SHH环境变量设置时的一些特殊情况。我可以用任意数量的附加参数对命令进行推、拉、取,一切都很好地为我工作(不需要像其中建议的那样编写额外的脚本)。

注2:PuTTY安装路径一般在Path中,可以省略。不管怎样,我更喜欢指定完整的路径。

自动化:

在使用git之前,可以从命令行运行以下批处理文件。它说明了设置的用法:

git-init.bat
   @ECHO OFF
   :: Use start since the call is blocking
   START "%ProgramFiles%\PuTTY\pageant.exe" "%HOMEDRIVE%%HOMEPATH%\.ssh\id_ed00000.ppk"
   SET GIT_SSH=%ProgramFiles%\PuTTY\plink.exe

无论如何,我已经在SystemPropertiesAdvanced.exe >中设置了GIT_SSH变量,并将pageant.exe添加为运行注册表键(*)。

(*)添加“运行注册表项>”步骤

run regedit.exe Navigate to HKEY_CURRENT_USER > Software > Microsoft > Windows > CurrentVersion > Run Do (menu) Edit > New > String Value Enter an arbitrary (but unique) name Do (menu) Edit > Modify... (or double-click) Enter the quotes-enclosed path to pageant.exe and public key, e.g., "C:\Program Files\PuTTY\pageant.exe" "C:\Users\username\.ssh\id_ed00000.ppk" (notice that %ProgramFiles% etc. variables do not work in here unless choosing Expandable string value in place of the String value in step 3.).

其他回答

您可以删除密钥的密码短语

$ ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]

或者你可以逃跑

$ ssh-keygen -p

您将得到输入keyfile的提示。默认为~/。Ssh /id_rsa so按enter键

您将被提示输入当前的密码短语,请输入。

然后会出现输入新口令的提示,按enter键

对于Mac OSX Sierra,我发现github上建议的Open Radar修复了我的问题。似乎Sierra改变了默认行为(我在升级后开始遇到这个问题)。

我发现这个特别有用: https://github.com/lionheart/openradar-mirror/issues/15361#issuecomment-249059061

ssh-add -A 

这导致我的身份在运行后被添加到代理中

ssh-add -K {/path/to/key}

总之,在OSX.12中:

ssh-add -K {/path/to/key}
ssh-add -A 

会导致:

Identity added: {/path/to/file} ({/path/to/file})

编辑: 我注意到,下一次我完全重启(即代理停止并重新启动)时,它不再工作。更完整的解决方案是上面提到的@ChrisJF:创建~/。ssh / config文件。以下是我的输出:

$ cat ~/.ssh/config
Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

您可以根据需要添加任意数量的IdentityFile条目,但这是默认设置。这也是上面的开放式雷达链接上的“趋势”答案,ATM。

我想为那些可能还需要进入的人补充一个答案 密码,因为他们已经设置 身份仅为yes。这可能是由多个密钥和身份文件引起的,这些密钥是git或服务器的密钥。

在我生成密钥并将其复制到服务器后:

ssh-keygen
ssh-copy-id -i ~/.ssh/12gpu_server.pub lerner@192.168.20.160

我发现它不管用。

然后我去检查~/。ssh/config文件,我看到这个在 底:

Host *
IdentitiesOnly yes

然后我在上面加上这个:

Host 12gpu
HostName 192.168.20.160
User lerner
IdentityFile ~/.ssh/12gpu_server

我可以通过输入ssh 12gpu登录。

然后,您可以使用自己喜欢的名称添加多个ssh密钥 只需要在配置中添加上述四行设置即可 文件。

Host是连接到服务器时要输入的名称 晚;主机名是服务器的ip或域名,如github.com;“User”为登录的用户名 在服务器端,比如用户名或git for github或gitlab;而IdentityFile是存储 生成的密钥。

如果您使用的Windows和GIT没有第三方工具,并且您的密钥没有密码/密码短语保护,请使用以下方法:

Environment Variable HOME must be set to your user profile (e.g. C:\Users\Laptop) Go to C:\Users\Laptop\.ssh\ folder and edit "config" file (or create the file!) Example: C:\Users\Laptop.ssh\config (note: there is no . at the end!) Add your git-server host to the "config" file like so: #Example host entry Host myhostname.com HostName myhostname.com User git IdentityFile c:/users/laptop/.ssh/id_rsa.pub PasswordAuthentication no Port 422 Save the file and clone the repository like this: git clone ssh://myhostname.com/git-server/repos/picalc.git

您可以为“config”文件主机条目使用额外的配置参数。这些可以在你的本地git安装文件夹中找到,例如。“C: \ Program Files \ Git \ etc \ ssh \ ssh_config”。摘录:

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h

我已经设置了密码,但由于某种原因,它不再识别它了。因此,我只是再次使用ssh-add -K将身份文件添加到我的钥匙链中,它不再要求我输入密码。