홈/엔지니어/Linux / Shell/SSH Config 파일로 다중 서버 접속 관리 —…
Linux / Shell초급UbuntuDebianCentOSmacOSsshssh-config다중서버

SSH Config 파일로 다중 서버 접속 관리 — Host 별칭·점프 호스트·ssh-agent 설정

~/.ssh/config로 다중 서버 단축키·점프 호스트·키 파일·포트포워딩을 관리하고 ssh-agent로 키를 자동 로드하는 방법을 설명합니다.

목차
  1. ~/.ssh/config 기본 구조
  2. 기본 서버 등록
  3. 글로벌 기본값 설정
  4. 점프 호스트 (Bastion/Jump Server)
  5. 포트포워딩 자동 설정
  6. ssh-agent — 키 자동 관리
  7. 파일 권한 (중요)

~/.ssh/config 기본 구조

CODE
Host <별칭>
    HostName <실제 IP 또는 도메인>
    User <사용자명>
    Port <포트>
    IdentityFile <키 파일 경로>

파일 생성:

Bash
mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

기본 서버 등록

CODE
# ~/.ssh/config

# 운영 서버
Host prod
    HostName 203.0.113.10
    User deploy
    Port 2222
    IdentityFile ~/.ssh/prod_rsa

# 개발 서버
Host dev
    HostName 203.0.113.20
    User ubuntu
    IdentityFile ~/.ssh/dev_rsa

# AWS 인스턴스
Host aws-web
    HostName ec2-12-34-56-78.ap-northeast-2.compute.amazonaws.com
    User ec2-user
    IdentityFile ~/.ssh/my-key.pem
Bash
# 이제 이렇게 접속
ssh prod
ssh dev
ssh aws-web

글로벌 기본값 설정

CODE
Host *
    ServerAliveInterval 60      # 60초마다 keepalive
    ServerAliveCountMax 3       # 3번 응답 없으면 종료
    AddKeysToAgent yes          # ssh-agent에 자동 추가
    IdentityFile ~/.ssh/id_ed25519
    StrictHostKeyChecking ask   # 새 호스트 확인 요청

점프 호스트 (Bastion/Jump Server)

외부에서 직접 접속할 수 없는 내부 서버에 경유 서버를 통해 접속합니다.

CODE
Host bastion
    HostName 203.0.113.1
    User admin
    IdentityFile ~/.ssh/bastion_rsa

Host internal-db
    HostName 10.0.1.50
    User ubuntu
    IdentityFile ~/.ssh/internal_rsa
    ProxyJump bastion          # bastion 경유
    # 구버전 OpenSSH:
    # ProxyCommand ssh -W %h:%p bastion
Bash
ssh internal-db    # bastion 경유 자동 접속

포트포워딩 자동 설정

CODE
Host tunnel-db
    HostName prod-server.example.com
    User deploy
    LocalForward 15432 localhost:5432   # 로컬 15432 → 서버 5432
    LocalForward 16379 localhost:6379   # 로컬 16379 → 서버 6379
Bash
ssh -N tunnel-db    # 포트포워딩만 (셸 없이)
# 이제 로컬에서 localhost:15432로 PostgreSQL 접속 가능

ssh-agent — 키 자동 관리

비밀번호 없이 키를 반복 사용하려면 ssh-agent를 활용합니다.

Bash
# agent 시작
eval "$(ssh-agent -s)"

# 키 등록
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/prod_rsa

# 등록된 키 목록
ssh-add -l

# 모든 키 제거
ssh-add -D

# macOS: Keychain에 저장 (재부팅 후에도 유지)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

~/.ssh/config에 추가하면 자동 로드됩니다:

CODE
Host *
    AddKeysToAgent yes
    UseKeychain yes    # macOS 전용

파일 권한 (중요)

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

권한이 너무 넓으면 SSH가 키 파일 사용을 거부합니다.

#ssh#ssh-config#다중서버#jump-host#ssh-agent
편집 안내 · Editorial Note

AI 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)GNU/Linux man 페이지 ↗

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.