下文章來源于后端技術學堂 ,作者LemonCoder
今天和大家分享一個遠程開發解決方案,聊一聊我平常是如何用 VS Code 進行遠程開發工作的,以及一步步教你搭建遠程開發環境,擁有比德芙還絲滑的遠程開發體驗。
我們廠里為了最大程度提高工程師生產力,各種研發配套工具非常的齊全,對開發人員每人都有一臺云主機,而且是個人主機哦,申請就有的那種。有了云開發主機在家里或者在公司都可以隨便折騰,加班也更方便了(好像哪里有點不對)。
大家都知道我是從事后臺服務器開發工作的,主力語言是C/C++。我們的服務端程序一般都是跑在 Linux 服務器上面,傳統的開發流程是在 window 或 Mac 的 IDE 環境編寫代碼,然后用 ftp 或 sync同步代碼到開發機上編譯,程序或服務最終發布到測試或生產環境運行。
就我來說,以前是這么干的:在本地 Windows 和 Linux 云開發機之間開啟一個同步程序,本地編輯代碼,實時后臺同步到 Linux 云開發機,然后用 xshell 登錄開發機編譯、調試,這個過程有點不夠優雅,現在有更香的解決方案。
這是一款開源編輯器,卻不止是編輯器。
★
Visual Studio Code(簡稱VS Code)是一個由微軟開發,同時支持Windows 、 Linux和macOS等操作系統且開放源代碼的代碼編輯器,它支持測試,并內置了Git 版本控制功能,同時也具有開發環境功能,例如代碼補全(類似于 IntelliSense)、代碼片段和代碼重構等。該編輯器支持用戶個性化配置,例如改變主題顏色、鍵盤快捷方式等各種屬性和參數,同時還在編輯器中內置了擴展程序管理的功能 - 引用維基百科
”
我最喜歡的是它的插件能力,幾乎想要啥功能都能找到插件支持,應該不用我安利你們都會喜歡的。
在 2019 年的 Stack Overflow 組織的開發者調研中,VS Code被認為是最受開發者歡迎的開發環境,據調查87317名受訪者中有 50.7% 的受訪者聲稱正在使用VS Code。
開發環境排名 - 圖片來源網絡
說了這么多,反正要表達的就是VS Code很厲害就是了。
重點來了,今天的主角功能是下面這個遠程開發擴展插件。
微軟在 PyCon 2019 大會上發布了VS Code Remote ,從 1.35.0 版本正式提供可以在本地編輯遠程開發環境的文件的功能,所以首先確保你的VS Code版本是在這個之上的才能體驗到。
VS Code遠程開發的工作原理,大致是這樣的:
VS Code 遠程開發原理
下面講講如何配置,我的Local OS是 Win10 , Remote OS 是 Linux云主機。
遠程開發本地 VS Code 用 SSH 協議與遠程服務端通信,所以要先配置SSH環境變量,由于Git自帶SSH客戶端程序
SSH程序
如果你還沒裝Git的話,這里要先安裝 Git,所以配置 Git 的 bin目錄到環境變量的 PATH 變量下,這樣VS Code連接的時候就能找到它了。
環境變量
要能連上遠程主機,首先我們需要下載VS Code遠程開發插件,VS Code其實是提供了一個遠程開發插件包,包括:
遠程開發插件
打開軟件的擴展界面,搜索 Remote 開頭的插件,也能看到這三個的不同遠程開發插件,我們這里連接的是云主機,選擇安裝 Remote - SSH 插件安裝即可。
機器配置
SSH配置文件
登錄遠程服務
打開遠程目錄
本地插件和遠程插件
遠程編輯和調試
按照上面的配置步驟,每次連接到遠程服務器,都需要輸入服務器登錄密碼很麻煩,可以配置SSH免密登錄,免去每次輸入密碼的煩惱,具體操作步驟如下:
秘鑰列表
這套遠程開發環境體驗下來,我整體是比較滿意的,最大的好處是不用給電腦裝太多軟件,選擇VS Code是因為需要經常在C++/Python/Go三種IDE之間切換比較麻煩,現在即使是電腦配置低點都沒關系,因為所有的編輯器插件擴展和代碼都在云端,通過 SSH連接操作一個VS Code打遍所有。
而且由于遠程開發插件的存在,不論我在哪里,只要有電腦都能方便的打開云端開發環境,非常的方便,這么好用的工具大幅提升生產力,所以今天來分享給大家。
老規矩。感謝各位的閱讀,文章的目的是分享對知識的理解,技術類文章我都會反復求證以求最大程度保證準確性,若文中出現明顯紕漏也歡迎指出,我們一起在探討中學習。
作為一名使用 win10 的開發者,搭建開發環境往往是一件比較痛苦的事,需要從網上下載各種軟件,在電腦上配置不同軟件運行環境,對軟件進行各種設置,不僅耗時,而且容易出現各種問題。搭建的環境還會存在下面一些問題:
為了解決上面這些問題,我們可以使用 WSL+Docker+Scoop 搭建開發環境,取代通過圖形界面安裝軟件和 VMware 安裝 Linux 環境的傳統方式。
WSL 是適用于 Linux 的 Windows 子系統, 可讓開發人員按原樣運行 GNU/Linux 環境 - 包括大多數命令行工具、實用工具和應用程序 - 且不會產生傳統虛擬機或雙啟動設置開銷。
Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然后發布到任何流行的 Linux 機器或 Windows 機器上。
Scoop 是 Windows 的命令行安裝程序,可以讓我們從命令行以最小的安裝量安裝程序, 它消除了安裝界面和安裝程序造成的路徑污染,它安裝和卸載軟件只需一行命令就可以完成,它還可以隨意切換軟件的版本。
一個完整的開發環境組成:首先使用 WSL 搭建一個 Linux 環境,然后在 Linux 環境里安裝 Docker 應用,使用 Docker 來安裝一些常用軟件,比如數據庫、web 環境等,Scoop 則是在本機上安裝一些開發用到的軟件,比如 jdk、maven、idea 等。WSL 和 Scoop 都是比較容易備份和還原的,這兩個搭建好了,我們利用備份還原可以快速在另一臺機器上搭建同樣的一套環境,下面是搭建環境的具體流程。
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2
WSL 每次啟動后,windows 都會給它分配一個隨機的 IP 地址,這樣是不利于我們訪問 docker 里的服務的,為了解決這個問題,我們可以創建一個腳本文件 wsl2-ip.bat來給 wsl 分配一個固定的 IP,在每次啟動 wsl 后用管理員權限運行這個腳本,文件內容如下:
@echo off
setlocal enabledelayedexpansion
:: set wsl2 ip
wsl -u root ip addr | findstr "192.168.169.2" > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
wsl -d Ubuntu-20.04 -u root ip addr add 192.168.169.2/28 broadcast 192.168.169.15 dev eth0 label eth0:1
echo set wsl ip success: 192.168.169.2
)
:: set windows ip
ipconfig | findstr "192.168.169.1" > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
netsh interface ip add address "vEthernet (WSL)" 192.168.169.1 255.255.255.240
echo set windows ip success: 192.168.169.1
)
pause
其中 192.168.169.2 表示 Linux 的 IP 地址, 192.168.169.1 表示主機的 IP 地址。
有時會遇到 Ubuntu 20.04 LTS 無法連接網絡,這個非常重要,我們可以運行下面命令解決:
sed -i '4d' /etc/resolv.conf
sed -i '3a\nameserver 8.8.8.8' /etc/resolv.conf
sed -i '4a\nameserver 114.114.114.114' /etc/resolv.conf
mv /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list
# 清華源
# 默認注釋了源碼鏡像以提高 apt update 速度,如有需要可自行取消注釋
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 預發布軟件源,不建議啟用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
# 中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
#網易163源
deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
apt update
apt upgrade -y
apt install -y fontconfig daemonize
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
if [ -z "$SYSTEMD_PID" ]; then
sudo /usr/bin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
%sudo ALL=(ALL) NOPASSWD: /usr/bin/nsenter -t [0-9]* -a su - [a-zA-Z0-9]*
source /etc/profile
passwd root
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -i '15c Port 22' /etc/ssh/sshd_config
sed -i '17c ListenAddress 0.0.0.0' /etc/ssh/sshd_config
sed -i '34c PermitRootLogin yes' /etc/ssh/sshd_config
sed -i '58c PasswordAuthentication yes' /etc/ssh/sshd_config
systemctl restart sshd
官方和阿里的源選擇其一,建議使用阿里源,速度快。
apt-get remove docker docker-engine docker.io containerd runc
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io
apt-mark hold docker-ce docker-ce-cli containerd.io
apt-get remove docker docker-engine docker.io containerd runc
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io
apt-mark hold docker-ce docker-ce-cli containerd.io
創建 /etc/docker/daemon.json 文件,并填寫以下內容:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
$env:SCOOP='D:\Softwares\Scoop\LocalApps'
[Environment]::SetEnvironmentVariable('SCOOP', $env:SCOOP, 'User')
$env:SCOOP_GLOBAL='D:\Softwares\Scoop\GlobalApps'
[Environment]::SetEnvironmentVariable('SCOOP_GLOBAL', $env:SCOOP_GLOBAL, 'Machine')
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
scoop install sudo
sudo scoop install 7zip git innounp dark -g
scoop checkup
Add-MpPreference -ExclusionPath 'D:\Softwares\Scoop\LocalApps'
Add-MpPreference -ExclusionPath 'D:\Softwares\Scoop\GlobalApps'
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1
scoop bucket add extras
scoop bucket add versions
scoop bucket add nightlies
scoop bucket add nirsoft
scoop bucket add php
scoop bucket add nerd-fonts
scoop bucket add nonportable
scoop bucket add java
scoop bucket add games
scoop bucket add jetbrains
scoop bucket add dorado https://github.com/chawyehsu/dorado
scoop bucket add Ash258 https://github.com/Ash258/Scoop-Ash258.git
scoop bucket add pleiades https://github.com/jfut/scoop-pleiades.git
scoop bucket add Scoop-Apps https://github.com/ACooper81/scoop-apps
scoop bucket add scoop-zapps https://github.com/kkzzhizhou/scoop-zapps
scoop bucket add lemon https://github.com/hoilc/scoop-lemon
scoop bucket add raresoft https://github.com/L-Trump/scoop-raresoft
如果軟件倉庫添加失敗,還是因為網絡的原因,大家可以把這些倉庫 fork 到自己的碼云賬戶下,然后再添加。
scoop install aria2 -g
scoop config aria2-max-connection-per-server 16
scoop config aria2-split 16
scoop config aria2-min-split-size 1M
如果想關閉 aria2,可以運行 scoop config aria2-enabled false命令。
# 搜索app
scoop search <應用名>
# 檢查哪些軟件有更新
scoop status
# 版本切換
scoop reset <應用名>@<版本切換>
# 禁止某app更新
scoop hold <應用名>
# 更新所有app
scoop update *
# 安裝app
scoop install <應用名>
#全局安裝app
scoop install -g <應用名>
#安裝extras軟件源下的app
scoop install extras/<應用名>
# 卸載app
scoop uninstall <應用名>
# 卸載全局安裝的app
scoop uninstall -g <應用名>
開發環境軟件的安裝與配置到這里就完成了,利用上面這些軟件,你可以快速搭建 Java、Go、C++、Python等開發環境。
[1] Linux內核更新包: https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
[2] Microsoft Store: https://aka.ms/wslstore
[3] Ubuntu 20.04 LTS: https://www.microsoft.com/store/apps/9n6svws3rx71
[4] IP地址查詢網站: https://www.ipaddress.com/