개몽가

[GCP] 로컬에서 GCP VM 인스턴스 SSH 접속 본문

Infra/DevOps

[GCP] 로컬에서 GCP VM 인스턴스 SSH 접속

개몽가 2021. 8. 16. 14:50
  1. SSH 키 생성 여부 확인
    $ cat ~/.ssh/id_rsa.pub
    cat: /Users/shingme/.ssh/id_rsa.pub: No such file or directory


  2. SSH 키 생성
    $ cd /Users/shingme
    $ ssh-keygen -t rsa -C "shinmi@gmail.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/shingme/.ssh/id_rsa): /Users/shingme/.ssh/id_rsa_gcloudbysm
    Created directory '/Users/shingme/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    (생략....)​


  3. SSH 키 확인
    $ cd ~/.ssh
    $ ls
    id_rsa_gcloudbysm     id_rsa_gcloudbysm.pub​
    private key : id_rsa_gcloudbysm
    public key : id_rsa_gcloudbysm.pub

  4. public key를 GCP 메타데이터 등록 (간략하게 기입)
    GCP console > Compute Engine > 설정 > 메타데이터 > SSH 
    수정 클릭 
    항목 추가
    public key 값 복붙하면 됨

  5. GCP VM인스턴스 접속 
    $ ssh -i ~/.ssh/id_rsa_gcloudbysm shinmi@xx.xx.xx.xx
    The authenticity of host 'xx.xx.xx.xx' can't be established.
    ECDSA key fingerprint is SHA256:...
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added 'xx.xx.xx.xx' (ECDSA) to the list of known hosts.
    Enter passphrase for key '/Users/shingme/.ssh/id_rsa_gcloudbysm': 
    Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-1038-gcp x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
     * Super-optimized for small spaces - read how we shrank the memory
       footprint of MicroK8s to make it the smallest full K8s around.
    
       https://ubuntu.com/blog/microk8s-memory-optimisation
    (생략...)


  6. 터미널 ssh config 등록
    ~/.ssh 에 config 파일 생성 후 아래와 같이 설정
    Host gcp-dev           # 호스트명
    HostName xx.xx.xx.xx   # 퍼블릭 IP주소
    User shinmi            # 사용자 이름
    IdentityFile ~/.ssh/id_rsa_gcloudbysm  # 프라이빗키 파일 경로


  7. GCP VM 인스턴스 접속
    $ ssh gcp-dev​