· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Using Ssh Agent


1. 서설

subversion을 항상 local에서만 사용하다가 얼마전에 archive 전용 서버를 하나 마련하고 remote로 사용하고 있었지요. svn+ssh 을 이용해서 접속하고 있었는데, 이거 매번 ssh을 위해서 패스워드를 치기가 영 귀찮은 것이 아닙니다. 어떤 때는 coding하는 시간보다 패스워드 치는 시간이 더 긴 것 같다는 착각이 들 때도 -.-;;;; 그래서 ssh-agent를 사용하는 방법을 찾아봤는데 의외로 간단히 정리돼 있는 게 없어서, 아주 간단히 사용방법의 하나를 소개합니다. 이후는 편의상 반말로 :)

2. 미리 확인할 것들

  1. 우선 당연히 ssh client와 server가 제대로 동작하도록 설정하였는지 확인한다.
  2. 위의 사항이 제대로 되었으면 당연히 되겠지만, 혹시 모르니 ssh-keygen, ssh-copy-id, ssh-agent, ssh-add의 명령어가 사용 가능한지 확인한다.

3. 상황 설명

현재 자신이 작업하고 있는 머신이 machA라고 하고, svn+ssh server가 돌고 있는 머신을 machB라고 하자. 편의상 두 머신에 alice라는 username으로 계정을 가지고 있다고 가정한다.

4. 준비 작업

우선 ssh용으로 쓸 key pair를 만들어야 한다. machA에서 다음의 작업을 수행한다.

alice@macha:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lbird/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): <password>
Enter same passphrase again: <password>
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
33:d9:ac:19:bd:75:13:e5:4a:d9:3c:98:fa:4f:39:d6 lbird@debian
alice@macha:~$

key를 생성했으면 machB로 공개키를 전달해야 한다. machA에서 다음 작업을 수행한다.

alice@macha:~$ ssh-copy-id alice@machb
alice@machb's password: <alice@machb의 password>
Now try logging into the machine, with "ssh 'alice@machb'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

lbird@debian:~$

여기서 <alice@machb의 password>와 위의 <password>는 같을 필요는 없다는 것에 유의. 확인은 안해봤지만 당연히 그럴 것 같다. :)

5. ssh-agent 설정

다른 곳에 좋은 설명이 많겠지만 간단히 설명하면, ssh-agent라는 프로그램은 한번 실행하면 계속 메모리에 상주하면서 사용자의 private key를 안전하게 보호하고 있게 된다. 여기에 ssh-add라는 프로그램으로 사용자가 추가하고 싶은 private key를 집어 넣을 수 있다. ssh-agent는 실행될 때 소켓을 하나 만든다. 다른 프로그램들은 그 소켓 이름을 SSH_AUTH_SOCK라는 환경 변수에서 알아내서 ssh-agent와 통신해서 private key를 얻는다.

machA (자신의 주 작업장)에 처음 로긴하면 일단 다음의 명령을 실행한다.
alice@macha:~$ ssh-agent > ~/.ssh-agent.sh

순서는 좀 바뀌었지만, machA의 alice의 홈디렉토리에 있는 .bash_profile에 다음의 몇줄을 추가하도록 한다.
# ssh-agent variables
if [ -f ~/.ssh-agent.sh ]; then
  . ~/.ssh-agent.sh
fi

사실상 ssh-agent와 관련한 작업은 이제 끝났고 사용만 하면 된다.

추가> 왠지 모든 것이 자동으로 일어났으면 한다고 생각된다면(제가 그렇습니다. -.-;;) 다음의 방법을 사용할 수 있다. 다시 말하면, ssh-agent를 직접 command line에서 실행하는 것이 귀찮은 것이다.

.bash_profile에 위의 내용 대신 아래와 같이 삽입한다.
# ssh-agent variables
if [ -f ~/.ssh-agent.sh ]; then
    . ~/.ssh-agent.sh
fi
if [ ! -S $SSH_AUTH_SOCK ]; then
    ssh-agent -t 86400 > ~/.ssh-agent.sh
    echo "No agent! ssh-agent started."
    . ~/.ssh-agent.sh
fi

이렇게 하면 일단 .ssh-agent.sh이라는 파일이 이미 있는지 확인하고 있으면 읽어들인다. 그리고 거기에서 설정된 소켓($SSH_AUTH_SOCK)이 정말로 있는지 확인한다. 그렇지 않으면 ssh-agnet가 돌고 있지 않으니 새로 실행한다는 메시지를 띄우고 .ssh-agent.sh를 다시 만든다. 다른 것 또 한가지는 -t 86400 옵션을 추가해서 기본적으로 ssh-add를 통해서 key를 집어 넣을 때 제한시간을 하루로 설정한다는 것이다.

6. 사용

일단 shell로 로긴을 하면 이제 ssh-agent가 항상 돌고 있다고 확신할 수 있게 됐다. 이제는 ssh-add를 통해서 key를 로드해줄 필요가 있는데, 이미 만들어둔 key를 그냥 로드할 경우에는 command line에서

alice@macha:~$ ssh-add

라고만 하고 패스워드를 입력하면 된다.

7. 사족

그냥 끝내면 심심하니까.. ^^

ssh-agent가 없을 때는 접속할 때 이런 식이다.
alice@macha:~$ ssh alice@machb
Password: <alice@machb의 password>
Linux machb 2.6.8-2-686 #1 Tue Aug 16 13:22:48 UTC 2005 i686 GNU/Linux

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have mail.

Last login: Wed Dec 27 12:02:44 2006 from macha
alice@machb:~$

ssh-agent가 있으면

alice@macha:~$ ssh alice@machb
Linux machb 2.6.8-2-686 #1 Tue Aug 16 13:22:48 UTC 2005 i686 GNU/Linux

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have mail.

Last login: Wed Dec 27 12:02:44 2006 from macha
alice@machb:~$

password를 안 물어본다.

svn+ssh을 통해서 작업한다면 update나 commit을 할 때마다 한두번씩 물어보는 패스워드를 처음에 한 번만 입력하면 된다는 아주아주 편리한 점이 생긴다.

8. Document history

  • 처음 작성 -- Lbird 2006-12-28 23:53:47
  • .bash_profile 내용 수정 -- -- Lbird 2007-03-29 17:33:16

9. TODO



ID
Password
Join
Let him who takes the Plunge remember to return it by Tuesday.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2007-03-29 17:33:16
Processing time 0.0093 sec