VSCode中Open SSH会话文件是否存在本地副本?AWS EC2远程SSH会话崩溃后未提交修改的文件能否在本地找回?
Hey Sean, sorry to hear you ran into this frustrating situation—let’s break down what’s going on and your available options clearly:
First, How VSCode Remote SSH Handles Your Changes
By default, when you use VSCode Remote SSH to connect to an EC2 instance, all your uncommitted edits live directly on the remote EC2 server, not your local machine. VSCode acts like a "thin client" here: it streams file content to your local screen for editing, but saves every change back to the remote filesystem immediately (offline mode for SSH connections is rare and not enabled by default).
The file names/icons you saw in VSCode after the crash were just cached metadata on your local machine—not the actual edited content. Once the session was cleared, that local cache gets wiped, so you can’t recover the changes from your computer directly.
Your Best Path: Regain Access to the EC2 Instance
Since your changes are stored on the EC2 server, your top priority should be getting back into it. Here are actionable steps to try:
- Check EC2 Instance Status: Head to the AWS Console’s EC2 dashboard. Is your instance still running? If it was stopped, start it back up. If it was terminated, skip to the next section.
- Validate Network & Security Settings:
- Confirm your EC2’s public IP/DNS hasn’t changed (if you’re using a non-static IP).
- Double-check the instance’s security group to ensure inbound SSH (port 22) is allowed from your local IP address.
- Troubleshoot SSH Connectivity:
- Try connecting via a regular terminal (not VSCode) with this command:
ssh -i /path/to/your/key.pem ec2-user@<ec2-public-ip>—this will give you specific error messages (like permission denied or connection timeout) to diagnose the issue. - If SSH is unresponsive but the instance is running, use AWS Systems Manager (SSM) Session Manager to connect (this works if your instance has the SSM agent installed and proper IAM permissions).
- Try connecting via a regular terminal (not VSCode) with this command:
- Recover via EBS Volume: If all connection methods fail, detach the EC2’s EBS volume, attach it to another running EC2 instance, and mount it to access the filesystem directly.
If the EC2 Instance Was Terminated
If your instance was accidentally terminated:
- Check for EBS snapshots of the instance’s root volume (AWS sometimes takes automatic snapshots, but this isn’t guaranteed). If you have a snapshot, you can create a new EBS volume from it, attach it to another instance, and retrieve your files.
- If there are no snapshots, unfortunately, your uncommitted changes are almost certainly lost—terminated instances’ volumes are deleted by default unless you changed the retention setting when launching the instance.
Prevent This Headache In The Future
To avoid similar issues down the line:
- Commit changes frequently to a Git repo (even a private one) to have a backup outside of EC2.
- Enable automatic EBS snapshots for your EC2 instances.
- Install the Local History extension on the remote VSCode setup—it saves local versions of files even if you don’t commit them to Git.
内容的提问来源于stack exchange,提问作者Sean Miller

