从零配置:VSCode Remote-SSH+CPolar 告别开发环境地狱
配置 VSCode Remote-SSH
安装 VSCode 并确保已安装 Remote-SSH 扩展。打开命令面板(Ctrl+Shift+P),输入 "Remote-SSH: Connect to Host",选择 "Add New SSH Host"。输入 SSH 连接信息,格式为 user@hostname -p port。保存后选择配置文件路径,通常为默认的 .ssh/config。
生成 SSH 密钥对并上传公钥到远程服务器。本地终端运行 ssh-keygen -t rsa -b 4096,将 ~/.ssh/id_rsa.pub 内容追加到远程服务器的 ~/.ssh/authorized_keys。修改服务器 SSH 配置 /etc/ssh/sshd_config,确保包含以下参数:
PubkeyAuthentication yes
PasswordAuthentication no
安装配置 CPolar 内网穿透
在远程服务器下载并安装 CPolar。根据服务器架构选择对应版本,例如 Linux x86_64 使用:
wget https://www.cpolar.com/static/downloads/cpolar-stable-linux-amd64.tar.gz
tar -zxvf cpolar-stable-linux-amd64.tar.gz
sudo mv cpolar /usr/local/bin/
创建 CPolar 配置文件 ~/.cpolar/cpolar.yml,设置本地 SSH 端口转发:
tunnels:
ssh:
addr: 22
proto: tcp
region: hk
启动 CPolar 服务并设置开机自启:
cpolar service install
cpolar service start
验证远程连接
获取 CPolar 公网地址。运行 cpolar dashboard 或在管理界面查看分配的域名和端口。在本地 VSCode 的 Remote-SSH 配置中使用该地址:
Host remote_dev
HostName xxxxx.cpolar.cn
User your_username
Port 12345
通过 VSCode 连接时选择 "Remote-SSH: Connect to Host" 并选取配置好的主机。首次连接会提示验证服务器指纹,确认后即可建立安全隧道。连接成功后可在 VSCode 左下角看到远程主机名。
优化开发环境
在远程服务器安装必要的开发工具链。例如 Python 环境可安装 miniconda:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
配置 VSCode 远程扩展。连接后安装适合的远程开发扩展,如 Python、Docker 等。通过设置同步功能(需登录 Microsoft/GitHub 账号)可自动同步扩展和配置。
设置端口转发解决 Web 应用访问问题。在 VSCode 远程环境的 "Ports" 面板可添加需要转发的端口,例如将远程 3000 端口映射到本地 3000 端口。
更多推荐
所有评论(0)