使用VScode查看远程服务器代码

本文档详细介绍了如何使用VSCode通过SSH连接远程服务器查看代码的方法,包括配置步骤及解决Badownerorpermissions错误的方案,特别针对Windows 10系统用户提供了具体的修复指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用VScode查看远程服务器代码

安装VSCode以及配置ssh

参考《Remote-SSH配置–如何配置VSCode远程开发》https://blog.starunity.dev/2019/05/08/remote-ssh-configure/

解决Bad owner or permissions

Bad owner or permissions 即对某个文件没有访问权限,不同系统的解决方式不同。

  • window10系统
  • 官方解决方案: https://code.visualstudio.com/docs/remote/troubleshooting#_local-ssh-file-and-folder-permissions
    如下:

Windows: The specific expected permissions can vary depending on the
exact SSH implementation you are using. We strongly recommend using
the out of box Windows 10 OpenSSH Client. If you are using this
official client, cut-and-paste the following in an administrator
PowerShell window to try to repair your permissions:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

Install-Module -Force OpenSSHUtils -Scope AllUsers

Repair-UserSshConfigPermission ~/.ssh/config
Get-ChildItem ~\.ssh\* -Include "id_rsa","id_dsa" -ErrorAction SilentlyContinue | % {
    Repair-UserKeyPermission -FilePath $_.FullName @psBoundParameters
}

For all other clients, consult your client’s documentation for
what the implementation expects. However, note that not all SSH
clients may work.

  • 官方解决方案的问题
    Install-Module -Force OpenSSHUtils -Scope AllUsers执行时报错
  • 本人的解决方案:卸载openSSH
    管理员模式下打开PowerShell, 运行下面命令卸载OpenSSH
# Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Uninstall the OpenSSH Server
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
### 如何配置 VSCode 连接到远程服务器进行代码调试 #### 安装必要的扩展 为了实现通过 Visual Studio Code (VSCode) 对远程服务器上的代码进行编辑和调试,需先安装 Remote - SSH 扩展。该扩展允许用户直接在本地计算机上使用熟悉的开发环境来访问远端资源[^2]。 ```bash # 在VSCode市场中搜索 "Remote - SSH" 并点击安装按钮完成安装过程。 ``` #### 设置SSH连接信息 接下来,在 `.ssh/config` 文件内定义目标机器的相关参数以便简化后续操作流程: ```plaintext Host myServerAlias HostName your.server.ip.address User usernameOnRemoteMachine IdentityFile ~/.ssh/id_rsa_path_if_needed ``` 上述配置中的 `myServerAlias` 是给定的一个便于记忆的名字;而其他字段则应替换为实际的服务器地址、用户名以及私钥路径(如果适用的话)[^3]。 #### 创建并上传项目至远程主机 利用 SFTP 或者 Git 等方式把待测程序源码传送到指定位置之后,就可以准备开启真正的调试环节了。对于 Python 开发而言,可以考虑预先设置好虚拟环境,并确保所有依赖项都已妥善处理完毕[^5]。 #### 启动远程会话与调试器 回到 VSCode 中,按下 F1 键调出命令面板,输入并执行 `Remote-SSH: Connect to Host...` 来建立新的链接实例。选择之前设定好的别名 (`myServerAlias`) 即可自动加载对应的 SSH 配置[^4]。 一旦成功登录到远程节点后,按照常规方法打开想要调试的工作区或文件夹。此时可以通过左侧活动栏里的“运行和调试”图标进入具体项目的断点调试模式。对于某些特定语言来说,可能还需要额外安装相应的支持组件或是调整 launch.json 的内容以适配不同的应用场景需求[^1]。 例如针对Python脚本添加如下所示 JSON 片段作为启动选项之一: ```json { "version": "0.2.0", "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/path/to/your/project/on/server" } ] } ] } ``` 此配置片段假设已经在远程服务器上启用了监听服务等待客户端接入。记得修改其中涉及的具体路径和其他个性化属性以匹配实际情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值