Home > Troubleshooting > πŸ”[Troubleshooting] πŸš€ Bastion Host의 Key Pair λ³΅κ΅¬ν•˜κΈ°

πŸ”[Troubleshooting] πŸš€ Bastion Host의 Key Pair λ³΅κ΅¬ν•˜κΈ°
Troubleshooting Backend Development AWS

πŸš€ Bastion Host의 Key Pair λ³΅κ΅¬ν•˜κΈ°

πŸ“‹ 문제 상황

ν˜„μž¬ 쑰건:

  • Ubuntu 24.04 μ‚¬μš©
  • SSM IAM Role μ—†μŒ β†’ SSM Session Manager 접속 λΆˆκ°€λŠ₯
  • Status Check 정상 (EC2 μ½˜μ†”μ—μ„œ 3/3 검사 톡과)
  • Key Pair λΆ„μ‹€ β†’ κΈ°μ‘΄ λ°©μ‹μœΌλ‘œ SSH 접속 λΆˆκ°€

ν•΄κ²° 방법:

EBS λ³Όλ₯¨μ„ λΆ„λ¦¬ν•˜μ—¬ λ‹€λ₯Έ EC2에 μ—°κ²°ν•œ ν›„ authorized_keysλ₯Ό μˆ˜μ •ν•˜λŠ” 방식이 μœ μΌν•˜κ³  ν™•μ‹€ν•œ λ°©λ²•μž…λ‹ˆλ‹€.


βœ… 볡ꡬ ν”„λ‘œμ„ΈμŠ€

1️⃣ Bastion Host 쀑지

AWS Console β†’ EC2 β†’ Bastion Host 선택 β†’ Stop

⚠️ 주의: Terminateκ°€ μ•„λ‹Œ Stop을 μ„ νƒν•˜μ„Έμš”!


2️⃣ 루트 λ³Όλ₯¨(EBS) 뢄리

  1. EC2 상세 ν™”λ©΄ β†’ Storage νƒ­
  2. Root volume 선택 β†’ Actions β†’ Detach Volume
  3. Detach μ™„λ£Œ λŒ€κΈ° (μ•½ 5~10초)

3️⃣ μž„μ‹œ EC2 μΈμŠ€ν„΄μŠ€ 생성

생성 쑰건:

  • λ™μΌν•œ AZ에 생성 (Attachλ₯Ό μœ„ν•΄ ν•„μˆ˜)
  • μΈμŠ€ν„΄μŠ€ νƒ€μž…: t3.micro (Ubuntu 24.04)
  • μƒˆλ‘œμš΄ Key Pair 생성 및 λ‹€μš΄λ‘œλ“œ

πŸ’‘ 이 EC2λŠ” λ³Όλ₯¨ 파일 μˆ˜μ •μ„ μœ„ν•œ μž„μ‹œ μž‘μ—…μš©μž…λ‹ˆλ‹€.


4️⃣ λΆ„λ¦¬ν•œ λ³Όλ₯¨μ„ μž„μ‹œ EC2에 μ—°κ²°

  1. Volumes β†’ Detached μƒνƒœμ˜ EBS 선택
  2. Actions β†’ Attach Volume
  3. Instance: μž„μ‹œ EC2 선택
  4. Device name: /dev/sdf (μžλ™ μ„€μ •)

5️⃣ μž„μ‹œ EC2에 SSH 접속 및 λ³Όλ₯¨ 마운트

β‘  λ³Όλ₯¨ μ—°κ²° 확인

lsblk

좜λ ₯ μ˜ˆμ‹œ:

nvme0n1      β†’ μž„μ‹œ EC2의 루트 λ³Όλ₯¨
nvme1n1      β†’ Bastion Hostμ—μ„œ κ°€μ Έμ˜¨ λ³Όλ₯¨
β”œβ”€nvme1n1p1  β†’ Bastion Host의 루트 νŒŒν‹°μ…˜ (마운트 λŒ€μƒ)

β‘‘ 마운트 디렉토리 생성

sudo mkdir -p /mnt/bastion

β‘’ Bastion Host 루트 νŒŒν‹°μ…˜ 마운트

sudo mount /dev/nvme1n1p1 /mnt/bastion

πŸ“Œ μž₯치 이름은 lsblk 결과에 따라 /dev/xvdf1 λ˜λŠ” /dev/nvme1n1p1둜 λ‹€λ₯Ό 수 μžˆμŠ΅λ‹ˆλ‹€.


6️⃣ authorized_keys 파일 μˆ˜μ •

β‘  SSH 디렉토리 확인

Bastion Host의 μ‚¬μš©μžκ°€ ubuntu인 경우:

ls -al /mnt/bastion/home/ubuntu/.ssh

β‘‘ authorized_keys 파일 μ—΄κΈ°

sudo nano /mnt/bastion/home/ubuntu/.ssh/authorized_keys

β‘’ μƒˆλ‘œμš΄ Public Key μΆ”κ°€

파일 맨 μ•„λž˜μ— μƒˆλ‘œμš΄ Public Keyλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC... your_email@example.com
  • CTRL + O β†’ μ €μž₯
  • CTRL + X β†’ μ’…λ£Œ

β‘£ κΆŒν•œ μ„€μ • (ν•„μˆ˜)

sudo chown ubuntu:ubuntu /mnt/bastion/home/ubuntu/.ssh/authorized_keys
sudo chmod 600 /mnt/bastion/home/ubuntu/.ssh/authorized_keys
sudo chmod 700 /mnt/bastion/home/ubuntu/.ssh

⚠️ μ€‘μš”: authorized_keysλŠ” λ°˜λ“œμ‹œ 600 κΆŒν•œμ΄μ–΄μ•Ό SSHκ°€ μž‘λ™ν•©λ‹ˆλ‹€.


7️⃣ λ³Όλ₯¨ 뢄리 및 μž¬μ—°κ²°

β‘  마운트 ν•΄μ œ

sudo umount /mnt/bastion

β‘‘ AWS Consoleμ—μ„œ λ³Όλ₯¨ μž¬μ—°κ²°

  1. μž„μ‹œ EC2μ—μ„œ Detach Volume
  2. Bastion Host에 Attach Volume
    • Device: /dev/sda1 (μžλ™ μ„€μ •)

8️⃣ Bastion Host μ‹œμž‘ 및 SSH 접속 ν…ŒμŠ€νŠΈ

β‘  Bastion Host μ‹œμž‘

AWS Console β†’ Bastion Host β†’ Start

β‘‘ SSH 접속

ssh -i ~/.ssh/your_new_key.pem ubuntu@<Bastion-Host-Public-IP>

πŸŽ‰ 볡ꡬ μ™„λ£Œ!


πŸ”₯ FAQ: .pem 파일과 Public Key

Q. β€œμ œκ°€ μƒμ„±ν•œ ν‚€νŽ˜μ–΄λŠ” .pem 파일인데, authorized_keys에 무엇을 λ„£μ–΄μ•Ό ν•˜λ‚˜μš”?”

A. .pem νŒŒμΌμ€ Private Keyμ΄λ―€λ‘œ, Public Keyλ₯Ό μΆ”μΆœν•΄μ„œ λ„£μ–΄μ•Ό ν•©λ‹ˆλ‹€.

πŸ” μ΄ν•΄ν•˜κΈ°

ν•­λͺ© μ„€λͺ…
.pem 파일 Private Key (κ°œμΈν‚€) - μ ˆλŒ€ μ„œλ²„μ— λ„£μœΌλ©΄ μ•ˆ 됨
authorized_keys Public Key (κ³΅κ°œν‚€)만 μ €μž₯ν•˜λŠ” κ³³

πŸ“ .pemμ—μ„œ Public Key μΆ”μΆœ 방법

둜컬 ν„°λ―Έλ„μ—μ„œ μ‹€ν–‰:

ssh-keygen -y -f your_key.pem > your_key.pub

μ˜ˆμ‹œ:

ssh-keygen -y -f bastion.pem > bastion.pub

Public Key 확인:

cat bastion.pub

좜λ ₯ μ˜ˆμ‹œ:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... 

이 λ‚΄μš©μ„ authorized_keys에 μΆ”κ°€ν•˜λ©΄ λ©λ‹ˆλ‹€.

🎯 μ΅œμ’… SSH 접속

ssh -i bastion.pem ubuntu@<Bastion-Host-Public-IP>

πŸ“Œ 체크리슀트

볡ꡬ 과정을 μ™„λ£Œν–ˆλ‹€λ©΄ μ•„λž˜ ν•­λͺ©μ„ ν™•μΈν•˜μ„Έμš”:

  • Bastion Hostκ°€ Stop μƒνƒœμΈκ°€? (Terminate μ•„λ‹˜)
  • μž„μ‹œ EC2κ°€ λ™μΌν•œ AZ에 μžˆλŠ”κ°€?
  • λ³Όλ₯¨μ΄ μ œλŒ€λ‘œ λ§ˆμš΄νŠΈλ˜μ—ˆλŠ”κ°€? (lsblk둜 확인)
  • Public Keyλ₯Ό authorized_keys에 μΆ”κ°€ν–ˆλŠ”κ°€?
  • κΆŒν•œμ΄ μ˜¬λ°”λ₯Έκ°€? (파일: 600, 디렉토리: 700)
  • 마운트λ₯Ό ν•΄μ œν–ˆλŠ”κ°€? (umount)
  • λ³Όλ₯¨μ΄ Bastion Host에 /dev/sda1둜 μž¬μ—°κ²°λ˜μ—ˆλŠ”κ°€?
  • Bastion Hostκ°€ μ‹œμž‘λ˜μ—ˆλŠ”κ°€?
  • SSH 접속이 μ„±κ³΅ν–ˆλŠ”κ°€?

🚨 μ£Όμ˜μ‚¬ν•­

  1. μ ˆλŒ€ Private Key(.pem)λ₯Ό μ„œλ²„μ— μ˜¬λ¦¬μ§€ λ§ˆμ„Έμš”
    • λ³΄μ•ˆμƒ μ‹¬κ°ν•œ μœ„ν—˜μž…λ‹ˆλ‹€.
  2. authorized_keys κΆŒν•œμ€ λ°˜λ“œμ‹œ 600
    • κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ SSHκ°€ κ±°λΆ€λ©λ‹ˆλ‹€.
  3. λ³Όλ₯¨ Detach/Attach μ‹œ λ°˜λ“œμ‹œ λ™μΌν•œ AZ 확인
    • λ‹€λ₯Έ AZ의 μΈμŠ€ν„΄μŠ€μ—λŠ” μ—°κ²°ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
  4. μž„μ‹œ EC2λŠ” μž‘μ—… μ™„λ£Œ ν›„ μ‚­μ œ κ°€λŠ₯
    • λΉ„μš© μ ˆκ°μ„ μœ„ν•΄ μž‘μ—… μ™„λ£Œ ν›„ Terminateν•˜μ„Έμš”.

πŸŽ“ 정리

이 방법은 SSM Session Managerλ₯Ό μ‚¬μš©ν•  수 μ—†κ³ , κΈ°μ‘΄ Key Pairλ₯Ό λΆ„μ‹€ν–ˆμ„ λ•Œ 100% ν™•μ‹€ν•˜κ²Œ 볡ꡬ할 수 μžˆλŠ” λ°©λ²•μž…λ‹ˆλ‹€.

핡심은 EBS λ³Όλ₯¨μ„ λΆ„λ¦¬ν•˜μ—¬ λ‹€λ₯Έ EC2μ—μ„œ 파일 μ‹œμŠ€ν…œμ— μ ‘κ·Όν•˜κ³ , authorized_keysλ₯Ό 직접 μˆ˜μ •ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

볡ꡬ ν›„μ—λŠ” μƒˆλ‘œμš΄ Key Pairλ₯Ό μ•ˆμ „ν•˜κ²Œ λ³΄κ΄€ν•˜κ³ , κ°€λŠ₯ν•˜λ‹€λ©΄ SSM Session Managerλ₯Ό μœ„ν•œ IAM Role을 μΆ”κ°€ν•˜μ—¬ ν–₯ν›„ μœ μ‚¬ν•œ 상황을 μ˜ˆλ°©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.