I need to code / edit files in a remote server using SSH, and I would like to access it with VSCode.
I'm on Windows 10, using "Git Bash" as integrated terminal in VSCode, which means I can connect to the server using VSCode's terminal.
What I'm missing is a way to open files from the terminal to the editor, and even better - interacting with the files using the explorer.
How can this be done?
Since the May, 2nd 2019 announcement of "Remote Development with VS Code", you now officially have:
Visual Studio Code Remote - SSH
The Remote - SSH extension lets you use any remote machine with a SSH server as your development environment.
Since nearly every desktop and server operating system has a SSH server that can be configured, the extension can greatly simplify development and troubleshooting in a wide variety of situations.
You can:
Develop on the same operating system you deploy to or use larger, faster, or more specialized hardware than your local machine.
Quickly swap between different, remote development environments and safely make updates without worrying about impacting your local machine.
Access an existing development environment from multiple machines or locations.
Debug an application running somewhere else such as a customer site or in the cloud.
Q1 2020: VSCode 1.42 improves support for Windows servers, including automatic OS detection.
Is this what you are looking for?
https://marketplace.visualstudio.com/items?itemName=humy2833.ftp-simple#overview
You need to setup sFtp connection to your server.
Install extensions Code Runner and SSH-FS. Add config into your user setting like this:
"code-runner.runInTerminal":true,
"code-runner.fileDirectoryAsCwd": true,
"code-runner.ignoreSelection": true,
"code-runner.saveFileBeforeRun": true,
"files.eol": "\n",
"sshfs.configs": [
{
"label": "label",
//Must use the root direction "/"
"root": "/",
"host": "host",
"port": port,
"username": "name",
"password": "password"
"name": "name"
}]
Login your server account over ssh by the vscode terminal. Then you can edit and run your code on remote server.
Try disabling it and re-enabling it. It works for me.
I was able to log in via putty and ssh from terminal, so it wasn't that. It's just the configuration gets hung up or something.
All of the previous answers require installing a package from the VS Code Marketplace. Here is a solution, that won't even require you to install VS Code.
The Web Based VS Code Editor: Run VS Code on any machine anywhere and access it in the browser.
All you need to do is, first install code-server using:
$ curl -fsSL https://code-server.dev/install.sh | sh
Start code-server using:
$ code-server
After running this you can log on to https://127.0.0.1:8080 and view the web-based VS code.
To keep the service running in the background, use:
$ sudo systemctl restart code-server#$USER
You might also want to edit the configurations, for e.g., run it on 0.0.0.0:8080 instead of 127.0.0.1:8080 or to change the password - use:
$ nano ~/.config/code-server/config.yaml
I have used nano as my text editor, feel free to use your preferred text editor.
For in-depth setup and configuration guide: Setup Guide
Original GitHub repo for more info: code-server
Related
I use my VS Code remote - SSH extension to jump between a lot my customers servers, and would actually like not install the VS Code server on every server that I use.
Is there a way not to install the VS Code server, when you use the Remote - SSH extension, or is there maybe even another extension I should use instead?
To fix this on your own (without using the script mentioned above), you could simply log into each of the linux. cs servers (using SSH), then the command pkill -u node , which will stop any running vscode-server processes
I fixed it but not using the official remote - SSH, but the extension SSH FS, which is a little more combersome as it can see you'r "normal" ssh config, but i works.
https://marketplace.visualstudio.com/items?itemName=Kelvin.vscode-sshfs
I have VS Code running on macOS connecting to a linux box where I am doing Go development. I am connecting via a user that has sudo privileges on the linux host. The root account is disabled on the remote host.
The application I am writing needs to run with root privileges. Is there a way to set vscode to elevate privileges when debugging the application? Or do I need to enable the root account for software development purposes?
While not the exact same environment, I suppose that the needed underlying functionality of needing root to debug, test, and run your code from VSCode is the same as in this answer to How can I debug Go file in VS Code with root privileges? here.
The gist: VSCode version 1.65.0 has a new experimental launch option "asRoot": "true", use it with "console": "integratedTerminal".
For instance, in your launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Test/dbg pkg as root",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${fileDirname}",
"console": "integratedTerminal",
"asRoot": true,
},
]
}
When launching this configuration by pressing F5, a new debug terminal session opens (or might get reused) and the following command executed, needing sudo (which you mentioned you do have rights to use):
/usr/bin/env GOPATH=/home/foobar/go /usr/bin/sudo /home/foobar/go/bin/dlv dap --check-go-version=false --client-addr=:41945
Here is how I was able to run my tests as root for Python development. This should work for most Visual Studio Code debug environments. First run the debugger and let the Debug Console start up. Stop debugger if needed. Run sudo su and enter password. Now the Debug Console is running as root.
It appears that when using the Restart debugger command, it creates a new console so this cannot be used; you need to stop and than restart.
Start debugging again and you should now be root.
If you close the debug console, you will need to repeat the above steps.
Below is the answer I received from the vs code developer team. This isn’t a feature and it will never be a feature.
In order to debug as a root, the debugger (dlv) must run as a root.
Nether dlv, nor this extension aims to offer privilege escalation
solutions. In remote debugging, dlv on the remote machine is
externally managed, not by this extension, so it's out of scope.
Moreover, this extension just communicates with the dlv on the remote
host with DAP, which is not designed for security. So I am afraid this
extension is not the right layer for privilege escalation. To debug as
a root, as you already know, run dlv on the remote machine as a root
(you may also need to set --only-same-user=false if the remote host is
Linux) but protect access to the dlv server & the remote machine
appropriately using proven security technologies. For local debugging,
this is tracked in #558. But, I want to emphasize that debugging as a
root still needs to be done with great care.
Is it possible in VSCode to edit files within WSL2 of a remote PC. (This is like a combination of Remote-WSL and Remote-SSH.) I can connect to that remote PC via ssh and RDP.
The path \\wsl$\ does not seem to be available in my remote-ssh connection.
PCA - me, local, VSCode
^
|
ssh and/or RDP
|
V
PCB - remote, WSL2
(I currently have Windows OpenSSH set up on the remote PC, with the default CMD shell. I tried setting the shell to Bash but then the remote extension wouldn't install.)
Update Setting PC-B's ssh server shell to bash.exe does solve all my problems. I'm not sure why it didn't work before. See my answer below for more details.
Answering my own question I was certain I'd tried this before and it had not worked, but now that I try it again it works. Perhaps I just needed a reboot of Windows and WSL2.
Enable Windows SSH server on PC-B and set the shell to be bash.exe
# Powershell as Administrator
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType 'Automatic'
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force
If you want authorized_keys-based login to this PC and your account is an Administrator account, you will need to follow the special instructions here: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement (I have heard of having to manually fix the administrator_authorized_keys file permission at some point too. https://stackoverflow.com/a/64868357/600360 )
Using Remote-SSH connect VSCode from PC-A to PC-B (using Windows credentials) and tell VSCode that it is a linux server (because you're connecting to bash.exe within WSL2).
If you're behind a proxy server, add the proxy to your ~/.wgetrc.
Voila.
These steps taken from THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine where you can find more details.
If you want to connect to remote WSL, you probably should set it up to run own sshd instead of relying on hosting Windows to do the tunneling.
If I have got your problem statement right, the flow seems to be outlined in one of VS Code blog posts. Here I'll mention steps I think you will need to get to your target state.
Firstly, disable sshd on remote Windows so it does not occupy port 22. then, install and start sshd inside PCB:
# from PCB command prompt
# something like that, depending on your choice of distro
sudo apt remove openssh-server && sudo apt install openssh-server
# this would again depend on your chosen distro
sudo /etc/init.d/ssh start # after i do this - windows pops up a firewall prompt to allow me create a rule. you might need to add it manually
then you'd need to either enable password logins via ssh or (preferably) generate a key pair and put your public key into /home/your_name/.ssh/authorized_keys on PCB.
Assuming you have OpenSSH for Windows installed (this seems to be the client Remote SSH relies on), on your PCA follow something like so:
# something along these lines on your PCA
PS C:\WINDOWS\system32>ssh-keygen
# note location of .pub file and copy its contents into remote ./ssh/authorized_keys
# add generated private key to ssh-agent service
PS C:\WINDOWS\system32> Start-Service ssh-agent # if this fails - ensure service is installed and enabled
PS C:\WINDOWS\system32> ssh-add path\to\your\private_key # ensure you have dropped all permission except your own user
Watch out for permissions:
ssh keys are considered secret, so neither client nor server would start unless you drop all permissions from your key material. On linux do chmod 600 .ssh/authorized_keys, and for windows ssh-agent follow instructions from this SE answer.
The above may seem a bit daunting but is in fact very standard SSH setup procedure
easy steps
just update your vscode to the latest version
install Remote Development extension pack
allow WSL2 connection in the settings
if my answer helped you upvoted :D
I know it's been a while since the post, but here is a useful answer that uses Remote -- Tunnels extension.
https://stackoverflow.com/a/75389647
It appears that, for now, you need to manually install the CLI in WSL in order for it to work. The downloads can be found here. Since you are on Windows, I would recommend the x64 CLI download. When you extract the tar.gz file, you will get a file named code. I would recommend moving it to your home directory for ease. To open the tunnel, run ./code tunnel (from wherever you put the code file) to force the new CLI. If you use code tunnel it will still use the Windows version.
Source: https://github.com/microsoft/vscode/issues/171196
I am having trouble using the remote-ssh plugin in Visual Studio between machine A and machine B where the key is provided by KeeAgent from a KeePass database on machine A.
Machine A runs on Windows 10 and I have set up the Host (machine B) in Visual Studio Code correctly according to the guide. However, when trying to use the remote feature, I get the following error:
[09:48:32.111] Log Level: 2
[09:48:32.121] remote-ssh#0.51.0
[09:48:32.121] win32 x64
[09:48:32.124] SSH Resolver called for "ssh-remote+XXX.XXX.XXX.XXX", attempt 1
[09:48:32.125] SSH Resolver called for host: XXX.XXX.XXX.XXX
[09:48:32.125] Setting up SSH remote "XXX.XXX.XXX.XXX"
[09:48:32.314] Using commit id "91899dcef7b8110878ea59626991a18c8a6a1b3e" and quality "stable" for server
[09:48:32.317] Install and start server if needed
[09:48:38.141] getPlatformForHost was canceled
[09:48:38.142] Resolver error: Connecting was canceled
[09:48:38.147] ------
where XXX.XXX.XXX.XXX obviously is the correct IP of machine B in my output.
As I mentioned, the SSH key for this machine is provided via KeeAgent and works fine with PuTTy. I am not allowed to store a SSH key in plaintext on machine A, hence the Keepass/KeeAgent solution. I guess this is the Problem, that somehow Visual Studio Code does not work with KeeAgent.
Can I work around this, i.e. by telling VS Code to use plink.exe as SSH client, which works with KeeAgent? Or does VS Code allow the encrypted storage of SSH keys itself, thereby making KeeAgent obsolete? I am new to both VS Code and the remote-ssh plugin, so I'm sorry if my questions have obvious solutions.
Maybe the problem lies elsewhere though. In that case I am thankful for every hint.
Thanks!
Go to Options > KeeAgent, then scroll down to Agent Mode Options and activate Enable agent for Windows OpenSSH (experimental): (default: disabled) Enable an SSH agent socket compatible with the built-in Windows 10 SSH client.
I'm trying to get Remote-SSH working with ssh-agent, from Windows 7 desktop to CentOs 7 server.
I'm using ssh, ssh-add and ssh-agent from the Git for windows package. From the cmd prompt, this is all working fine, I can "ssh-add -l" and see the keys, and I can run "ssh " and it runs with no problems and without asking for password:
C:\Users\gnb>"c:\Program Files\Git\usr\bin\ssh-add.exe" -l
4096 SHA256:zg2IR6OlPwCGP8SzcbriXIQjth5zuDc9rbO6uaNPmcU gnb#VDI028-MEL (RSA)
C:\Users\gnb>ssh vdi ls
Desktop
tsclient
wkspace
C:\Users\gnb>
From within VS-Code, I can't get this to work. Running the exact same ssh-add command from within the VS-Code Terminal does not seem to find the agent:
C:\Users\gnb>"c:\Program Files\Git\usr\bin\ssh-add.exe" -l
Could not open a connection to your authentication agent.
The actual remote functionality in VS-Code more or less works, but keeps stopping to ask for passwords.
What's missing here? Why can the vs-code environment not connect to the ssh-agent?
OK, I've had a few more hours playing around with this and I think I understand what's going on.
It only works from cmd if it is the same cmd window that was used to start the agent. Opening a new cmd window then ssh gets the same msg as starting vs-code from the menu - can't find the agent. It appears that the start-ssh-agent script that comes with git/ssh on Win7 is setting some environment variables or some such that the ssh client needs. This does make sense, Unix ssh-agent acts the same way, but I'm clearly not used to thinking about windows apps in those terms.
It also seems the start-ssg-agent script will set the environment variables to point to an existing agent if one is running, else will create a new agent. So a 2 line batch file
start-ssh-agent
code
will reliably start up VS Code with the ssh-agent. If you name the identity file in the ssh config, vs-code will add they key to the agent when required. Otherwise you need to manually add the key to the agent, or fall back to entering the password all the time.
Suspect the better solution is to be on Win10 and use ssh-agent as a service, which should mean VS-code should find the agent when run from a menu. But I can't test that.