配置 Git 通过 SSH 登录一次

发布于 2024-08-09 04:30:58 字数 83 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

追星践月 2024-08-16 04:30:58

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

git config -l

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

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

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

git config -l

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

git config remote.origin.url [email protected]:your_username/your_project.git
爺獨霸怡葒院 2024-08-16 04:30:58

尝试ssh-add,您需要ssh-agent运行并保存您的私钥

(好吧,回答更新的问题,您首先运行ssh-keygen 生成公钥和私钥 Jefromi 解释了。您将公钥放在服务器上。您应该使用密码,如果没有,则您的私钥中有相当于纯文本密码的密码。但是当如果您这样做,那么您实际上需要 ssh-agent,如下所述。)

您希望在登录时在后台运行 ssh-agent。一旦您登录后,想法是运行 ssh-add 一次且仅一次,以便向代理提供您的密码,以解码您的密钥。然后,代理将在您的密钥解锁并加载的情况下位于内存中,以便您每次 ssh 到某处时都可以使用。

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

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

从这里开始:man ssh-agent

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

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

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

我的 Cygwin 系统需要手动完成,因此我在 .profile 中执行此操作,并且我有 .bashrc.profile

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

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


1. And even local and remote sudo with the right pam extension.

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

(Ok, responding to the updated question, you first run ssh-keygen to generate a public and private key as Jefromi explained. 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-agent as explained below.)

You want to be running ssh-agent in the background as you log in. Once you log in, the idea is to run ssh-add once 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.

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

On OSX (err, macOS), GNOME and KDE systems, ssh-agent is 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.

Start here: 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-add or ssh both will have access to the agent because they all have the environment variables with magic socket pathnames or whatever.

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.

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

My Cygwin system needed it done manually, so I did this in my .profile and I have .bashrc source .profile:

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

The .agent file 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-add and if it fails start an agent.


1. And even local and remote sudo with the right pam extension.

对你的占有欲 2024-08-16 04:30:58

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

git config --global credential.helper cache

然后

git config --global credential.helper 'cache --timeout=2592000'
  • timeout=2592000(30 天,以秒为单位)启用缓存 30 天(或任何适合您的时间)。

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

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

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

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

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

更新 #1:

如果您收到此错误 git: 'credential-cache' 不是 git 命令。请参阅“get --help”

,然后将 git config --global credential.helper 'cache --timeout=2592000' 替换

git config --global credential.helper 'store --file ~/.my-credentials'

更新#2:

如果您不断收到用户名和密码的提示并遇到此问题:

登录失败,使用ctrl+c取消基本凭据提示。

重新安装 最新版本的 git 对我有用。

更新#3:

作为限电的一部分,密码身份验证被暂时禁用。请改用个人访问令牌。

  • 生成 Github accessToken
  • 取消设置现有凭证缓存 git config --global --unset credential.helper
  • git config --global credential.helper 'store --file ~/ .my-credentials'
  • 任何提示输入用户名 的 git 命令密码输入令牌而不是密码

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

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 suits 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

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.

Update #1:

If you are receiving this error git: 'credential-cache' is not a git command. See 'get --help'

then replace git config --global credential.helper 'cache --timeout=2592000'

with git config --global credential.helper 'store --file ~/.my-credentials'

Update #2:

If you keep getting the prompt of username and password and getting this issue:

Logon failed, use ctrl+c to cancel basic credential prompt.

Reinstalling the latest version of git worked for me.

Update #3:

Password authentication is temporarily disabled as part of a brownout. Please use a personal access token instead.

  • Generate Github accessToken
  • Unset existing credential cache git config --global --unset credential.helper
  • git config --global credential.helper 'store --file ~/.my-credentials'
  • Any git command that'll prompt for username & password and enter token instead of password.
橘香 2024-08-16 04:30:58

这是关于配置 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

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

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-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-agent if your distribution doesn't already have one running for you).

忆离笙 2024-08-16 04:30:58

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

在此处输入图像描述

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:

enter image description here

鯉魚旗 2024-08-16 04:30:58

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

转到项目的 .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

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

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:

[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
尐籹人 2024-08-16 04:30:58

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

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

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

使用公钥身份验证

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

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

从这里开始:man ssh-agent。

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

使用 keychain 你可以做与 ssh-agent 相同的事情,但是“system -宽的”。打开计算机后,打开外壳并输入证书的密码。然后,所有其他 shell 都会使用该“已加载”证书,并且在您重新启动 PC 之前,永远不会再次询问您的密码。

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

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).

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:

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

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.

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.

With keychain you 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.

Gnome has a similar application, called Gnome Keyring that 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.

握住我的手 2024-08-16 04:30:58

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

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

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

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

扮仙女 2024-08-16 04:30:58
ssh-keygen -t rsa

当要求输入密码时,请将其留空,即按 Enter 键即可。
就这么简单!!

ssh-keygen -t rsa

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

記柔刀 2024-08-16 04:30:58

从您要推送的盒子中尝试一下,

    ssh [email protected]

然后您应该会收到来自 github 的欢迎响应,然后就可以推送了。

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.

雪若未夕 2024-08-16 04:30:58

我必须从不允许使用 ssh 密钥登录而只能使用用户/密码登录的服务器克隆 git 存储库。我发现无法将 Git 插件配置为使用简单的用户/密码组合,因此我添加了以下 shell 命令作为 Linux 构建机器上的预构建步骤,这取决于工具 Expect (apt-get install Expect)

:这不是解决此问题的好方法,因为您的密码在 JENKINS 作业的配置和日志中显示为清晰的文本!仅当无法配置 RSA 密钥身份验证或其他配置可能性时才使用它!

rm -rf $WORKSPACE &&
expect -c 'set timeout -1; spawn git clone USER@MYHOST:/MYPATH/MYREPO.git $WORKSPACE; expect "password:" {send "MYPASSWORD\r"}; expect eof'

I had to clone a git repo from a server that did not allow login vie ssh key but only with a user/password. I found no way to configure the Git Plugin to use a simple user/password combination so i added the the following shell command as pre-build step on a linux build machine which depends on the tool expect (apt-get install expect):

THIS IS NOT A GOOD WAY OF SOLVING THIS PROBLEM AS YOUR PASSWORD IS SHOWN AS CLEAR TEXT IN THE CONFIGURATION AND LOGS OF THE JENKINS JOB! ONLY USE IT IF THERE IS NO WAY TO CONFIGURE RSA-KEY AUTHENTIFICATION OR OTHER CONFIGURATION POSSIBILITES!

rm -rf $WORKSPACE &&
expect -c 'set timeout -1; spawn git clone USER@MYHOST:/MYPATH/MYREPO.git $WORKSPACE; expect "password:" {send "MYPASSWORD\r"}; expect eof'
乖乖哒 2024-08-16 04:30:58

我一直试图避免输入密码,因为我在 Windows 上使用 ssh。
我所做的是修改我的 .profile 文件,以便我在特定会话中输入我的密码。这就是一段代码:

    SSH_ENV="$HOME/.ssh/environment"

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

    # test for identities
    function test_identities {
        # test whether standard identities have been added to the agent already
        ssh-add -l | grep "The agent has no identities" > /dev/null
        if [ $? -eq 0 ]; then
            ssh-add
            # $SSH_AUTH_SOCK broken so we start a new proper agent
            if [ $? -eq 2 ];then
                start_agent
            fi
        fi
    }

    # check for running ssh-agent with proper $SSH_AGENT_PID
    if [ -n "$SSH_AGENT_PID" ]; then
        ps -fU$USER | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
      test_identities
        fi
    # if $SSH_AGENT_PID is not properly set, we might be able to load one from
    # $SSH_ENV
    else
        if [ -f "$SSH_ENV" ]; then
      . "$SSH_ENV" > /dev/null
        fi
        ps -fU$USER | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
            test_identities
        else
            start_agent
        fi
    fi

因此,我在会话中输入一次密码。

I have being trying to avoid typing the passphrase all the time also because i am using ssh on windows.
What i did was to modify my .profile file, so that i enter my passphrase one in a particular session. So this is the piece of code:

    SSH_ENV="$HOME/.ssh/environment"

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

    # test for identities
    function test_identities {
        # test whether standard identities have been added to the agent already
        ssh-add -l | grep "The agent has no identities" > /dev/null
        if [ $? -eq 0 ]; then
            ssh-add
            # $SSH_AUTH_SOCK broken so we start a new proper agent
            if [ $? -eq 2 ];then
                start_agent
            fi
        fi
    }

    # check for running ssh-agent with proper $SSH_AGENT_PID
    if [ -n "$SSH_AGENT_PID" ]; then
        ps -fU$USER | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
      test_identities
        fi
    # if $SSH_AGENT_PID is not properly set, we might be able to load one from
    # $SSH_ENV
    else
        if [ -f "$SSH_ENV" ]; then
      . "$SSH_ENV" > /dev/null
        fi
        ps -fU$USER | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
            test_identities
        else
            start_agent
        fi
    fi

so with this i type my passphrase once in a session..

只为守护你 2024-08-16 04:30:58

在 .ssh/config 文件顶部添加一行 AddKeysToAgent yes。当然 ssh-agent 必须事先运行。如果它没有运行(通过 prep ssh-agent 检查),则只需运行它 eval $(ssh-agent)

现在,密钥已在系统范围内加载到内存中,您可以不必再次输入密码。

解决方案的来源是 https://askubuntu.com/questions/362280/enter- ssh-passphrase-once/853578#853578

Add a single line AddKeysToAgent yes on the top of the .ssh/config file. Ofcourse ssh-agent must be running beforehand. If its not running ( check by prep ssh-agent ) , then simply run it eval $(ssh-agent)

Now, the key is loaded systemwide into the memory and you dont have to type in the passphrase again.

The source of the solution is https://askubuntu.com/questions/362280/enter-ssh-passphrase-once/853578#853578

甜中书 2024-08-16 04:30:58

我尝试了所有这些建议以及更多建议,这样我就可以从我的 AWS 实例进行 git 克隆。什么都没起作用。我终于在绝望中作弊:我在本地计算机上复制了 id_rsa.pub 的内容,并将其附加到我的 AWS 实例上的 ~/.ssh/known_hosts 中。

I tried all of these suggestions and more, just so I could git clone from my AWS instance. Nothing worked. I finally cheated out of desperation: I copied the contents of id_rsa.pub on my local machine and appended it to ~/.ssh/known_hosts on my AWS instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文