November 11, 2018

How to use the authentication key to connect an SSH server

01. Generate the RSA authentication key.
RSA 인증 키 생성.
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): myKey2
Enter passphrase (empty for no passphrase): ******
Enter same passphrase again: ******
Your identification has been saved in myKey2.
Your public key has been saved in myKey2.pub.
The key fingerprint is:
SHA256:/lmbh■■■■■■■■wBD78■■■■■■■■■■7qPtXAbkw ■■■@■■■■■
The key's randomart image is:
+---[RSA 2048]----+
|  .+. .... o .   |
|    +. o  = *    |
|     +o .+ B E   |
|    . o.. B * + .|
|     . ■■■■■■■■■|
|       .■■■■■■■■|
|        . .++ + o|
|         . +.+ o |
|          o o    |
+----[SHA256]-----+
$
$ ls
myKey2  myKey2.pub  tmp
$
$ mv ./myKey* ./.ssh/

myKey2 is the name of the RSA authentication key. If the path is not recorded, it is created in the home directory. Absolute paths are recommended because strings such as tilde (~) can not be used.

myKey2는 RSA 인증 키의 이름이다. 경로를 기록하지 않으면 홈 디렉토리에 생성된다. 틸드(~) 같은 문자열은 사용할 수 없기에 절대 경로를 권장한다.

If you input a password when you create it, you must enter it every time you connect to the SSH server.

생성 시 비밀번호를 입력하면, SSH 서버에 접속할 때마다 그 비밀번호를 입력해야 한다.

Generated the public/private keys are usually stored in "[Home directory]/.ssh/".

생성된 공개키와 개인키는 일반적으로 "[Home directory]/.ssh/"에 모아둔다.




02. Register the key on the server.
서버에 키 등록.
[Client side]
$ cat ~/.ssh/myKey2.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQ■■■■■■■■■■■■■■dw2H8IpjN2yUBtjidyHKFeXuPEXH■■■■■■■■■■■■■■nxR3sfimRzDk7wBOc/vBQniyZXImjL5b43n3aqqNXlhhv3QFD4R■■■■■■■■■■■■■■KZLKlLFoPUem2E6JVRqKLlUKt■■■■■■■■■■■■■■■■■■■■■wKJzsLojFRBdjWR0Bb0bz85HkVnsbBWm0LMcys■■■■■■■■■■■■■■87zQCoUXnpcUnFlx7l3RDjE+hGdjobJngZD9fkzRemni5z00rwHeC6dTjvctsrbmmaBle5M7nDcPlsujlPYR3LmzKZv41ExN8Yzkj71wLZm4K39 ■■■■■■■@■■■■■■■
$
$

[Server side]
$ vi /root/.ssh/authorized_keys

Copy the contents of the client's public key (*.pub) and paste it into the server's "authorized_keys" file.

클라이언트의 공개키(*.pub)의 내용을 복사하여, 서버의 authorized_keys 파일에 붙여넣는다.




03. Connect using the private key.
개인키로 서버에 접속.
$ ssh -i ~/.ssh/myKey2 root@192.168.■■■.■■■
Enter passphrase for key '/home/user/.ssh/myKey2':
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-43-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

369 packages can be updated.
186 updates are security updates.

New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Sun Nov 11 00:45:10 2018 from 192.168.200.198
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# id
uid=0(root) gid=0(root) groups=0(root)


Check environment  점검 환경
O   S Ubuntu 16.04(64 bits)
Cygwin_NT-6.1
CPU Intel i7
SHELL GNU bash (4.3.48) RAM 1 GB