Skip to content

Remote SSH Debugging Setup in Visual Studio Code

kxu edited this page Jan 20, 2022 · 1 revision

Remote SSH/Debugging in Visual Studio Code

Launch a vm (instance) on AWS

  • Launch a ubuntu vm on AWS (default setting)
  • Generate a RSA key pair or reuse the existing key pair
  • Download the private key
  • Suggest moving the key under C:\Users\<username>\.ssh\, otherwise may cause issues when connecting

Install remote support package 'Remote Development'

  • Open Visual Studio Code (VSC) locally
  • Click 'Extensions' on the left navigation menu
  • Search 'Remote Development'
  • Click 'Install'
  • 'Remote Explorer' icon appears after installation

Configure SSH connections

  • Click 'Remote Explorer' icon on the left navigation menu
  • Select 'SSH Targets' under the dropdown menu in the side bar window
  • Click the gear icon (Configure) next to 'SSH TARGETS'
  • In the pop-up bar, select 'C:\Users<username>.ssh\config'
  • The config file appears in the right editor area
  • Fill in the following information
Host <alias>
    HostName <Public IPv4 DNS of the vm on AWS>
    User <username>
    IdentityFile <full dir of the private key>

Connect to the remote vm

  • Click 'Connect to host in New Window' in the side bar window
  • In the new window, select 'Linux' in the pop-up bar, select 'Continue'
  • Click 'New Terminal' under 'Terminal' tab on the menu bar
  • Now you have a terminal which ssh into the remote vm

Install GO packages on the remote vm

  • Run sudo apt-get -y update; sudo apt-get -y install build-essential
  • Run curl -LO https://go.dev/dl/go1.17.6.linux-amd64.tar.gz
  • Run sudo tar -C /usr/local -xvf go1.17.6.linux-amd64.tar.gz
  • Append export PATH=$PATH:/usr/local/go/bin to ~/.profile
  • Run source ~/.profile

Install GO support packages on the remote vm

  • Click 'Extensions' on the left navigation menu
  • Search 'Go'
  • Click 'Go' package
  • Click 'Install in SSH:<remote_vm_alias>'
  • Press F1, search ''Go: Install/Update Tools'
  • Select all packages, click 'OK'

Test code

  • Run mkdir code on remote vm
  • Click 'Open Folder' from 'File' tab in VSC
  • Select the dir /home/ubuntu/code in the search bar, click 'OK'
  • A new VSC window pops up.
  • Creat a new file and put some test code
  • Open a new terminal and run the code

Debugging setup

  • Press F5, click 'create a launch json file'
  • In the 'program' field, change to "${workspaceFolder}/<filename>.go"
  • Set breakpoints in the code, and run.

Debugging as root setup

  • Open a terminal
  • Run sudo ln -s /usr/local/go/bin/go /usr/bin/go
  • Edit launch.json following the below example:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug", // builds and launches, or use `exec` to executes precompiled binary
            "host": "127.0.0.1",
            "port": 2345, //2345 can be any unused port
            "program": "${workspaceFolder}/<filename>.go"
        }
    ]
}
  • Run sudo <go/bin directory>/dlv-dap dap -l='127.0.0.1:2345' --only-same-user=false --check-go-version=false, where 2345 can be any unused port.
  • Set breakpoints in the code, and run.
  • Reference