When using VSCode’s Remote SSH extension, you may run into the issue where the connection stalls.
Symptoms include repeatedly seeing the dialog Disconnected. Attempting to reconnect...
Or being stuck on the notification Setting up SSH Host [...] Initializing VSCode
Why this happens
In my experience the above symptoms happen because VSCode’s remote development plugin is maxing out the RAM on your server causing it to lock up. This typically happen on low-budget servers with low resources.
Often times the only way to resolve the issue when it happens is to restart the server. Then it might work okay for a little while, only to eventually stall out again.
Fix: Enable swap space
The ideal fix is to up the resources on your server. If cost is an issue, the next best thing you can do is enable a swap file, which is an area on a hard drive that your server can temporarily store data to if it runs out of RAM memory.
How you enable swap space may differ on what server software you’re using. As an example, here are instructions for a Ubuntu or Debian server:
First, create the swap file - here we’re allocating 4gb for swap space:
> sudo fallocate -l 4G /swapfile
Next, adjust permissions on the resulting swap file so it isn’t readable by anyone besides root:
> sudo chmod 600 /swapfile
Next, tell the system to set up the swap space:
> sudo mkswap /swapfile
Next, enable the swap space:
> sudo swapon /swapfile
Finally, we want to make it so the server always enables this swap space, even after a reboot. To do this run the following command which will add the swap file to your file systems table (fstab) config file:
> sudo echo '/swapfile none swap sw 0 0' >> /etc/fstab
Confirm it worked: You can confirm your swap file is set up correctly with the following command:
> sudo swapon --show
Expected output:
Filename Type Size Used Priority
/swapfile file 4194300 0 -2
With the above swap space set up, you should have a much smoother experience when doing SSH remote development with VSCode on your server.