[Configuration environment] VS Code under Windows remotely connects to the virtual machine Ubuntu

Table of contents

One, the environment

Two, key steps

Three, detailed steps

4. Secret-free remote connection

Five, reference link

Six, extra knowledge


One , the environment

  • Windows 11 Home Chinese Edition
  • VS Code version: 1.80.2
  • VMware® Workstation 16 Pro (Version: 16.1.2 build-17966106)
  • ubuntu-22.04.2-desktop-amd64

Two , key steps

  • Install OpenSSH under Windows
  • VS Code installs the Remote-SSH plugin

Three , detailed steps

  1. Install an SSH server on Ubuntu. Open a terminal and run the following commands in sequence to install OpenSSH server and start the SSH server:
    1. sudo apt update
    2. sudo apt install openssh-server
    3. service sshd restart
  2. Open VS Code on Windows, and install the "Remote - SSH" extension. Search for "Remote - SSH" in the extended view of the sidebar and install it.
  3. Open the command palette in VS Code. You can open the command palette by pressing  (or clicking the " Open Remote WindowCtrl+Shift+P " button in the lower left corner of VS Code ).
  4. In the Command Palette, enter " Remote-SSH: Connect to Host " and select that option.
  5. Select " Configure SSH Hosts " in the pop-up menu .
  6. Select the " ~/.ssh/config " file or create a new configuration file ( multiple Linux server information can be configured in this configuration file ).
  7. In the configuration file, add the following, replacing hostname with your Ubuntu IP address or hostname, and username with your username on Ubuntu:
  8. Return to the command panel, enter " Remote-SSH: Connect to Host " again (or click the " Open Remote Window " button in the lower left corner of VS Code ), and select the connection you just configured.

  9. Enter your Ubuntu user password for authentication, but the following error is encountered here, indicating that the OpenSSH client is not installed in the Windows environment.

  10. Run PowerShell as an administrator to install OpenSSH using PowerShell. To make sure OpenSSH is available, run the following command:
    1. Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
  11. If you don't have an SSH client installed, the above will output the following:
    1. Name : OpenSSH.Client~~~~0.0.1.0
    2. State : NotPresent
  12. Then, execute the following command to install the OpenSSH client
    1. Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
  13. Start and configure the OpenSSH server to start using, and then run the following commands in order to start sshd
    1. Start-Service sshd
    2. Set-Service -Name sshd -StartupType 'Automatic'
    3. if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) { Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 } else { Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists." }
  14. Then click " Remote Explorer " in the left column of the VsCode interface , and in the expanded "Remote Linux Connection Information" list, right-click one of them to make a remote connection, as follows.
  15. After a successful connection, VS Code will open a new window displaying the file system on Ubuntu (you can use "File" > "Open Folder" to browse and open folders in the remote home directory.).
  16. Then you can write, save and run the code in the Ubuntu environment in VS Code.
  17. If you want to end the SSH connection, you can use File > Close Remote Connection to go back to running VS Code locally.

4. Secret - free remote connection

To implement password-free remote login to Linux systems in VSCode, you can use SSH key pairs. Here are the detailed steps:

  1. Generate an SSH key pair: First, you need to generate an SSH key pair on a local computer (such as Windows). Open a terminal or command prompt window (such as PowerShell) and run the following command:
    1. ssh-keygen -t rsa
  2. Follow the prompts to select the save location and password of the key file and other settings. This will generate a private key file ( id_rsa) and a public key file ( id_rsa.pub).
  3. Add the public key file to the Linux server: Upload the public key file to  ~/.ssh/authorized_keys a file on the remote Linux server. Copy the public key file to the server (my public key file is in the local directory  C:/Users/86152/.ssh/id_rsa.pub) with the following command:
    1. scp C:/Users/86152/.ssh/id_rsa.pub username@server_ip:~/
  4. usernameReplace with the username on the Linux server you want to connect to and server_ipreplace with the IP address of the server. This command will copy the public key file to the home directory of the remote server.
  5. Connect to the Linux server using SSH: Use the following command followed by the login password to authenticate:
    1. ssh username@server_ip
  6. Add the public key to authorized_keysthe file: Append the contents of the public key file to ~/.ssh/authorized_keysthe file with the following command (make sure .sshthe directory exists, create it if it doesn't already exist):
    1. cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
  7. Set file permissions: Use the following command to fix file permission issues, this will ensure that .sshdirectories have permissions of 700 (owner has read, write, and execute permissions) and authorized_keysfiles have permissions of 600 (owner has read and write permissions):
    1. chmod 700 ~/.ssh
    2. chmod 600 ~/.ssh/authorized_keys
  8. After completing the above steps, you can connect to the Linux server through VsCode without entering a password.

Five , reference link

1. Install OpenSSH on Windows

2. Remote development via SSH

3. Remote development tips and tricks

Six , extra knowledge

1.SSH (Secure Shell) is a network protocol for remote login and secure file transfer. It provides a means of secure encrypted communication between a client and a remote server over an insecure network connection (for example, the Internet). The following is an introduction to SSH:

  • Security: SSH is an encryption protocol that ensures the security of data transmitted over insecure network connections. It uses public key encryption and symmetric key encryption to protect the confidentiality of communications and prevent data from being eavesdropped and tampered with.
  • Remote login: The most common use of SSH is remote login. Using an SSH client, users can connect to a remote server over the Internet, and execute commands and access resources on the remote server as if they were operating directly on the server.
  • File transfer: SSH provides a secure file transfer mechanism called SCP (Secure Copy) or SFTP (SSH File Transfer Protocol). Using SCP or SFTP, users can securely transfer files between a local computer and a remote server.
  • User Authentication: SSH uses public key cryptography for authentication. When establishing an SSH connection, a public key is exchanged between the client and the server. The client generates a pair of keys (public key and private key) and sends the public key to the server. The server associates the public key with a specific user and uses it to authenticate the user.
  • Ports and configuration: SSH uses the default TCP port 22 for communication. However, the port of the SSH server can be changed if required. Additionally, configuration files for the SSH server reside on the server and can be used to define many different behaviors and restrictions.
  • Client and Server: SSH consists of an SSH client and an SSH server. The client is used to initiate an SSH connection and send commands and requests to the server. The server is responsible for accepting client connections and providing the required services (login, file transfer, etc.).
  • Public Networks: Since SSH provides secure encrypted communication, it is widely used to access remote servers on untrusted public networks. Through SSH, users can safely use remote servers on the Internet without worrying about the safety of sensitive data.

In short, SSH is a secure remote login and file transfer protocol, which provides encrypted communication and authentication mechanisms, enabling users to safely access and operate remote systems on insecure networks.


2. OpenSSH is an open source software suite that implements remote login and secure file transfer functions. It is an implementation of the SSH protocol and provides a set of command-line tools and daemons for secure remote access and file transfer over a network. The following is an introduction to OpenSSH:

  • Function: OpenSSH provides secure remote login and file transfer functions through encrypted communication protocols. It supports various functions of the SSH protocol, including encryption, public key and authentication, remote command execution, file transfer (via SCP and SFTP), and functions such as port forwarding and tunneling.
  • Security: OpenSSH is committed to providing secure communication and data transmission. It uses encryption technology to protect communication content and ensure that data cannot be eavesdropped, tampered with or forged during transmission. These include encrypting communications using symmetric-key algorithms and public-key cryptography, and ensuring the authentication of users using various authentication methods such as passwords, public keys, and certificates.
  • Components: OpenSSH consists of several core components. Among them, the OpenSSH client (ssh) is used to initiate SSH connections and interact with remote hosts; the OpenSSH server (sshd) runs on remote hosts and is responsible for accepting SSH connections, performing user authentication, and providing remote login and file transfer services; There are other auxiliary tools like ssh-keygen for generating and managing key pairs, and ssh-agent for managing proxy authentication with private keys.
  • Operating system support: OpenSSH can run on a variety of operating systems, including various Unix-like systems (such as Linux, BSD, etc.) and versions of Microsoft Windows. OpenSSH can be easily installed and configured on these systems for secure remote login and file transfer.
  • Configuration Files and Customization: OpenSSH provides flexible configuration options that allow users to customize to their specific needs. By modifying OpenSSH configuration files (such as sshd_config), you can define security policies, access control rules, port settings, user configuration, etc. This makes OpenSSH adaptable to different environments and needs.
  • Community Support and Open Source: OpenSSH is an open source software and its source code is publicly available. This allows users and developers to freely review, modify and distribute the code, and contribute improvements. OpenSSH has strong community support and a wide user base, so it can be continuously improved and updated while maintaining security.

Overall, OpenSSH is a powerful open source software suite that provides secure remote login and file transfer functions, and ensures the security of data and user identities through its encrypted communication protocol. It is widely used in various application scenarios such as managing remote systems, performing system maintenance, remote cooperation, backup and file transfer.

Guess you like

Origin blog.csdn.net/weixin_43729127/article/details/131877323