Monday, February 17, 2025

How to set up remote ssh without password from VSCode

 You can set up passwordless SSH access from VSCode by using SSH keys. Here’s how:

  1. Generate an SSH key pair (if you don’t have one):
    Open your terminal and run:

    ssh-keygen -t rsa -b 4096

    Follow the prompts. If you want a completely passwordless experience, leave the passphrase empty.

  2. Copy your public key to the remote server:
    Use the ssh-copy-id command: (you need replace with your own info)

    ssh-copy-id username@remote_server_address

    This adds your public key (typically ~/.ssh/id_rsa.pub) to the ~/.ssh/authorized_keys file on the remote machine.

  3. Verify remote SSH settings:
    Ensure the remote server’s SSH configuration (usually in /etc/ssh/sshd_config) has PubkeyAuthentication yes enabled, and that the ~/.ssh directory and authorized_keys file have the correct permissions.

  4. Configure your SSH client (optional but useful):
    Edit (or create) the ~/.ssh/config  (it is on C:\Users\<YourUsername>\.ssh in windows OS) file on your local machine with an entry like:

    Host my-remote HostName remote_server_address User username IdentityFile ~/.ssh/id_rsa

    This simplifies the connection process.

  5. Connect using VSCode’s Remote - SSH extension:

    • Install the Remote - SSH extension in VSCode if you haven’t already.
    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and type “Remote-SSH: Connect to Host…”.
    • Select the host (either from your SSH config or enter it manually).

VSCode will use your SSH key for authentication, allowing you to log in without entering a password.


FAQ:

Q1) Where to find ~/.ssh in windows OS?

A1) On Windows, the equivalent of the ~/.ssh folder is located in your user profile directory, typically at:    

C:\Users\<YourUsername>\.ssh

Q2) How to solve?

ssh-copy-id username@remote_server_address ssh-copy-id : The term 'ssh-copy-id' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + ssh-copy-id username@remote_server_address + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (ssh-copy-id:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

A2: (replace username and remote_server_address with your own case)
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh username@remote_server_address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

No comments:

Post a Comment