리셋 되지 말자

[vagrant] vagrant, VSCode Remote SSH 본문

Infra

[vagrant] vagrant, VSCode Remote SSH

kyeongjun-dev 2021. 6. 17. 12:53

필요사항

  • Virtualbox
  • Vagrant
  • VSCode

 

 

vagrant 로 가상머신 두개 생성

- Virtualbox의 호스트 네트워크 ip 대역 확인 : 여기선 192.168.56.x

 

- Vagrantfile 작성

Vagrant.configure("2") do |config|
    #my ubuntu
    config.vm.define "my-ubuntu" do |cfg|
        cfg.vm.box = "ubuntu/bionic64"

        # 시스템 사양에 알맞게 cpu, memory 설정
        cfg.vm.provider "virtualbox" do |vb|
            vb.name = "my-ubuntu"
            vb.cpus = 2
            vb.memory = 2048
        end
        cfg.vm.host_name = "my-ubuntu"

        # virtualbox의 호스트네트워크 ip 대역에 맞게 설정 192.168.56.x
        cfg.vm.network "private_network", ip: "192.168.56.100", :adapter => 2
    end

    #remote ubuntu
    config.vm.define "remote-ubuntu" do |cfg|
        cfg.vm.box = "ubuntu/bionic64"

        # 시스템 사양에 알맞게 cpu, memory 설정
        cfg.vm.provider "virtualbox" do |vb|
            vb.name = "remote-ubuntu"
            vb.cpus = 2
            vb.memory = 2048
        end
        cfg.vm.host_name = "remote-ubuntu"
        # virtualbox의 호스트네트워크 ip 대역에 맞게 설정 192.168.56.x
        cfg.vm.network "private_network", ip: "192.168.56.101", :adapter => 2
    end
end

 

- Vagrant 파일로 실행

ubuntu@ubuntu-All-Series:~/바탕화면/210617$ vagrant up

 

- my-ubuntu  VM 접속

ubuntu@ubuntu-All-Series:~/바탕화면/210617$ vagrant ssh my-ubuntu
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-143-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Jun 17 02:34:19 UTC 2021

  System load:  0.01              Processes:             98
  Usage of /:   2.7% of 38.71GB   Users logged in:       0
  Memory usage: 6%                IP address for enp0s3: 10.0.2.15
  Swap usage:   0%                IP address for enp0s8: 192.168.56.100




New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

 

- my-ubuntu에서 remote-ubuntu로 ssh 접속 시도 : my-ubuntu 192.168.56.100, remote-ubuntu 192.168.56.101

vagrant@my-ubuntu:~$ ssh 192.168.56.101
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
ECDSA key fingerprint is SHA256:O/UCGxwWX3qtY3sxI9JzRx6M04UveLP74/WI3RW4CBA.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.101' (ECDSA) to the list of known hosts.
vagrant@192.168.56.101: Permission denied (publickey).

 

- my-ubuntu에서 ssh key 생성

vagrant@my-ubuntu:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:UdrYhVWenMvaqgj94g88YmJGapmvt2dea2B3f0KpSSY vagrant@my-ubuntu
The key's randomart image is:
+---[RSA 2048]----+
|          .oo..  |
|         *.. o o |
|        + o   =  |
|         .   . . |
|    .   S    .o  |
|   =  ooE + oo   |
|  = +.+o*= =. .  |
| . +.oo+o*o o..  |
|  .oo=.o+o+..o   |
+----[SHA256]-----+

 

- my-ubuntu에 생성된 public key인 id_rsa.pub 내용 출력 및 복사

vagrant@my-ubuntu:~$ cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC902iA44JUJnZ+bNbsTOLh29la4z+loPXormhjNyqzcnJye3xI6xu7rUHSUuXitw/QT5aBzk3XHge7gt4BFHZN8CuMq3GJjQGfgF/LZlcmYBNYfgPK4dxM7TIi0qQDA5EL3RQce++2lMhxjstd9gT9vOsHB1HvPOO3P/3Yl3/XsFC5mQPOSyGWpc51+3Fp3mkKFbzsKmaV855u72OP4N6j98/s1IU3eIM+iJ1IUYmXBp/0oXNZhjrLXmeifPswtHs3+HhO1SdxV0vTc/alXOg6daKSbWTMt+HBMV+/oL/uE65D4d7083cFi06KXuUhDOHx9Dw5yWX3Rmx6U/Qt6lkJ vagrant@my-ubuntu

 

- remote-ubuntu 접속

ubuntu@ubuntu-All-Series:~/바탕화면/210617$ vagrant ssh remote-ubuntu
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-143-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Jun 17 02:46:35 UTC 2021

  System load:  0.0               Processes:             96
  Usage of /:   2.7% of 38.71GB   Users logged in:       0
  Memory usage: 6%                IP address for enp0s3: 10.0.2.15
  Swap usage:   0%                IP address for enp0s8: 192.168.56.101




New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

 

- 복사한 my-ubuntu의 id_rsa.pub 키를 remote-ubuntu의 ~/.ssh/authorized_keys 에 추가. ( >> : 파일에 이어 쓰기)

vagrant@remote-ubuntu:~$ echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC902iA44JUJnZ+bNbsTOLh29la4z+loPXormhjNyqzcnJye3xI6xu7rUHSUuXitw/QT5aBzk3XHge7gt4BFHZN8CuMq3GJjQGfgF/LZlcmYBNYfgPK4dxM7TIi0qQDA5EL3RQce++2lMhxjstd9gT9vOsHB1HvPOO3P/3Yl3/XsFC5mQPOSyGWpc51+3Fp3mkKFbzsKmaV855u72OP4N6j98/s1IU3eIM+iJ1IUYmXBp/0oXNZhjrLXmeifPswtHs3+HhO1SdxV0vTc/alXOg6daKSbWTMt+HBMV+/oL/uE65D4d7083cFi06KXuUhDOHx9Dw5yWX3Rmx6U/Qt6lkJ vagrant@my-ubuntu' >> ~/.ssh/authorized_keys

 

- my-ubuntu에서 다시 remote-ubuntu로 ssh 접속 시도 : 패스워드 입력도 없이 접속되는 것을 확인

vagrant@my-ubuntu:~$ ssh 192.168.56.101
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-143-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Jun 17 02:53:47 UTC 2021

  System load:  0.0               Processes:             97
  Usage of /:   2.7% of 38.71GB   Users logged in:       1
  Memory usage: 6%                IP address for enp0s3: 10.0.2.15
  Swap usage:   0%                IP address for enp0s8: 192.168.56.101




New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Thu Jun 17 02:46:35 2021 from 10.0.2.2
vagrant@remote-ubuntu:~$

 

 

VSCode 에서 Remote SSH extension 설치 및 설정

- 좌측 Extensions 선택 후, remote ssh 검색한 뒤 설치 (단축키 : Ctrl + Shift + X)

 

- 좌측 하단의 '><'표시가 있는 초록색 네모가 생긴것을 확인

 

- 초록색 네모 클릭 -> Open SSH Configuration File... 선택

 

- /home/ubuntu/.ssh/config 선택 : 윈도우는 c:\Users\사용자계정\.ssh\config

 

- config 파일 수정 : /home/ubuntu/.ssh/config

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host remote-ubuntu
    HostName 192.168.56.101
    User vagrant
    IdentityFile /home/ubuntu/.ssh/private_key

IdentityFile이 remote-ubuntu에 접속할 때 사용할 private key 파일을 지정함.

 

Private key를 알맞은 위치에 생성

- my-ubuntu에서 생성한 private key인 id_rsa 키 출력 및 복사

vagrant@remote-ubuntu:~$ exit
logout
Connection to 192.168.56.101 closed.
vagrant@my-ubuntu:~$ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvdNogOOCVCZ2fmzW7Ezi4dvZWuM/paD16K5oYzcqs3Jycnt8
SOsbu61B0lLl4rcP0E+Wgc5N1x4Hu4LeARR2TfArjKtxiY0Bn4Bfy2ZXJmATWH4D
yuHcTO0yItKkAwORC90UHHvvtpTIcY7LXfYE/bzrBw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DmolkCgYANTFyVb5nkPBRaab8PIbU5h6fiKqZtKaw9FA6l
QMVUPQKiq24eblsgqYCxuCwFoE6cgk8rEuLe89w7sOs1zc+8slBZS2wvbPi2LN9A
Wr/g3W4G96dRKoPLyhtj55urIeG9lVpoE2gz7MgrWiwdPGiGgxxDLOA/ZUh1HN9P
4cj0sQKBgEMIUVjNp5/W1oQu2cflhdmXUDchJRUcDkliEd90uC5JZSJiInSHuZ2B
PSrP9HBSWOwB5MWRqDqdJXVy12xWxFV9tczZ4tt/okOz999t5YJLF3Eg/e++E52p
XXeOMRtzg3oE079Z6Tnw7IBtPOfHRn//4AGhg+DL1NCNRVU5Bgtq
-----END RSA PRIVATE KEY-----

 

- /home/ubuntu/.ssh/private_key 파일 생성 후, 복사한 id_rsa의 내용을 붙여넣기

ubuntu@ubuntu-All-Series:~/.ssh$ echo '-----BEGIN RSA PRIVATE KEY-----
> MIIEowIBAAKCAQEAvdNogOOCVCZ2fmzW7Ezi4dvZWuM/paD16K5oYzcqs3Jycnt8
> SOsbu61B0lLl4rcP0E+Wgc5N1x4Hu4LeARR2TfArjKtxiY0Bn4Bfy2ZXJmATWH4D
> yuHcTO0yItKkAwORC90UHHvvtpTIcY7LXfYE/bzrBwdR7zzjtz/92Jd/17BQuZkD
> zkshlqXOdftxad5pChW87CpmlfOebu9jj+Deo/fP7NSFN3iDPoidSFGJlwaf9KFz
> WYY6y15nonz7MLR7N/h4TtUncVdL03P2pVzoOnWikm1kzLfhwTFfv6C/7hOuQ+He
> 9PN3BYtOil7lIQzh8fQ8Ocll90ZselP0LepZCQIDAQABAoIBACqZ63YhmPoxt54f
> 5wwZJUHzCRCwt3QAb6/MlHjPxLGJGc4/kGD9ArhX5dHB8P20f8QMWIbBOg7xGmMm
> t5Qed35+TAfB4jUwhBC7ysPnL9/Y5/6QKWG61qodAYd3Jn/WqRK0hvLtjZq4QVPa
> 7CEmOXCImWZ2ahj63VAe+GVHetV4xU9sUNq2WCPH5fbq8HtQzdTA5IltC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~/.ssh/private_key

 

- 생성한 private key의 권한을 변경

ubuntu@ubuntu-All-Series:~/.ssh$ chmod 0644 ~/.ssh/private_key
.ssh 디렉토리 하위 파일들의 올바른 퍼미션
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts

 

 

Remote SSH 접속

- 초록색 네모 클릭 후, Remote-SSH: Connect to Host... 선택

 

- config에서 설정한 remote-ubuntu 선택

 

- Continue 선택

 

- 좌측의 Open Folder 선택 하면 나타나는 창에서 /home/vagrant/ 그대로 둔 뒤에, OK 선택

 

- 디렉토리 목록 확인 및 터미널 동작까지 확인

Comments