如何通过远程SSH插件使用Visual Studio代码进行远程开发

介绍 (Introduction)

Visual Studio Code is a popular Integrated Developer Environment (IDE) for developers. Its large selection of plugins, minimal design, and cross-platform support make it a great choice for developers of all levels. This tutorial focuses on using the Remote-SSH plugin to enable remote software development. With this plugin you can edit files on your local workstation, but run development tasks such as program execution, unit tests, or static analysis on a remote server.

Visual Studio Code是开发人员常用的集成开发人员环境(IDE)。 它提供了丰富的插件选择,最小的设计和跨平台支持,是所有级别的开发人员的绝佳选择。 本教程重点介绍使用Remote-SSH插件来启用远程软件开发。 使用此插件,您可以在本地工作站上编辑文件,但可以在远程服务器上运行开发任务,例如程序执行,单元测试或静态分析。

There are many reasons why this may be beneficial to you. For example, you may have a Windows workstation and want to develop on Windows, but your code will eventually run on Linux. You may need more RAM or processing power than your current machine has available, or you want to keep code off of your personal machine due to a company policy, or the desire to keep your workstation prestine.

有许多原因可能会对您有所帮助。 例如,您可能有Windows工作站,想在Windows上进行开发,但是您的代码最终将在Linux上运行。 您可能需要的内存或处理能力超出当前计算机所能提供的内存或处理能力,或者由于公司政策或希望使工作站保持原始状态而希望使代码不与个人计算机接触。

In this tutorial, you’ll enable the Remote-SSH plugin, configure Visual Studio Code to execute code on the remote server, and execute code from your local Visual Studio Code installation on the remote server.

在本教程中,您将启用Remote-SSH插件,将Visual Studio Code配置为在远程服务器上执行代码,并在远程服务器上的本地Visual Studio Code安装中执行代码。

先决条件 (Prerequisites)

In order to follow along with this guide, you’ll need:

为了遵守本指南,您需要:

步骤1 —安装远程SSH插件 (Step 1 — Installing the Remote-SSH Plugin)

The Extensions Marketplace is where you can download supported and third-party extensions for a variety of different tools and programming languages. This is where you will search for the Remote-SSH plugin and install it.

扩展市场中,您可以下载受支持的第三方扩展以及各种不同的工具和编程语言。 您将在此处搜索Remote-SSH插件并进行安装。

On the left-hand side of the IDE there is a vertical row of five icons. The bottom icon, which looks like four squares in a box with the top right square exploding out, is the icon for the Extensions Marketplace:

在IDE的左侧,有五个图标的垂直行。 底部图标(看起来像一个盒子中的四个正方形,右上角的正方形爆炸了)是Extensions Marketplace的图标:

You can also access this section by pressing Ctrl+Shift+X. When you open this page you will see suggested plugins to download and install.

您也可以通过按Ctrl+Shift+X来访问此部分。 当您打开此页面时,您会看到建议的插件来下载和安装。

Once you have the Extensions Marketplace open, type Remote-SSH in the Search Extensions in Marketplace search bar. When you find the plugin, select it and then click the green Install button to install the extension.

打开Extensions Marketplace后, 在Marketplace搜索栏中的搜索扩展中键入Remote-SSH 。 找到插件后,将其选中,然后单击绿色的“ 安装”按钮以安装扩展。

The extension is now installed. Next, you’ll configure the extension so you can connect to your server.

现在已安装扩展。 接下来,您将配置扩展,以便可以连接到服务器。

步骤2 —配置远程SSH插件并连接到服务器 (Step 2 — Configuring the Remote-SSH Plugin and Connecting To Your Server)

Now that you have the plugin installed you can configure it to connect to a server. To do so, you’ll need the following pieces of information:

现在已经安装了插件,您可以对其进行配置以连接到服务器。 为此,您需要以下信息:

  • The server’s IP or hostname.

    服务器的IP或主机名。
  • The username you’ll connect with.

    您将使用的用户名。
  • The private key you’ll use to authenticate your user.

    您将用来认证用户的私钥。

You’ll use this information to create an SSH configuration file that Visual Studio Code can use to SSH to the server to sync files and execute code on your behalf. You will create this configuration using Visual Studio Code.

您将使用此信息来创建SSH配置文件,Visual Studio Code可以使用该配置文件对服务器进行SSH SSH,以同步文件并代表您执行代码。 您将使用Visual Studio Code创建此配置。

Now that you have the Remote-SSH plugin installed, you’ll see a small green box in the bottom left-hand corner of the Visual Studio Code interface. If you hover over the box with your mouse pointer, the popup will say Open a remote window. The button looks like a greater than sign slightly under a less than sign ><, like the one in the following image:

现在,您已经安装了Remote-SSH插件,您将在Visual Studio Code界面的左下角看到一个绿色的小方框。 如果将鼠标指针悬停在框上,弹出窗口将显示“ 打开远程窗口” 。 该按钮看起来像一个大于号,稍小于一个小于号> < ,如下图所示:

Click the button, and a dialog box appears in the top center. Select Remote-SSH: Open Configuration File… from the list:

单击按钮,然后在顶部中心出现一个对话框。 从列表中选择Remote-SSH:打开配置文件…

The next prompt will ask you which configuration file you want to open. If you’re on Windows, you’ll see two locations: one in your personal user directory, and one in the installation location for SSH. You should use the file in your user directory when configuring the server.

下一个提示符将询问您要打开哪个配置文件。 如果您使用的是Windows,则会看到两个位置:一个位于您的个人用户目录中,另一个位于SSH的安装位置中。 配置服务器时,应使用用户目录中的文件。

Select the file and your editor will open the config file. Add the following code to the file to define the connection to your server, replacing the highlighted sections with the information for your server:

选择文件,编辑器将打开config文件。 将以下代码添加到文件中,以定义与服务器的连接,将突出显示的部分替换为服务器信息:

config
配置
Host my_remote_server
    HostName your_server_ip_or_hostname
    User sammy
    IdentityFile /location/of/your/private/key

Here’s how this configuration file works:

此配置文件的工作方式如下:

  • Host: This specifies a name for your host. This lets you use a short name or abbreviation instead of the full IP address or host name when connecting to the server.

    Host :这为您的主机指定一个名称。 这使您在连接到服务器时可以使用短名称或缩写而不是完整IP地址或主机名。

  • HostName: The actual hostname of the server, which is either an IP address or a fully qualified domain name.

    HostName :服务器的实际主机名,可以是IP地址或完全限定的域名。

  • User: The user you want to use to connect with.

    User :您要用来连接的用户。

  • IdentityFile: The path to your SSH private key. On Mac and Linux systems, you’ll find this in your home directory in a hidden .ssh directory, typically called id_rsa. If you are on Windows you will have specified a location to save this file when you created it using putty-gen.

    IdentityFile :SSH私钥的路径。 在Mac和Linux系统上,您会在主目录的隐藏.ssh目录(通常称为id_rsa 。 如果您使用的是Windows,则在使用putty-gen创建文件时,将指定保存该文件的位置。

Specify the appropriate values in your file and save the file.

在文件中指定适当的值并保存文件。

Visual Studio Code is now configured and ready to connect to your server. Click on the green Open a remote window button in the bottom left-hand corner and select Remote-SSH: Connect to Host…

现在已配置Visual Studio Code,并准备连接到服务器。 单击左下角绿色的“ 打开远程窗口”按钮,然后选择“ 远程SSH:连接到主机”。

Once you’ve done this all the availble and configured servers will appear in the dropdown menu. Select the server that you want to connect to from this list.

完成此操作后,所有可用的和配置的服务器将显示在下拉菜单中。 从此列表中选择要连接的服务器。

If this is the first time you have connected to this server from your machine, you’ll likely be prompted with the SSH Fingerprint verification dialog, like the one in the following image:

如果这是您第一次从计算机连接到该服务器,则可能会提示您使用“ SSH指纹验证”对话框,如下图所示:

This is to ensure that you are really connecting to the server you think you are. You can verify this by logging in to your server manually and running ssh-keygen -l -f /etc/ssh/ssh_host_key.pub to view the fingerprint of the server. If this fingerprint is the same as the one being presented to you in Visual Studio Code, then you are indeed connecting to the server you think you are so you can click Continue.

这是为了确保您确实连接到了您认为的服务器。 您可以通过手动登录到服务器并运行ssh-keygen -l -f /etc/ssh/ssh_host_key.pub来查看服务器的指纹来验证这一点。 如果此指纹与Visual Studio Code中提供给您的指纹相同,则说明您确实在连接到您认为是的服务器,因此可以单击继续

Visual Studio Code defaults to opening a new window when a new connection is made. A new window will appear with the welcome screen. You’ll know that your connection was successful if you see SSH: your_ip_address_or_hostname in the green box in the bottom left-hand corner. This means that Visual Studio Code is connected and communicating with your remote server.

建立新连接时,Visual Studio Code默认为打开新窗口。 将出现一个带有欢迎屏幕的新窗口。 如果在SSH: your_ip_address_or_hostname的绿色框中看到SSH: your_ip_address_or_hostname ,您将知道连接成功。 这意味着Visual Studio Code已连接并与远程服务器通信。

Now that you’re connected, you can run commands and code from your editor.

现在已经建立连接,您可以从编辑器中运行命令和代码。

步骤3 —在远程服务器上执行代码 (Step 3 — Executing Code on the Remote Server)

The Remote-SSH plugin is configured, and it’s time to run some code on your remote machine. Open a terminal window by selecting Terminal from the navigation bar at the top of the Visual Studio window and clicking New Terminal. You can also open a terminal by pressing CTRL+Shift+`. The terminal that is opened is a terminal on your remote server, not one on your local machine.

配置了Remote-SSH插件,是时候在远程计算机上运行一些代码了。 通过从Visual Studio窗口顶部的导航栏中选择“ 终端” ,然后单击“ 新建终端”,打开一个终端窗口。 您也可以通过按CTRL+Shift+`打开终端。 打开的终端是远程服务器上的终端,而不是本地计算机上的终端。

When the terminal opens, issue the following command to view the IP address of your server to verify that you are connected to your remote server:

当终端打开时,发出以下命令以查看服务器的IP地址,以确认您已连接到远程服务器:

  • ip addr

    IP地址

You’ll see the following output in your terminal:

您将在终端中看到以下输出:


   
   
Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 16:cb:05:5b:30:f1 brd ff:ff:ff:ff:ff:ff inet your_server_ip brd your_broadcast_address scope global eth0 valid_lft forever preferred_lft forever ...

To test out the ability to run remote code, create a new Python file called hello.py in your editor. When you are connected to your remote server, all files created through Visual Studio Code will be saved to that server, not on your local machine.

为了测试运行远程代码的能力, hello.py在编辑器中创建一个名为hello.py的新Python文件。 当您连接到远程服务器时,通过Visual Studio Code创建的所有文件都将保存到该服务器上,而不是保存在本地计算机上。

Add the following contents to the file:

将以下内容添加到文件中:

hello.py
你好
print("Hello Sammy!")

To run this program on your server, open a terminal in Visual Studio Code from the navigation menu or by pressing the key sequence CTRL+Shift+`. Since this terminal session is connected to your remote server, run the following command in the terminal to execute your hello.py program:

要在服务器上运行该程序,请从导航菜单中或通过按CTRL+Shift+`键序列在Visual Studio Code中打开终端。 由于此终端会话已连接到远程服务器,因此请在终端中运行以下命令以执行hello.py程序:

  • python3 hello.py

    python3 hello.py

Your program’s output will be displayed.

程序的输出将显示。

You can also execute the file from the Debug context menu by selecting Run without Debugging.

您也可以通过选择“不调试运行”来从“ 调试”上下文菜单执行文件。

Note: If you have any development extensions installed in Visual Studio Code, like the Python extension, you will have to reinstall these extensions on your server through the Extension Marketplace. If you have previously installed these plugins in Visual Studio Code, when you search for them again, the Marketplace will say Install on SSH: hostname. Always pay attention to what devlopment context you are in, because this is where Visual Studio Code will install your plugins and create your files. If you try to run your code without these plugins installed, error dialog boxes will appear in the bottom right-hand corner of the screen prompting you to install them on your remote server. After you have installed these they will likely require you to reload Visual Studio Code. When you relaunch it, it will continue working on the remote server without you having to manually reconnect.

注意 :如果您在Visual Studio Code中安装了任何开发扩展(如Python扩展),则必须通过扩展市场在服务器上重新安装这些扩展。 如果您先前已在Visual Studio Code中安装了这些插件,则再次搜索它们时,Marketplace将显示Install on SSH:hostname 。 始终注意您所处的开发上下文,因为这是Visual Studio Code将在其中安装插件并创建文件的地方。 如果尝试在未安装这些插件的情况下运行代码,则错误对话框将出现在屏幕的右下角,提示您将其安装在远程服务器上。 安装这些文件后,它们可能会要求您重新加载Visual Studio代码。 重新启动时,它将在远程服务器上继续工作,而无需手动重新连接。

结论 (Conclusion)

You now have Visual Studio Code configured for development on a remote server using SSH. Remote execution with an IDE provides many benefits, including the ability to quickly test how your code runs on different operating systems and different hardware specifications. As long as you have an internet connection you could connect to your server and work on your code from any computer, and you’ll be able to develop using a Linux environment even if you run Windows as your primary operating system.

现在,您已经为使用SSH在远程服务器上进行开发配置了Visual Studio Code。 使用IDE进行远程执行具有许多好处,包括能够快速测试代码如何在不同的操作系统和不同的硬件规格上运行。 只要您具有Internet连接,就可以连接到服务器并可以从任何计算机上处​​理代码,即使您将Windows作为主要操作系统,也可以使用Linux环境进行开发。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-use-visual-studio-code-for-remote-development-via-the-remote-ssh-plugin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值