2014년 11월 6일 목요일

ssh 키연결

ssh 서버 설정하기(리무트)
[서버에서]
/etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key


RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
//RSAAuthentication, PubkeyAuthentication을 허용하며  각 유저의 .ssh/authorized_keys 파일에
공개키를 저장하겠음

PasswordAuthentication yes
위에 실패할경우 패스워드 접속을 하겠음

* 옵션별 상세 설명
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
설명
Specifies a file containing a private host key used by SSH.  The
    default is /etc/ssh/ssh_host_key for protocol version 1, and
    /etc/ssh/ssh_host_dsa_key, /etc/ssh/ssh_host_ecdsa_key,
    /etc/ssh/ssh_host_ed25519_key and /etc/ssh/ssh_host_rsa_key for
    protocol version 2.

    사용이유
   To prevent man-in-the-middle attacks, each SSH server has a unique identifying code, called a host key. These keys prevent a server
   from forging another server’s key. If you connect to a server for the first time or if the server presets a different key then
   previously, WinSCP will prompt you to verify the key.

RSAAuthentication yes
RSA 인증을 설정. Protocol 1 에 적용

PubkeyAuthentication yes
공개키 인증 설정. Protocol 2에 적용

AuthorizedKeysFile
공개키 파일명 파일명을 지정
디폴트는 유저의 홈 디렉토리로부터의 상대 패스에서 AuthorizedKeysFile 에 설정되 있는 파일을 공개키 파일로 설

서비스 재시작
service sshd restart

로컬에서 키생성
[로컬에서]
ssh-keygen -t dsa
Enter file in which to save the key (/Users/lee/.ssh/id_dsa):  enter
이름을 지정하면 키 복사시 ssh-copy-id -i path/to/key_name.pub user_name@host_name
연결시 ssh -i path/to/key_name user@host
로 접속 해야 한다.



키 파일 서버에 올리기
[로컬에서]
scp -P 포트 로컬파일 리무트계정@리무트주소:리무트파일
scp -P 포트 .ssh/id_dsa.pub 리무트계정@리무트주소:.ssh/id_dsa.pub

서버에서 올라간 공개키를  authorized_keys 파일에 첨부하기
[서버에서]
없으면 만든다
touch authorized_keys
chmod 644 authorized_keys

존재하면 존재하는 파일에 새로 만든 공개키를 추가한다.
cat  test_dsa.pub >> authorized_keys

어떻게 동작하는지 알고 싶으면 아래 링크 확인
http://www.parkjonghyuk.net/lecture/modernCrypto/lecturenote/chap05.pdf

안될경우
  ssh -v
http://stackoverflow.com/questions/6377009/adding-public-key-to-ssh-authorized-keys-does-not-log-me-in-automatically