본문 바로가기
Programming

google collab vscode ssh 연결하기

by 느리게 걷는 즐거움 2022. 8. 20.
728x90
반응형

Collab-ssh 라이브러리

collab-ssh라이브러리는 라이브러리 홈페이지에서 아래와 같이 google collab 노트북과 ssh tunnel을 연결하는 라이브러리라고 설명하고 있습니다.
https://pypi.org/project/colab-ssh/

실행을 위해서 아래의 코드를 연결할 코랩 노트북에서 실행합니다. launch_ssh_cloudflared() 암호는 VSCODE에서 연결 시 입력할 암호를 설정합니다.

# Install colab_ssh on google colab
!pip install colab_ssh --upgrade

from colab_ssh import launch_ssh_cloudflared, init_git_cloudflared
launch_ssh_cloudflared(password="<PUT_YOUR_PASSWORD_HERE>")

CloudFlare 설치

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation

자신의 운영체제에 맞는 cloudflare파일을 다운로드하여 실행이 가능한 로컬 컴퓨터 위치에 복사합니다. 이 파일을 이용해서 내컴퓨터의 ssh 연결을 위해 필요하며 VSCODE의 config파일을 통해서 실행됩니다. 저는 c:\에 위치한 상태를 가정했습니다.

VSCODE

VSCODE에서 ssh로 서버에 연결을 도와주는 extension인 remote-ssh를 설치합니다. VSCODE의 extension을 선택해서 "remote-ssh"를 설치합니다.

remote-ssh설치가 끝났으면 VSCODE에서 ctrl + shift + p를 눌러 command 창을 실행 시킨 후 collab 연결에 사용하는 cloudflare 서버 설정을 위해 remote ssh 설정 수정합니다.

수정할 수 있는 .config파일이 여러 개 나타납니다. Window의 경우 default로 C:\User\<사용자명>\.ssh\config 파일을 사용합니다. 이 파일에 cloundflare 서버관련 설정 정보를 입력합니다.
아래와 같이 입력 시 마지막 ProcyCommand 부분의 cloudflared.exe 파일의 위치는 다운로드한 cloudflare 실행파일이 위치 정보를 입력합니다.

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host *trycloudflare.com
    HostName %h
    User root
    Port 22
    ProxyCommand c:\cloudflared.exe access ssh --hostname %h

 

Google Collab ssh로 연결하기

테스트를 위해 collab 노트북 파일을 생성하고 collab-ssh를 실행합니다. cloundflare로 연결할 예정이고 password는 "1234"로 합니다. collab-ssh가 실행되면 코랩 노트북 연결에 사용된 ssh 주소가 생성됩니다. 이 주소는 코랩 연결을 위해 launch_ssh_cloudflared()를 수행할 때마다 달라집니다.

ssh 연결준비가 완료되었으니 구글 드라이브 마운트를 수행합니다.

VSCODE에서 연결을 위해서 VSCode Remote SSH아래의 링크를 복사하여 VSCODE의 ssh 연결을 실행합니다. ctrl+shift+p 를 이용하여 command 창을 실행한 후 Remote-SSH:Connect to Host를 실행합니다.

실행 후 연결한 연결정보를 collab에서 생성된 주소를 아래와 같이 입력합니다.

연결이 수행되면 host의 운영체제를 물어봅니다. google collab을 이용할 예정이니 "Linux"를 선택합니다.

연결을 위해 지문정보도 추가하고 설정한 암호인 "1234"를 입력합니다.

VSCODE의 terminal 창에서 폴더구조를 확인하면 /content/drive 가 정상적으로 mount된 것을 확인할 수 있습니다.

 

Reference

- https://blog.naver.com/PostView.nhn?blogId=ys10mjh&logNo=222328257839&parentCategoryNo=&categoryNo=29&viewDate=&isShowPopularPosts=true&from=search
- https://pypi.org/project/colab-ssh/

728x90
반응형