ssh 개념정리

SSH 완벽 가이드 (K8s & 리눅스 실무 기준)

1. SSH 키 생성과 보관

SSH 접속의 기본은 키 기반 인증입니다.

키 생성 (ed25519 권장)

ssh-keygen -t ed25519 -a 100 -C "yun@homelab" -f ~/.ssh/id_ed25519
  • -t ed25519: 키 타입을 ed25519로 설정 (보안+속도)
  • -a 100: KDF 라운드 수 (브루트포스 방어)
  • -C: 키 주석(Comment)
  • -f: 키 저장 경로

퍼미션 설정

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

공개키 배포

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host

2. ~/.ssh/config 설정 예제

Host *
  ServerAliveInterval 30
  ServerAliveCountMax 3
  TCPKeepAlive yes
  ControlMaster auto
  ControlPersist 10m
  ControlPath ~/.ssh/cm-%r@%h:%p
  IdentitiesOnly yes
  IdentityFile ~/.ssh/id_ed25519
  StrictHostKeyChecking ask
  UpdateHostKeys yes

Host bastion
  HostName 203.0.113.10
  User yun

Host k8s-master
  HostName 192.168.219.151
  User cadenza
  ProxyJump bastion

Host k8s-worker-1
  HostName 192.168.219.181
  User cadenza
  ProxyJump bastion

Host k8s-worker-2
  HostName 192.168.219.182
  User cadenza
  ProxyJump bastion

Host k8s-*
  Compression yes
  ForwardAgent no

3. 자주 쓰는 SSH 명령 패턴

  • 디버깅: ssh -vvv host
  • 특정 키로 접속: ssh -i ~/.ssh/otherkey host
  • 특정 포트: ssh -p 2222 host
  • 원격 명령 실행: ssh host "uptime"
  • 파일 복사: scp file.txt k8s-master:/tmp/
  • 디렉토리 동기화: rsync -avz -e ssh ./dir/ k8s-worker-1:/opt/dir/

4. 포트 포워딩 활용

로컬 포워딩 (-L)

ssh -L 16443:127.0.0.1:6443 k8s-master

→ 로컬에서 kubectl --server https://127.0.0.1:16443 가능

원격 포워딩 (-R)

ssh -R 18080:127.0.0.1:8080 bastion

동적 포워딩 (-D)

ssh -D 1080 bastion

5. 안전한 서버(sshd) 설정 (/etc/ssh/sshd_config)

Protocol 2
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
MaxAuthTries 3
LoginGraceTime 20
AllowUsers cadenza yun
ClientAliveInterval 60
ClientAliveCountMax 2
UsePAM yes
X11Forwarding no
AllowTcpForwarding yes
PermitTunnel no
GSSAPIAuthentication no

6. UFW 방화벽 SSH 제한 예제

ufw allow from 192.168.219.0/24 to any port 22
ufw allow from 100.64.0.0/10 to any port 22
ufw deny 22
ufw enable
ufw status numbered

7. 실무에서 자주 쓰는 패턴

  • 베스천 서버를 통한 모든 SSH 접속 (ProxyJump)
  • 멀티플렉싱으로 속도 향상
  • 포트 포워딩으로 내부 서비스 디버깅
  • tmux와 조합하여 세션 유지
  • authorized_keys 옵션으로 키별 권한 제한

8. 트러블슈팅

  • Permission denied 문제 → 권한, 소유권 확인
  • 호스트키 변경 경고 → ssh-keygen -R host 후 재등록
  • 느린 접속 → GSSAPIAuthentication no, UseDNS no
  • 연결 끊김 → ServerAliveInterval 설정

9. 치트시트

# 베스천 경유 원샷
ssh -J bastion k8s-master

# kubelet 로그 확인
ssh k8s-worker-1 "journalctl -u kubelet -n 300 -f"

# etcdctl 실행
ssh k8s-master "sudo ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 ..."

이 문서는 홈랩 K8s 운영 환경에서 SSH를 최대한 잘 쓰기 위해 작성된 가이드입니다.