본문 바로가기
Infra2024년 5월 28일5분 읽기

SSH 키 관리 — ssh-agent와 config 파일 활용법

YS
김영삼
조회 762

SSH 키 생성

SSH 키 쌍은 비대칭 암호화를 기반으로 하며, 개인키(private key)와 공개키(public key)로 구성됩니다. 현재 권장되는 알고리즘은 Ed25519입니다.

ssh-keygen -t ed25519 -C "user@example.com" -f ~/.ssh/id_ed25519
ssh-keygen -t rsa -b 4096 -C "user@example.com" -f ~/.ssh/id_rsa
ssh-keygen -p -f ~/.ssh/id_ed25519

ssh-agent 활용

ssh-agent는 개인키를 메모리에 캐싱하여, 매 접속 시 패스프레이즈를 다시 입력할 필요 없게 해주는 백그라운드 프로세스입니다.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -t 3600 ~/.ssh/id_ed25519
ssh-add -l
ssh-add -D

macOS Keychain 연동

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

# ~/.ssh/config에 추가
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

~/.ssh/config 파일 설정

# ~/.ssh/config

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3
  AddKeysToAgent yes
  IdentitiesOnly yes

Host prod
  HostName 10.0.1.100
  User deploy
  Port 2222
  IdentityFile ~/.ssh/prod_ed25519
  ForwardAgent no

Host staging
  HostName 192.168.1.50
  User admin
  ProxyJump bastion
  IdentityFile ~/.ssh/staging_ed25519

Host bastion
  HostName bastion.example.com
  User jump
  Port 22
  IdentityFile ~/.ssh/bastion_ed25519

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_personal_ed25519

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_work_ed25519

다중 GitHub 계정 사용

git clone git@github-personal:myuser/personal-repo.git
git clone git@github-work:company/work-repo.git

cd work-repo
git config user.email "me@company.com"
git config user.name "My Name"

보안 강화

키 파일 권한 설정

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/authorized_keys

서버 측 보안 설정

# /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
MaxAuthTries 3
AllowUsers deploy admin

sudo systemctl restart sshd

SSH 터널링

종류용도명령
로컬 포워딩 (-L)원격 서비스에 로컬 접속ssh -L 5432:db:5432 bastion
리모트 포워딩 (-R)로컬 서비스를 원격에 노출ssh -R 8080:localhost:3000 server
동적 포워딩 (-D)SOCKS 프록시ssh -D 1080 server

실전 팁

  • 용도별로 SSH 키를 분리하세요 (GitHub용, 서버 접속용, 배포용 등).
  • 패스프레이즈 없는 키는 자동화(CI/CD)에서만 사용하고, 개인 키에는 반드시 패스프레이즈를 설정하세요.
  • SSH Agent Forwarding은 신뢰할 수 있는 서버에서만 사용하세요. 악의적 관리자가 키를 도용할 수 있습니다.
  • 정기적으로 authorized_keys를 점검하여 사용하지 않는 키를 제거하세요.

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록