|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Bitbucket Access Key로 SSH Push 설정하기" |
| 4 | +date: 2023-12-10 09:00:00 +0900 |
| 5 | +categories: [Development, Tutorial] |
| 6 | +tags: [bitbucket, ssh, git, devops, setup, tutorial] |
| 7 | +author: "Kevin Park" |
| 8 | +excerpt: "Bitbucket Private Repository에 SSH Key를 사용하여 비밀번호 없이 안전하게 push하는 완전한 설정 가이드" |
| 9 | +--- |
| 10 | + |
| 11 | +# Bitbucket Access Key로 SSH Push 설정하기 |
| 12 | + |
| 13 | +## 🎯 Summary |
| 14 | + |
| 15 | +**Bitbucket Private Repository에 SSH Key를 사용하여 비밀번호 없이 push하는 방법** |
| 16 | + |
| 17 | +### 핵심 단계 |
| 18 | +1. **SSH Key 생성** |
| 19 | +```bash |
| 20 | +ssh-keygen -t rsa -C "your-email@example.com" |
| 21 | +# Enter 키만 눌러서 기본 설정으로 생성 |
| 22 | +``` |
| 23 | + |
| 24 | +2. **SSH Agent 설정** |
| 25 | +```bash |
| 26 | +# SSH Agent 시작 |
| 27 | +eval "$(ssh-agent -s)" |
| 28 | + |
| 29 | +# 생성된 키를 SSH Agent에 추가 |
| 30 | +ssh-add ~/.ssh/id_rsa |
| 31 | + |
| 32 | +# 등록 확인 |
| 33 | +ssh-add -l |
| 34 | +``` |
| 35 | + |
| 36 | +3. **Public Key 복사** |
| 37 | +```bash |
| 38 | +cat ~/.ssh/id_rsa.pub |
| 39 | +# 출력된 내용 전체를 복사 |
| 40 | +``` |
| 41 | + |
| 42 | +4. **Bitbucket Repository 설정** |
| 43 | + - Repository Settings → Access Keys → Add Key |
| 44 | + - Label 입력, Read/Write 권한 체크 |
| 45 | + - 복사한 Public Key 붙여넣기 |
| 46 | + |
| 47 | +5. **SSH 주소로 Push** |
| 48 | +```bash |
| 49 | +git remote set-url origin ssh://git@bitbucket.org:username/repository.git |
| 50 | +git push origin master |
| 51 | +``` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 📚 상세 설명 |
| 56 | + |
| 57 | +### 배경 및 필요성 |
| 58 | + |
| 59 | +GitHub에서 Bitbucket으로 서버를 변경하면서 Private Repository에 접근하는 방법이 달라집니다. 매번 아이디/비밀번호를 입력하는 번거로움을 피하고, 특히 CI/CD 파이프라인이나 자동화 스크립트에서 안전하게 Git 작업을 수행하기 위해 SSH Key 인증을 설정해야 합니다. |
| 60 | + |
| 61 | +### SSH Key 생성 과정 |
| 62 | + |
| 63 | +#### 1. SSH Key 생성 |
| 64 | +```bash |
| 65 | +# RSA 타입의 SSH Key 생성 |
| 66 | +ssh-keygen -t rsa -C "your-email@example.com" |
| 67 | + |
| 68 | +# 실행 결과 예시 |
| 69 | +Generating public/private rsa key pair. |
| 70 | +Enter file in which to save the key (/root/.ssh/id_rsa): [Enter] |
| 71 | +Enter passphrase (empty for no passphrase): [Enter] |
| 72 | +Enter same passphrase again: [Enter] |
| 73 | +``` |
| 74 | + |
| 75 | +**주요 옵션:** |
| 76 | +- `-t rsa`: RSA 암호화 알고리즘 사용 |
| 77 | +- `-C`: 코멘트 추가 (보통 이메일 주소) |
| 78 | +- Enter만 누르면 기본 경로와 빈 패스프레이즈로 설정 |
| 79 | + |
| 80 | +#### 2. 생성된 파일 확인 |
| 81 | +```bash |
| 82 | +ls -la ~/.ssh/ |
| 83 | +# id_rsa (개인키), id_rsa.pub (공개키) 파일 확인 |
| 84 | + |
| 85 | +# 권한 설정 (보안상 중요) |
| 86 | +chmod 600 ~/.ssh/id_rsa |
| 87 | +chmod 644 ~/.ssh/id_rsa.pub |
| 88 | +``` |
| 89 | + |
| 90 | +### SSH Agent 설정 |
| 91 | + |
| 92 | +#### SSH Agent 시작 및 키 등록 |
| 93 | +```bash |
| 94 | +# SSH Agent 백그라운드 실행 |
| 95 | +eval "$(ssh-agent -s)" |
| 96 | +# Agent pid 1234 와 같은 메시지 출력 |
| 97 | + |
| 98 | +# SSH 키를 Agent에 추가 |
| 99 | +ssh-add ~/.ssh/id_rsa |
| 100 | + |
| 101 | +# 등록된 키 확인 |
| 102 | +ssh-add -l |
| 103 | +# 2048 SHA256:... /root/.ssh/id_rsa (RSA) 형태로 출력 |
| 104 | +``` |
| 105 | + |
| 106 | +**SSH Agent를 사용하는 이유:** |
| 107 | +- 한 번 키를 로드하면 세션 동안 재입력 불필요 |
| 108 | +- 여러 저장소에 동일한 키 사용 가능 |
| 109 | +- 보안상 메모리에서만 키 관리 |
| 110 | + |
| 111 | +### Bitbucket Access Key 등록 |
| 112 | + |
| 113 | +#### 1. Repository 설정 접근 |
| 114 | +1. Bitbucket Repository 페이지 이동 |
| 115 | +2. **Settings** 클릭 |
| 116 | +3. **Access Management** → **Access Keys** 선택 |
| 117 | + |
| 118 | +#### 2. Access Key 추가 |
| 119 | +```bash |
| 120 | +# Public Key 내용 복사 |
| 121 | +cat ~/.ssh/id_rsa.pub |
| 122 | +# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... your-email@example.com |
| 123 | +``` |
| 124 | + |
| 125 | +**설정 옵션:** |
| 126 | +- **Label**: 키를 구분할 수 있는 이름 (예: "Production Server Key") |
| 127 | +- **Key**: 복사한 Public Key 전체 내용 |
| 128 | +- **Permissions**: |
| 129 | + - ✅ **Read**: 저장소 클론/풀 권한 |
| 130 | + - ✅ **Write**: 푸시 권한 (필요시 체크) |
| 131 | + |
| 132 | +### SSH 연결 테스트 및 Push |
| 133 | + |
| 134 | +#### 1. SSH 연결 테스트 |
| 135 | +```bash |
| 136 | +# Bitbucket SSH 연결 테스트 |
| 137 | +ssh -T git@bitbucket.org |
| 138 | + |
| 139 | +# 성공시 출력 예시: |
| 140 | +# logged in as username. |
| 141 | +# You can use git or hg to connect to Bitbucket. |
| 142 | +``` |
| 143 | + |
| 144 | +#### 2. Remote URL 변경 |
| 145 | +```bash |
| 146 | +# 현재 remote URL 확인 |
| 147 | +git remote -v |
| 148 | + |
| 149 | +# HTTPS에서 SSH로 변경 |
| 150 | +git remote set-url origin ssh://git@bitbucket.org/username/repository.git |
| 151 | + |
| 152 | +# 또는 git clone시 SSH 주소 사용 |
| 153 | +git clone ssh://git@bitbucket.org/username/repository.git |
| 154 | +``` |
| 155 | + |
| 156 | +#### 3. Push 실행 |
| 157 | +```bash |
| 158 | +git add . |
| 159 | +git commit -m "SSH key setup test" |
| 160 | +git push origin master |
| 161 | + |
| 162 | +# 비밀번호 입력 없이 push 성공 |
| 163 | +# Enumerating objects: 12, done. |
| 164 | +# Compressing objects: 100% (11/11), done. |
| 165 | +# Total 12 (delta 6), reused 0 (delta 0) |
| 166 | +# To ssh://git@bitbucket.org/username/repository.git |
| 167 | +# ca052fa..57740e4 master -> master |
| 168 | +``` |
| 169 | + |
| 170 | +### 실제 활용 사례 |
| 171 | + |
| 172 | +#### Jenkins 자동 백업 설정 |
| 173 | +```bash |
| 174 | +#!/bin/bash |
| 175 | +# Jenkins 백업 스크립트에서 SSH Key 활용 |
| 176 | + |
| 177 | +# 백업 파일 생성 |
| 178 | +tar -czf jenkins_backup_$(date +%Y%m%d).tar.gz /var/lib/jenkins/ |
| 179 | + |
| 180 | +# Git에 자동 커밋 및 푸시 |
| 181 | +git add . |
| 182 | +git commit -m "Jenkins backup $(date +%Y-%m-%d)" |
| 183 | +git push origin master |
| 184 | +``` |
| 185 | + |
| 186 | +#### 다중 저장소 관리 |
| 187 | +```bash |
| 188 | +# ~/.ssh/config 파일로 여러 키 관리 |
| 189 | +Host bitbucket-work |
| 190 | + HostName bitbucket.org |
| 191 | + User git |
| 192 | + IdentityFile ~/.ssh/id_rsa_work |
| 193 | + |
| 194 | +Host bitbucket-personal |
| 195 | + HostName bitbucket.org |
| 196 | + User git |
| 197 | + IdentityFile ~/.ssh/id_rsa_personal |
| 198 | + |
| 199 | +# 사용법 |
| 200 | +git clone ssh://bitbucket-work/company/project.git |
| 201 | +git clone ssh://bitbucket-personal/username/personal-project.git |
| 202 | +``` |
| 203 | + |
| 204 | +### 주요 문제 해결 |
| 205 | + |
| 206 | +#### Permission Denied 오류 |
| 207 | +```bash |
| 208 | +# SSH 키 권한 확인 |
| 209 | +ls -la ~/.ssh/id_rsa |
| 210 | +# -rw------- 1 user user ... id_rsa (600 권한 필요) |
| 211 | + |
| 212 | +# 권한 수정 |
| 213 | +chmod 600 ~/.ssh/id_rsa |
| 214 | +``` |
| 215 | + |
| 216 | +#### SSH Agent 연결 실패 |
| 217 | +```bash |
| 218 | +# SSH Agent 상태 확인 |
| 219 | +ps aux | grep ssh-agent |
| 220 | + |
| 221 | +# Agent 재시작 |
| 222 | +killall ssh-agent |
| 223 | +eval "$(ssh-agent -s)" |
| 224 | +ssh-add ~/.ssh/id_rsa |
| 225 | +``` |
| 226 | + |
| 227 | +## 결론 |
| 228 | + |
| 229 | +SSH Key를 사용한 Bitbucket 인증 설정은 보안성과 편의성을 모두 제공하는 필수적인 개발 환경 구성입니다. 특히 자동화된 CI/CD 환경에서는 비밀번호 입력 없이 Git 작업을 수행할 수 있어 매우 유용합니다. |
| 230 | + |
| 231 | +**핵심 포인트:** |
| 232 | +- SSH Key는 한 번 설정하면 영구적으로 사용 가능 |
| 233 | +- Public Key만 서버에 등록하므로 보안상 안전 |
| 234 | +- 여러 저장소와 서버에서 동일한 키 재사용 가능 |
| 235 | +- Jenkins, GitHub Actions 등 자동화 도구와의 연동이 간편 |
| 236 | + |
| 237 | +**다음 단계:** |
| 238 | +- SSH Config 파일을 활용한 다중 계정 관리 |
| 239 | +- GPG Key를 추가한 커밋 서명 설정 |
| 240 | +- 2FA(Two-Factor Authentication)와 SSH Key 조합 사용 |
0 commit comments