大约 1 分钟
(By Deepseek)
在 VS Code 中安装 Remote - SSH 扩展
打开 VS Code,进入 Extensions(Ctrl+Shift+X),搜索 "Remote - SSH" 并安装。Remote - SSH Remote - SSH: Editing Configuration Files Remote Explorer
确保 Ubuntu 虚拟机已开启 SSH 服务
首先记得换源一下
- 在 Ubuntu 终端运行:
sudo apt install openssh-server -y sudo systemctl enable --now ssh
- 检查 SSH 是否运行:如果显示
sudo systemctl status ssh ctrl Z
active (running)
,说明 SSH 服务已启动 [2][3]。
获取 Ubuntu 虚拟机的 IP 地址
- 在 Ubuntu 终端运行:或
ip a
找到类似hostname -I
192.168.x.x
的 IP 地址。
配置 SSH 连接
- 按 Ctrl Shift P,输入 "Remote-SSH: Connect to Host...",选择 "Add New SSH Host"。
- 输入 SSH 命令格式:例如:
ssh username@ubuntu_vm_ip
ssh user@192.168.1.100
- 选择保存到
~/.ssh/config
文件,并输入密码(或配置 SSH 密钥免密登录)。
连接 Ubuntu 虚拟机
- 再次按 F1,选择 "Remote-SSH: Connect to Host...",选择刚才添加的主机。
- 输入 Ubuntu 用户的密码(如果未配置密钥),即可在 VS Code 中远程编辑文件 。
其他:修改主机配置
Change host file.
Host ecs-0001
HostName xxx.xxx.xxx.xx
User root
Port 22
If failed, should login the host to edit /etc/ssh/sshd_config
, setting AllowTcpForwarding yes
. And restart the sshd in host.
sudo systemctl restart sshd
Then re-try the 'Remote - SSH : Connect to Host'.