SSH 서버에 비밀번호 입력없이 자동 로그인하기 |
1. 개 요
신뢰성이 있는 원격 SSH 서버에 자주 접속하는 경우에 비밀번호를 입력하여 로그인하는 게 귀찮을 때가 있습니다. 오늘의 팁에서는 보안성을 가지면서 원격 SSH 서버에 비밀번호 입력없이 자동 로그인하는 방법에 대해서 알아봅니다.
SSH 구분
아이피 주소
계정(사용자명)
운영체제
클라이언트
192.168.35.240
dumca No1.Linux-2018-KDE-PLASMA5-SE 서버
192.168.35.126
xtreamer No.1Linux-2018-KDE-PLASMA5-PE
2. 자동 로그인 설정
STEP1. 클라이언트 시스템(192.168.35.240)에서 공개키를 다음과 같이 'ssh-keygen -t rsa' 명령을 실행하여 생성합니다.
[[dumca@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dumca/.ssh/id_rsa): 엔터키
Enter passphrase (empty for no passphrase): 엔터키
Enter same passphrase again: 엔터키
Your identification has been saved in /home/dumca/.ssh/id_rsa.
Your public key has been saved in /home/dumca/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:dLlmOQp8h4KLWFElV8ShdAtM25/rTdwGzsoWdfH+Jac dumca@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o+*+=. |
| . +.*.. . . |
| . o + o o |
| . o . + +. . .|
| . . + S X... . |
| o . . + =.* o. +|
|. . . . ..= o+o|
| o.+ .E .|
| .+ . |
+----[SHA256]-----+
[dumca@localhost ~]$
STEP2. 생성한 공개키를 SSH 서버(192.168.35.126)의 사용자홈/.ssh 경로에 authorized_keys 파일명으로 업로드합니다.
[dumca@localhost ~]$ cat .ssh/id_rsa.pub | ssh xtreamer@192.168.35.126 'cat >> .ssh/authorized_keys'
Password: <ssh 서버의 사용자 비밀번호를 입력합니다.>
공개키 파일을 업로드하는 다른 방법으로는 다음과 같이 ssh-copy-id를 이용해도 됩니다.
[dumca@localhost ~]$ ssh-copy-id -i .ssh/id_rsa.pub xtreamer@192.168.35.126
Password: <ssh 서버의 xtreamer 사용자 비밀번호를 입력합니다.>Now try logging into the machine, with "ssh 'xtreamer@192.168.35.126'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[dumca@localhost ~]$
STEP3. 업로드한 authorized_keys 파일에 대해서 퍼미션을 변경합니다.
STEP4. SSH 클라이언트에서 xtreamer 사용자 계정으로 로그인합니다.
[dumca@localhost ~]$ cat .ssh/id_rsa.pub | ssh xtreamer@192.168.35.126 "chmod 640 .ssh/authorized_keys"
[dumca@localhost ~]$ ssh xtreamer@192.168.35.126
Last login: Tue Apr 24 18:24:34 2018 from 192.168.35.240 <- 비번없이 자동로그인됨
[xtreamer@localhost ~]$