How to edit code in VS code on private server - visual-studio-code

I want to edit code on server over VS code ssh. With public access server it works fine. I just connect to IP over ssh with my key by VS Code ssh extension. But now I want to edit code on private server that can be accessed over public one.
Over terminal connection process look like:
> ssh -i key user#10.445.322.12
connected to public server
> ssh -i key user#172.43.65.11
connected to private server
So how I can achieve that? This two servers hosted on EC2 Amazon.

You can use SSH's feature ProxyJump to achieve this.
Press Ctrl+Shift+P and run command Remote-SSH: Open SSH Configuration File:
Insert both hosts and add the ProxyJump directive to your private server:
Host PublicServer
HostName 10.445.322.12
User user
Host PrivateServer
HostName 172.43.65.11
User user
ProxyJump PublicServer
Afterwards you should be able to connect to PrivateServer directly from VS Code.

Related

Connect to Host with VSCode using the private key

I want to connect to a remote server, I know the username and hostname, and I also have the private_key.pem file. How can I connect to this virtual machine using VSCode?
I get this error with the solutions on the internet, and that's reasonable because I have not imported this private_key.pem file anywhere, and I do not know how.
Permission denied (publickey)
Thanks for your help.
I use windows, but the remote machine is ubuntu.
Thanks for asking the question.
You can add private key in ssh Open Configuration File of visual studios.
Please follow the below steps:
Open Vscode
Press F1 and search for Remote-SSH: Open Configuration File
Edit the file for adding new server and private
Host name-of-ssh-host-here
User your-user-name-on-host
HostName host-fqdn-or-ip-goes-here
IdentityFile C:\path\to\my\id_rsa.
IdentityFile specify the path of private file.For windows use double slash.
Please find the reference at https://code.visualstudio.com/docs/remote/ssh#_getting-started

VS Code ask for password repeatedly when opening different folder on same host

I have connected to a remote Ubuntu host from Windows using VS Code and using it for remote development. Often times I open different code repositories in VS Code but every time I have to open a different folder despite having the connection established the VS Code ask for password.
It seems that once we are commented to a remote host then successive opening of different folder from same host should not prompt for password.
Is there any setting I am missing or should do to resolve this or save password.
I'm assuming you're connecting to an ssh remote.
There are two ways to authenticate an ssh connection, via password and via public/private key. When using the latter you don't need to enter the password each time.
To use the public/private keys here's what you have to do:
You first need a pair (public/private) of ssh keys. On windows you can use ssh-keygen to generate them for you and put them in the default ssh config folder ( ~/.ssh/)
You then have to configure the remote server to allow your ssh key, you can do this in two ways:
with the ssh-copy-id command if available (I think on windows it's not there, but you can try)
by manually add your public key (~/.ssh/id_rsa.pub) to the.ssh/authorized_keys file on the host machine
Here's a link to know more about passwordless logins via ssh: https://www.redhat.com/sysadmin/passwordless-ssh
Open git bash on Windows
cd .ssh
ssh-copy-id -i id_ed25519.pub your-username#your-server

How to save ssh password to vscode?

I am using vscode to connect to a remote host. I use Remote-SSH (ms-vscode-remote.remote-ssh) extension to do so. Every time I want to connect to the remote host, I need to enter the password.
Is there a way to save the ssh password to vscode?
To setup password-less authentication for ssh on Visual Studio Code, perform the following steps.
These examples assume the following (replace with your actual details)
Host: myhost
Local User: localuser
Remote User: remoteuser
Remote User Home Dir: remoteuserhome
SSH Port: 22
I'm using a Mac so Windows will be a bit different but the basics are the same
Tell VS Code and your machine in general how you will be connecting to myhost
Edit:
/Users/<localuser>/.ssh/config
Add:
Host <myhost>
HostName <myhost>
User <remoteuser>
Port 22
PreferredAuthentications publickey
IdentityFile "/Users/<localuser>/.ssh/<myhost>_rsa"
Next generate a public and a private key with something like OpenSSL
ssh-keygen -q -b 2048 -P "" -f /Users/<localuser>/.ssh/keys/<myhost>_rsa -t rsa
This should make two files:
<myhost>_rsa (private key)
<myhost>_rsa.pub (public key)
The private key (<myhost>_rsa) can stay in the local .ssh folder
The public key (<myhost>_rsa.pub) needs to be copied to the server (<myhost>)
I did it with FTP but you can do it however you wish but it needs to end up in a similar directory on the server.
ON THE SERVER
There is a file on the server which has a list of public keys inside it.
<remoteuserhome>/.ssh/authorized_keys
If it exists already, you need to add the contents of <myhost>_rsa.pub to the end of the file.
If it does not exist you can use the <myhost>_rsa.pub and rename it to authorized_keys with permissions of 600.
If everything goes according to plan you should now be able to go into terminal and type
ssh <remoteuser>#<myhost>
and you should be in without a password. The same will now apply in Visual Studio Code.
Let's answer the OP's question first:
How to 'save ssh password'?
Since there is no such thing as "ssh password", the answer to "how to save the remote user password" is:
This is not supported by VSCode.
VSCode proposes to setup an SSH Agent in order to cache the passphrase (in case you are using an encrypted key)
But if the public key was not properly registered to the remote account ~/.ssh/authorized_key, SSH daemon will default to the remote user credentials (username/password).
It is called PasswordAuthentication, often the remote user password.
And caching that password is not supported for SSH sessions.
It is only supported by a Git credential helper, when using HTTPS URLs.
(it defers to the OS underlying credential manager)
But I don't know of a remote user password cache when SSH is used.
As Chagai Friedlander comments, the answer to the original question is therefore:
No, but you can use SSH keys and that is better.
Speaking of SSH keys:
"ssh password": Assuming you are referring to a ssh passphrase, meaning you have created an encrypted private key, then "saving the ssh password" would mean caching that passphrase in order to avoid entering it every time you want to access the remote host.
Check first if you can setup the ssh-agent, in order to cache the passphrase protecting your private key.
See "VSCode: Setting up the SSH Agent"
This assumes you are using an SSH key, as described in "VSCode: Connect to a remote host", and you are not using directly the remote user password.
Using an SSH key means its public key would have been registered to the remote account ~/.ssh/authorized_keys file.
This section is the workaround the OP ended up accepting: registering the public key on the remote user account, and caching the local private key passphrase worked.
For those trying to connect through Vscode Remote SSH Extension steps provided at https://code.visualstudio.com/docs/remote/troubleshooting#_ssh-tips)
For Windows(Host) --> Linux(Remote)
Create an SSH .pub key in your windows ssh-keygen -t rsa -b 4096
Copy the contents of the .pub key (default path C:\Users\username/.ssh/id_rsa.pub)
SSH into Remote machine and append the contents of the pub key in authorized keys echo "pub-key" >> ~/.ssh/authorized_keys

VSCode remote developement using ssh with passphrase protected ssh-key

I have a remote ssh server on which I want to do remote developement.
My public key is added to the authorized keys on the server and my private key is passphrase protected.
I added the remote host as described here using ssh user#host -i ~/.ssh/key
But whenever I try to open a new VSCode window on the remote host it fails and I see in the log that the connection timed out, which is expected because I don't get asked for my private-key passphrase.
I read this post on remote development using ssh and I searched on google but couldn't find any hints to what I could do to enable/enter ssh-key passphrase while connecting to remote host.
I know I can connect to the remote using a non-passphrase-protected key but I'm not asking for that, I want to know if it's possible to connect to a remote host from VSCode using a passphrase protected key.
If your key is not the default one, that means you need a ~/.ssh/config entry in which you specify your SSH connection parameters:
Host myserver
Hostname host
User user
IdentityFile ~/.ssh/key
The Visual Studio Code Remote Development will automatically read the config file, and when you ask for opening an SSH session, you will see "myserver" entry: if you select it, the SSH session opened will use ~/.ssh/key, the right private key.
Then, as long as your ssh-agent is enabled, and you have entered at least once your passphrase (through a manual ssh myserver), VSCode will use the same agent to get the passphrase when it will need it.
This is what solved it for me, your issue could be different.
In VScode, choose command: Remote-SSH: Open SSH Configuration File...
Choose the location that is offered to you (or create a config file without extension to desired location), for example:
C:\Users\[USER_NAME]\.ssh\config. Make sure you have appropriate permissions.
Prepare you configuration file, for example
Host Custom_Name
HostName example.server.com
User your_user_name
IdentityFile C:\Users\[USER_NAME]\Documents\MobaXterm\home\.ssh\id_rsa
(or wherever you have the private key)
Finally add a custom path to your configuration file. This is what I lacked for long time
{
// add custom configuration file path
"remote.SSH.configFile": "C:\\Users\\[USER_NAME]\\.ssh\\config",
// Other settings
// omit comma after *config"* if that is your last setting
}
Now it should ask for passphrase as opposed to password.
P.S. Here is a lot of useful information with the setup, especially with the keys that I have omitted for brevity.

How to set a remote connection to a Vagrant container using "Visual Studio Code Remote - SSH"?

I'm exploring the new set extensions called VSCode Remote Pack and I want to connect to a Vagrant container using the Remote Container extension. Using a Windows 10 OS, how could I do that?
I tried the extension but it requests me to have Docker installed, what I suppose from that is that it only works for Docker containers. But I wonder if somebody have already managed to connect to a Vagrant box.
This are the docs from the extension: https://code.visualstudio.com/docs/remote/containers
VS Code Remote containers currently only support Docker (its implementation executes docker commands). Please open a feature request if you would like to see other tools supported.
As an alternative, you could try using Remote SSH to connect to vagrant containers. That should work but will require some extra container setup
Sorry for updating this so late.
The solution was pretty simple, as #MnZrk commented, what it needs to be done for setting up the connection is the following:
Run vagrant ssh-config > some-file.txt. This will generate a file with the configuration to run using SSH. Here an example of that file:
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile C:/Users/User/project/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
ForwardX11 yes
Notice that the host name is default, you could rename it to whatever you want so you could identify it more easily.
Copy the content of some-file.txt inside your SSH configuration file. This file could be edit directly from vscode by pressing F1 and writing Remote-SSH: Open Configuration File..., then you select the file you use for ssh configuration. After that file opens, just copy the content of some-file.txt there.
Finally, just press again F1 and type Remote-SSH: Connect to Host..., choose the connection with the host name default or the want you wrote in the first step, and that's all.