본문 바로가기

공개키 인증으로 SSH Password 없이 접속하기

SSH Key 만들기

SSH Key를 통해서 서버에 접속시 Unix 계열에서는 ssh-keygen 명령어를 이용하면 된다.

Window OS에서는 SSH Client 프로그램이 자체적으로 생성 제공하는 프로그램을 이용하면 된다. 여기서는 Unix 계열의 Key를 생성하는 방법을 기술 하도록 하겠다.

ssh-keygen 사용하기

아래의 명령어를 입력하면 $HOME/.ssh Folder 밑에 Private KeyPublic Key 가 생성이 된다.

Key 생성시 3번의 사용자 값 입력을 받는다.

  1. Enter file in which to save the key (/Users/secuof/.ssh/id_rsa):

    • Key의 저장 위치를 물어 본다. 이때 Enter를 입력했다면 /Users/secuof/.ssh/ 경로 밑에 id_rsa, id_rsa.pub 파일이 생성된다.
  2. Enter passphrase (empty for no passphrase):

    • Key에 사용될 Password를 입력 받는다 여기에서는 Password 없이 접속하기 위해서는 Enter를 입력한다.
  3. Enter same passphrase again:

    • Password 확인 2번째 입력 값과 동일하게 입력한다.
# ssh key를 rsa 암호화 방식으로 Key를 생성
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/secuof/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/secuof/.ssh/idid_rsa.
Your public key has been saved in /Users/secuof/.ssh/idid_rsa.pub.
The key fingerprint is:
SHA256:1N8Bb7UW+OxDNBwsJF9r54vESNUHI2t8yeFr7Zme2JQ secuof@Davids-iMac.local
The key's randomart image is:
+---[RSA 3072]----+
|           .+oO=o|
|         . .+X.@=|
|        . ..+.&=+|
|       .  .oo+oO.|
|        S  ..o* o|
|            ...+=|
|             . Eo|
|              = .|
|             . + |
+----[SHA256]-----+

SSH로 접속할 서버에 Public Key 복사하기

서버에 Public Key를 복사하는 방법은 직접 입력하는 방법과 ssh-copy-id 명령어를 입력하는 방법이 있다.

ssh-copy-id 로 public key 복사하기

사용방법

ssh-copy-id [ID]@[SSH Server Address]

ssh-copy-id 명령어를 이용해서 서버에 사용할 ID와 Password를 입력하게 되면 접속하는 서버의 Password를 한번 입력받은 후 Public Key가 접속하고자 하는 서버에 Key가 복사된다.

이후 ssh 명령어로 접속시 Password 입력받지 않고 접속이 가능하다.

사용 예시

### ssh-copy-id를 이용하여 Public Key 복사
$ ssh-copy-id secuof@ssh-connect-server.com 
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/secuof/.ssh/id_rsa.pub"
/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
secuof@ssh-connect-server.com 's password:

Number of key(s) added:        1

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

### Password 입력 없이 SSH 접속
$ ssh secuof@ssh-connect-server.com
Last login: Thu Jun 25 11:54:58 2020 from 123.123.123.123
secuof@ssh-connect-server.com:/home/secuof$

수동으로 Public Key 복사

먼저 기존에 만들어 놓은 Public키를 복사해 놓는다.

$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC9UoEhuJShwl0DukIeziOYLOJe/10grhbff4UVCojH1zjtY8tPlyw0a9/ZTh7a20k+W+iaXfWIx5w5sGo40ecwXDp4cVj2xJhm9dtnRBJuaUx03O/EUA4mNSBMLEH0k+xAiIjNxb1ql2X0SBFIM6VDTjfKkATJxAfIqJ1saompeWldSrWDjaXq0bg7VBKEXK7Jvkqg3U5pp4o5jFEukwBTqY8xxtzbgkqJJ7Z8RAhTyzePcEy/r9N0/2os6JhlnAdX+A7c3taIuD6ysZP9SMgxBsjSP5PmYi7VntHArIMyH+hCi+LF88PUuXNAAEY2HQ0orOcIMhWAksGEG9Gr5Xg9CsWl4cGwSLtXVa6VK151ITCdgTUgN/vCcuYHDJ9q1dKdFQJke2ScbghvP9MhDWwfEM8KBz5wDk2dQCP7pC/IJdAsJY8yewzoVd4Sz8giL+QgXWZPemtpCYUIswP8xbBLHB5NApwl8l5Hz5CU1xDgQ2FHYS3U46RIu/cbgo0Yd4k= secuof@Davids-iMac.local

이후 접속하고자 하는 서버에 접속한 이후 아래와 같이 입력한다.

여기서는 .ssh 디렉토리가 없다고 가정한다. 참고로 ssh-keygen을 이용해서 이미 key값을 생성한 경우 ~/.ssh 폴더 및 id_rsa, id_rsa.pub 파일이 있을 것이다. 또한 authorized_keys 파일도 이미 사용되고 있을 경우 수 있기 때문에 참고 해서 아래의 예시를 참고 하길 바란다.

### 먼저 접속할 서버에 SSH로 접속
secuof@mypc $ ssh secuof@ssh-connect-server.com
### 홈디렉토리 밑에 .ssh 폴더 생성
secuof@ssh-connect-server.com:/home/secuof$ mkdir ~/.ssh
### .ssh 디렉토리의 퍼미션을 700으로 셋팅
secuof@ssh-connect-server.com:/home/secuof$ chmod 700 .ssh
secuof@ssh-connect-server.com:/home/secuof$ cd .ssh
### authorized_keys 파일을 생성 (이미 존재할 경우 패스)
secuof@ssh-connect-server.com:/home/secuof$ touch authorized_keys
### authorized_keys 파일의 퍼미션을 600으로 셋팅
secuof@ssh-connect-server.com:/home/secuof$ chmod 600 authorized_keys
### authorized_keys 파일을 편집해서 Public Key를 입력
### 또는 `echo [public_key_string] >> ~/.ssh/authorized_keys` 와 같이 입력도 가능함.
secuof@ssh-connect-server.com:/home/secuof$ vi authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC9UoEhuJShwl0DukIeziOYLOJe/10grhbff4UVCojH1zjtY8tPlyw0a9/ZTh7a20k+W+iaXfWIx5w5sGo40ecwXDp4cVj2xJhm9dtnRBJuaUx03O/EUA4mNSBMLEH0k+xAiIjNxb1ql2X0SBFIM6VDTjfKkATJxAfIqJ1saompeWldSrWDjaXq0bg7VBKEXK7Jvkqg3U5pp4o5jFEukwBTqY8xxtzbgkqJJ7Z8RAhTyzePcEy/r9N0/2os6JhlnAdX+A7c3taIuD6ysZP9SMgxBsjSP5PmYi7VntHArIMyH+hCi+LF88PUuXNAAEY2HQ0orOcIMhWAksGEG9Gr5Xg9CsWl4cGwSLtXVa6VK151ITCdgTUgN/vCcuYHDJ9q1dKdFQJke2ScbghvP9MhDWwfEM8KBz5wDk2dQCP7pC/IJdAsJY8yewzoVd4Sz8giL+QgXWZPemtpCYUIswP8xbBLHB5NApwl8l5Hz5CU1xDgQ2FHYS3U46RIu/cbgo0Yd4k= secuof@Davids-iMac.local
:wq

Public Key 인증으로 SSH 로그인이 안되는 문제

퍼미션(Permission) 문제

.ssh 하위에는 아래의 파일들이 위치하는데 퍼미션이 너무 많이 오픈되어 있을 경우 SSH Key로 접속하지 못할수 있기 때문에 아래의 파일들은 해당 퍼미션으로 조정해야 한다.

만약 접속서버의 authorized_keys 파일이 600이 아닐경우 Public Key로 접속하지 못하는 문제가 발생할수 있다.

  • id_rsa : 600
  • id_rsa.pub : 644
  • authorized_keys : 600
  • known_hosts : 644

SSH 설정 문제

SSH Server의 설정이 안되어 있을 경우 접속이 안되는 문제가 발생할수 있다. 이경우 /etc/ssh/sshd_config 파일의 아래 값이 주석 처리 되어 있거나 yes로 Enable 되어 있지 않을 경우 발생할수 있다.

$ sudo vi /etc/ssh/sshd_config
### Public Key를 이용한 인증 허용 여부
PubkeyAuthentication yes
### 접속허용할 Public Key를 저장하고 있는 파일의 경로
AuthorizedKeysFile        .ssh/authorized_keys

### SSH 서버 재시작
$ sudo service sshd restart

Secuof Blog