[Linux] 키 페어를 생성해 On-Prem 서버 SSH 보안 강화하기

[Linux] 키 페어를 생성해 On-Prem 서버 SSH 보안 강화하기


Linux SSH KeyPair
  • AWS와 같은 클라우드 플랫폼을 사용하면 인스턴스에 SSH로 접근할 때 보안을 위한 키 페어를 요구합니다.
  • 이것을 제가 운영하는 On-Prem 서버에도 적용해보겠습니다.

맥에서 SSH 키 생성하기

> ssh-keygen -t rsa -b 4096
  • ssh-keygen 명령어는 SSH를 위한 공개키 / 개인키 쌍을 생성합니다.
  • Flags
    • -t, <type>
      • 생성할 키의 유형을 지정합니다.
      • rsa, dsa, ecdsa 등이 있습니다.
    • -b, <bits>
      • 키의 길이를 비트 단위로 지정합니다.
      • RSA 키의 경우 일반적으로 2048 비트 이상을 사용합니다.
      • 보안을 위해서 4096 비트로 지정했습니다.
Generating public/private rsa key pair.

Enter file in which to save the key (/Users/baeseungwon/.ssh/id_rsa): /Users/baeseungwon/.ssh/---
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/baeseungwon/.ssh/---
Your public key has been saved in /Users/baeseungwon/.ssh/---.pub
The key fingerprint is:
SHA256:AqAXV7sercdshajgfgE6plbLn3OyL2FRLSGFsdsGNFyOP4 baeseungwon@Baeseungwon-MacBook-Pro.local
The key's randomart image is:
+---[RSA 4096]----+
|  o ...          |
| . +   +         |
|. . . * +        |
| .   o O +       |
|      X S o      |
|     B O o       |
|  . *.B.E        |
|   @o*..o.       |
|  ++Bo**+        |
+----[SHA256]-----+
  • 키가 저장될 경로를 지정해주면 키가 생성됩니다.
  • 총 두 개 파일이 해당 경로에 생성되는데, 각각 개인 키(Private Key)와 공개 키(Public Key)입니다.

공개 키를 Ubuntu 서버에 설치하기

> ssh-copy-id -i ~/.ssh/---.pub -p XXXX bae@IP-ADDRESS
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/baeseungwon/.ssh/---.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
bae@IP-ADDRESS's password:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh -p 'XXXX' 'bae@IP-ADDRESS'"
and check to make sure that only the key(s) you wanted were added.

서버에서 SSH 설정

> sudo vi /etc/ssh/sshd_config
PubkeyAuthentication    yes
AuthorizedKeysFile      .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication  no
> sudo systemctl restart ssh

비밀번호를 사용한 접속 테스트

> ssh bae@IP-ADDRESS -p XXXX
bae@IP-ADDRESS: Permission denied (publickey).
  • 이제 키를 사용하지 않고 접속하면 거부됩니다.

키 페어를 사용한 접속 테스트

> ssh -i ~/.ssh/--- -p 8411 bae@IP-ADDRESS
Enter passphrase for key '/Users/baeseungwon/.ssh/personal_server':
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.0-105-generic x86_64)

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

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

8 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm

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

Your Hardware Enablement Stack (HWE) is supported until April 2025.
*** System restart required ***
Last login: Fri May 10 14:52:05 2024 from IP-ADDRESS
  • 키를 사용해 접속이 가능합니다.
© 2024 Seungwon Bae 🇰🇷