When developing with VSCode, if you frequently connect to a remote server via SSH, entering the password each time can be tedious. Fortunately, there are several ways to simplify this process. One common method is to use SSH key pairs for authentication instead of manually entering the password each time. After this setup, authentication occurs automatically upon each connection.
Step 1: Generate SSH Key Pair
First, generate an SSH key pair (a public key and a private key) on your local machine. You can generate the key pair using the following command:
bashssh-keygen -t rsa -b 4096
This command will prompt you for the location to save the key (default is ~/.ssh/id_rsa) and whether to set a password for the key (leave it blank to avoid entering it each time).
Step 2: Add the Public Key to the Remote Server
After generating the key pair, you need to add the public key (the .pub file) to the ~/.ssh/authorized_keys file on the remote server. You can use the ssh-copy-id command to do this:
bashssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_server_address
This command will prompt you to enter the password for the remote server once to copy the public key to the server.
Step 3: Use SSH Keys in VSCode
Installing and using the Remote - SSH extension in VSCode allows you to connect to a remote server via SSH. This extension automatically authenticates using your SSH private key; if you have set up the key as described, you no longer need to enter the password when connecting.
You can set up SSH connections in VSCode by following these steps:
- Install the Remote - SSH extension.
- Open the command palette (Ctrl+Shift+P or Cmd+Shift+P).
- Enter and select the “Remote-SSH: Connect to Host...” command.
- Enter or select the configured remote server.
Thus, each time you connect to the server using VSCode, it will authenticate using the pre-configured SSH key, without requiring manual password entry.
Summary
By using SSH key pairs, you not only avoid repeatedly entering the password when using VSCode, but this method is also more secure than plain password authentication. In fact, this is a recommended method to improve development efficiency and security. We hope this method helps you work more efficiently with VSCode for remote development!