SSH 자동 로그인

■ 비밀번호 없이 원격지 서버에 ssh를 통한 자동 로그인 방법을 알아보고자 한다. 먼저 root사용자로 로그인한 후 /etc/hosts파일을 아래와 같이 편집한다. 키 생성은 centos01서버에서 하며 centos01서버에서 각 원격지 서버(centos01, centos02, centos03, centos04, centos05)로 접속하는 것으로 가정한다. 일반 사용자의 계정 ID는 ‘nextman’이다.

bash# vi /etc/hosts
127.0.0.1   localhost
::1         localhost
192.168.192.128	centos01
192.168.192.130	centos02
192.168.192.133	centos03
192.168.192.132	centos04
192.168.192.131	centos05

 

■ ssh-keygen을 통해서 ssh개인키 및 공개키를 생성한다. 생성되면 일반사용자(nextman)계정의 .ssh디렉토리에 id_dsa(개인키), id_dsa.pub(공개키)가 생성된다. ls -al ~/.ssh/ 명령어를 통해서 확인 가능하다.

bash$ ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa
$ ls -al ~/.ssh/
total 20
drwx------ 2 nextman nextman 4096 Feb 28 21:09 .
drwx------. 5 nextman nextman 4096 Feb 28 16:26 ..
-rw------- 1 nextman nextman 672 Feb 28 21:06 id_dsa
-rw-r--r-- 1 nextman nextman 606 Feb 28 21:06 id_dsa.pub
-rw-r--r-- 1 nextman nextman 597 Feb 28 21:09 known_hosts

 

ssh-copy-id ‘원격지 사용자ID@원격지 호스트 명’ 명령어를 통해서 각 원격지 서버(centos01, centos02, centos03, centos04, centos05)서버의 nextman 일반사용자의 공개키를 centos01로 복사한다.

bash$ ssh-copy-id nextman@centos01
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
nextman@centos01's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'nextman@centos01'"
and check to make sure that only the key(s) you wanted were added.

bash$

 

■ 이제 각 원격지 서버에서 centos01서버에 모인 키를 다시 각 원격지 서버(centos01, centos02, centos03, centos04, centos05)로 scp명령어를 통해서 복사한다.

bash$ scp /home/nextman/.ssh/authorized_keys nextman@centos01:/home/nextman/.ssh/authorized_keys
authorized_keys                                                 100% 3030     3.0KB/s   00:00    
bash$ scp /home/nextman/.ssh/authorized_keys nextman@centos02:/home/nextman/.ssh/authorized_keys
authorized_keys                                                 100% 3030     3.0KB/s   00:00    
bash$ scp /home/nextman/.ssh/authorized_keys nextman@centos03:/home/nextman/.ssh/authorized_keys
authorized_keys                                                 100% 3030     3.0KB/s   00:00    
bash$ scp /home/nextman/.ssh/authorized_keys nextman@centos04:/home/nextman/.ssh/authorized_keys
authorized_keys                                                 100% 3030     3.0KB/s   00:00    
bash$ scp /home/nextman/.ssh/authorized_keys nextman@centos05:/home/nextman/.ssh/authorized_keys
authorized_keys                                                 100% 3030     3.0KB/s   00:00 

 

ssh ‘호스트명’으로 접속하면 비밀번호 입력 요청없이 원격지 서버에 접속이 된다.

bash$ ssh centos02
Last login: Sun Feb 28 20:31:55 2016 from 192.168.192.1
bash$

 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다