配置 Git over SSH 登录一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1595848/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:14:19  来源:igfitidea点击:

Configuring Git over SSH to login once

gitsshssh-keys

提问by reprogrammer

I have cloned my git repository over ssh. So, each time I communicate with the origin master by pushing or pulling, I have to reenter my password. How can I configure git so that I do not need to enter my password multiple times?

我已经通过 ssh 克隆了我的 git 存储库。因此,每次通过推或拉与源主通信时,我都必须重新输入密码。如何配置 git 以便我不需要多次输入密码?

采纳答案by DigitalRoss

Try ssh-add, you need ssh-agentto be running and holding your private key

试试看ssh-add,你需要ssh-agent运行并持有你的私钥

(Ok, responding to the updated question, you first run ssh-keygento generate a public and private key as Jefromiexplained. You put the public key on the server. You should use a passphrase, if you don't you have the equivalent of a plain-text password in your private key. But when you do, then you need as a practical matter ssh-agentas explained below.)

(好吧,回答更新的问题,你首先运行ssh-keygen以生成一个公钥和私钥,正如Jefromi解释的那样。你把公钥放在服务器上。你应该使用密码,如果你没有,你有一个普通的-text password in your private key。但是当你这样做时,你需要作为一个实际问题ssh-agent,如下所述。)

You want to be running ssh-agentin the background as you log in. Once you log in, the idea is to run ssh-addonce and only once, in order to give the agent your passphrase, to decode your key. The agent then just sits in memory with your key unlocked and loaded, ready to use every time you ssh somewhere.

您希望ssh-agent在登录时在后台运行。登录后,我们的想法是运行ssh-add一次且仅运行一次,以便为代理提供您的密码,以解码您的密钥。然后,代理就坐在内存中,您的密钥已解锁并已加载,随时可以在您每次 ssh 某处使用时使用。

All ssh-family commands1will then consult the agent and automatically be able to use your private key.

然后所有 ssh-family 命令1将咨询代理并自动能够使用您的私钥。

On OSX (err, macOS), GNOME and KDE systems, ssh-agentis usually launched automatically for you. I will go through the details in case, like me, you also have a Cygwin or other windows environment where this most certainly is not done for you.

在 OSX (err, macOS)、GNOME 和 KDE 系统上,ssh-agent通常会自动为您启动。我将详细介绍,以防万一,像我一样,您也有 Cygwin 或其他 Windows 环境,而这肯定不是为您完成的。

Start here: man ssh-agent.

从这里开始:man ssh-agent

There are various ways to automatically run the agent. As the man page explains, you can run it so that it is a parent of all your login session's other processes. That way, the environment variables it provides will automatically be in all your shells. When you (later) invoke ssh-addor sshboth will have access to the agent because they all have the environment variables with magic socket pathnames or whatever.

有多种方法可以自动运行代理。正如手册页所述,您可以运行它,使其成为所有登录会话的其他进程的父进程。这样,它提供的环境变量将自动存在于您的所有 shell 中。当您(稍后)调用ssh-addssh两者都可以访问代理时,因为它们都具有带有魔术套接字路径名或其他内容的环境变量。

Alternatively, you can run the agent as an ordinary child, save the environment settings in a file, and source that file in every shell when it starts.

或者,您可以将代理作为普通子进程运行,将环境设置保存在文件中,并在启动时在每个 shell 中获取该文件。

My OSX and Ubuntu systems automatically do the agent launch setup, so all I have to do is run ssh-addonce. Try running ssh-addand see if it works, if so, then you just need to do that once per reboot.

我的 OSX 和 Ubuntu 系统会自动进行代理启动设置,所以我所要做的就是运行ssh-add一次。尝试运行ssh-add并查看它是否有效,如果是,则每次重新启动只需执行一次。

My Cygwin system needed it done manually, so I did this in my .profileand I have .bashrcsource .profile:

我的 Cygwin 系统需要手动完成,所以我在我的.profile并且我.bashrc有源.profile

. .agent > /dev/null
ps -p $SSH_AGENT_PID | grep ssh-agent > /dev/null || {
        ssh-agent > .agent
        . .agent > /dev/null
}

The .agentfile is created automatically by the script; it contains the environment variables definitions and exports. The above tries to source the .agent file, and then tries to ps(1)the agent. If it doesn't work it starts an agent and creates a new agent file. You can also just run ssh-addand if it fails start an agent.

.agent文件由脚本自动创建;它包含环境变量定义和导出。以上尝试获取 .agent 文件,然后尝试ps(1)代理。如果它不起作用,它会启动一个代理并创建一个新的代理文件。您也可以直接运行ssh-add,如果失败则启动代理。



1. And even local and remote sudowith the right pam extension.1. 甚至本地和远程sudo使用正确的 pam 扩展。

回答by Muein Muzamil

Had a similar problem with the GitHub because I was using HTTPS protocol. To check what protocol you're using just run

GitHub 也有类似的问题,因为我使用的是 HTTPS 协议。要检查您使用的是什么协议,请运行

git config -l

and look at the line starting with remote.origin.url. To switch your protocol

并查看以 开头的行remote.origin.url。切换协议

git config remote.origin.url [email protected]:your_username/your_project.git

回答by Cascabel

This is about configuring ssh, not git. If you haven't already, you should use ssh-keygen(with a blank passphrase) to create a key pair. Then, you copy the public key to the remote destination with ssh-copy-id. Unless you have need of multiple keys (e.g. a more secure one with a passphrase for other purposes) or you have some really weird multiple-identity stuff going on, it's this simple:

这是关于配置 ssh,而不是 git。如果您还没有,您应该使用ssh-keygen(使用空白密码)来创建密钥对。然后,您将公钥复制到远程目标ssh-copy-id。除非您需要多个密钥(例如,一个更安全的带有密码用于其他目的的密钥)或者您有一些非常奇怪的多重身份问题,否则就是这么简单:

ssh-keygen   # enter a few times to accept defaults
ssh-copy-id -i ~/.ssh/id_rsa user@host

Edit: You should really just read DigitalRoss's answer, but: if you use keys with passphrases, you'll need to use ssh-add <key-file>to add them to ssh-agent(and obviously start up an ssh-agentif your distribution doesn't already have one running for you).

编辑:您真的应该阅读 DigitalRoss 的答案,但是:如果您使用带有密码短语的密钥,您将需要使用ssh-add <key-file>将它们添加到ssh-agentssh-agent如果您的发行版还没有为您运行,显然启动一个)。

回答by Zorayr

Make sure that when you cloned the repository, you did so with the SSH URL and not the HTTPS; in the clone URL box of the repo, choose the SSH protocol before copying the URL. See image below:

确保克隆存储库时使用的是 SSH URL 而不是 HTTPS;在 repo 的克隆 URL 框中,在复制 URL 之前选择 SSH 协议。见下图:

enter image description here

在此处输入图片说明

回答by Nishant Thapliyal

If you have cloned using HTTPS(recommended) then:-

如果您已使用HTTPS(推荐)进行克隆,则:-

git config --global credential.helper cache

and then

进而

git config --global credential.helper 'cache --timeout=2592000'
  • timeout=2592000 (30 Days in seconds) to enable caching for 30 days (or whatever suites you).

  • Now run a simple git command that requires your username and password.

  • Enter your credentials once and now caching is enabled for 30 Days.

  • Try again with any git command and now you don't need any credentials.

  • For more info :- Caching your GitHub password in Git

  • timeout=2592000(30 天以秒为单位)启用缓存 30 天(或任何适合您的套件)。

  • 现在运行一个简单的 git 命令,它需要您的用户名和密码。

  • 输入您的凭据一次,现在缓存启用 30 天。

  • 使用任何 git 命令再试一次,现在您不需要任何凭据。

  • 有关更多信息:-在 Git 中缓存您的 GitHub 密码

Note: You need Git 1.7.10 or newer to use the credential helper. On system restart, we might have to enter the password again.

注意您需要 Git 1.7.10 或更新版本才能使用凭证助手。在系统重新启动时,我们可能需要再次输入密码。

回答by uchamp

Extending Muein's thoughts for those who prefer to edit files directly over running commands in git-bash or terminal.

为那些喜欢直接编辑文件而不是在 git-bash 或终端中运行命令的人扩展 Muein 的想法。

Go to the .git directory of your project (project root on your local machine) and open the 'config' file. Then look for [remote "origin"] and set the url config as follows:

转到项目的 .git 目录(本地机器上的项目根目录)并打开“config”文件。然后查找 [remote "origin"] 并设置 url 配置如下:

[remote "origin"]
    #the address part will be different depending upon the service you're using github, bitbucket, unfuddle etc.
    url = [email protected]:<username>/<projectname>.git

回答by Gaston

I think there are two different things here. The first one is that normal SSH authentication requires the user to put the account's password (where the account password will be authenticated against different methods, depending on the sshd configuration).

我认为这里有两件不同的事情。第一个是普通的 SSH 身份验证需要用户输入帐户密码(其中帐户密码将根据不同的方法进行身份验证,具体取决于 sshd 配置)。

You can avoid putting that password using certificates. With certificates you still have to put a password, but this time is the password of your private key (that's independent of the account's password).

您可以避免使用证书输入该密码。对于证书,您仍然需要输入密码,但这次是您的私钥的密码(与帐户密码无关)。

To do this you can follow the instructions pointed out by steveth45:

为此,您可以按照 steveth45 指出的说明进行操作:

With Public Key Authentication.

使用公钥认证

If you want to avoid putting the certificate's password every time then you can use ssh-agent, as pointed out by DigitalRoss

如果你想避免每次都输入证书的密码,那么你可以使用 ssh-agent,正如 DigitalRoss 所指出的

The exact way you do this depends on Unix vs Windows, but essentially you need to run ssh-agent in the background when you log in, and then the first time you log in, run ssh-add to give the agent your passphrase. All ssh-family commands will then consult the agent and automatically pick up your passphrase.

Start here: man ssh-agent.

执行此操作的确切方式取决于 Unix 与 Windows,但本质上您需要在登录时在后台运行 ssh-agent,然后在第一次登录时运行 ssh-add 以向代理提供您的密码。然后所有 ssh-family 命令将咨询代理并自动获取您的密码。

从这里开始:man ssh-agent。

The only problem of ssh-agent is that, on *nix at least, you have to put the certificates password on every new shell. And then the certificate is "loaded" and you can use it to authenticate against an ssh server without putting any kind of password. But this is on that particular shell.

ssh-agent 的唯一问题是,至少在 *nix 上,您必须将证书密码放在每个新 shell 上。然后证书被“加载”,您可以使用它来对 ssh 服务器进行身份验证,而无需输入任何类型的密码。但这是在那个特定的外壳上。

With keychainyou can do the same thing as ssh-agent but "system-wide". Once you turn on your computer, you open a shell and put the password of the certificate. And then, every other shell will use that "loaded" certificate and your password will never be asked again until you restart your PC.

使用钥匙串,您可以做与 ssh-agent 相同的事情,但“系统范围”。一旦你打开你的电脑,你就会打开一个 shell 并输入证书的密码。然后,所有其他 shell 都将使用该“已加载”证书,并且在您重新启动 PC 之前永远不会再次询问您的密码。

Gnome has a similar application, called Gnome Keyringthat asks for your certificate's password the first time you use it and then it stores it securely so you won't be asked again.

Gnome 有一个类似的应用程序,称为Gnome Keyring,它会在您第一次使用证书时询问您的证书密码,然后安全地存储它,这样您就不会再被询问了。

回答by Miles

If you're using github, they have a very nice tutorial that explains it more clearly (at least to me).

如果您使用 github,他们有一个非常好的教程,可以更清楚地解释它(至少对我而言)。

http://help.github.com/set-up-git-redirect/

http://help.github.com/set-up-git-redirect/

回答by Srinivas Kattimani

ssh-keygen -t rsa

When asked for a passphrase ,leave it blank i.e, just press enter. as simple as that!!

当要求输入密码时,请将其留空,即,只需按 Enter。就如此容易!!

回答by toonsend

Try this from the box you are pushing from

从你推的盒子里试试这个

    ssh [email protected]

You should then get a welcome response from github and will be fine to then push.

然后你应该从 github 得到一个受欢迎的回复,然后推送就可以了。