IT박스

Windows에서 Git Bash를 시작할 때 SSH 에이전트 실행

itboxs 2020. 7. 1. 08:17
반응형

Windows에서 Git Bash를 시작할 때 SSH 에이전트 실행


git bash를 사용하고 있습니다. 나는 사용해야한다

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

새로운 git bash를 시작할 때마다.

ssh 에이전트를 영구적으로 설정하는 방법이 있습니까? 아니면 창에 ssh 키를 관리하는 좋은 방법이 있습니까?

나는 새로운 녀석입니다, 자세한 자습서를 줘주세요, 감사합니다!


자식 강타 세션에서, 당신은에 스크립트를 추가 할 수 있습니다 ~/.profile또는 ~/.bashrc( ~일반적으로 설정되어%USERPROFILE% 출시 말했다 세션의 순서를 자동을) ssh-agent. 파일이 존재하지 않으면 작성하십시오.

이것이 바로 " SSH 키 비밀번호 문구 작업 "에서 GitHub가 설명하는 것입니다 .

기사의 " 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

기타 자료 :

" ssh-agent가 git run with windows command shell에서 작동하도록하기 "는 비슷한 스크립트를 가지고 있지만, 더 강력하고 최신 인 위의 GitHub 기사를 주로 참조 할 것입니다.


추신 :이 지침은 Windows 10 Linux Subsystem에서 열린 Bash 셸과 관련이 있으며 Windows에서 Ubuntu의 Bash를 사용하여 Windows에서 생성 된 sym-linking SSH 키에 대해서는 언급하지 않습니다

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) 그런 다음 실행 $ source ~/.bashrc하여 구성을 다시로드하십시오.

위의 단계는 https://github.com/abergs/ubuntuonwindows#2-start-an-bash-ssh-agent-on-launch 에서 가져 왔습니다.

3) SSH 구성 파일이 없으면 작성하십시오. 새 명령을 작성하려면 다음 명령을 사용하십시오..ssh$ touch 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) 명령을 사용하여 SSH 에이전트에 키를 추가 $ ssh-add ~/.ssh/id_work_gmail하면 ssh를 사용하여 github 계정 또는 원격 호스트에 연결할 수 있어야합니다. 예를 들어 위의 코드 예제와 관련하여 :

$ ssh github.com-<YOUR_GITHUB_USERNAME>

또는

$ ssh <USER>@csexperimental.abc.com

SSH 에이전트에이 키 추가는 한 번만 수행해야합니다.

6) 이제 Windows Linux Subsystem에서 Bash 세션에서 로그 아웃하십시오. 즉, 모든 Bash 콘솔을 다시 종료하고 새 콘솔을 다시 시작한 다음 SSH 구성 파일에 구성된대로 Github 호스트 또는 다른 호스트에 SSH를 시도하십시오. 단계.

노트 :

Thanks.


I found the smoothest way to achieve this was using Pageant as the SSH agent and 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.

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.

When you install git-scm you can then specify it to use tortoise/plink rather than OpenSSH.

The net effect is you can open git-bash whenever you like and push/pull without being challenged for passphrases.

Same applies with putty and WinSCP sessions when pageant has your key loaded. It makes life a hell of a lot easier (and secure).


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.

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)

Create a new .bashrc file in your ~ directory.

There you can put your commands that you want executed everytime you start the bash


Simple two string solution from this answer:

For sh, bash, etc:

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

For csh, tcsh, etc:

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

참고URL : https://stackoverflow.com/questions/18404272/running-ssh-agent-when-starting-git-bash-on-windows

반응형