SSH Connections in Visual Studio Code
Part of the Mac series
Recently I needed to work on some script files on a server and wanted to edit them in Visual Studio Code. Visual Studio Code supports remote connections, including ssh connections. Unfortunately, some of these files require root privileges, and I can’t and don’t want to be able to ssh into this server as root.
What about sudo su
once you’re logged in, you say?
The good news about configuring ssh connections is that you can set up RemoteCommand entries in your ssh config that will be executed upon connection. sudo su
is a command. And the good news about Visual Studio Code is that it lets you specify an ssh config file to use for its ssh connections, so you can customize your Visual Studio Code ssh connections to perform a sudo su
upon connection, if needed, while keeping your normal ssh config file free of such silliness so that this doesn’t happen in your normal terminal ssh sessions.1
First create a copy of your ~/.ssh/config file and name it something obvious. Mine is named config_vscode.
Now edit your newly created created config_vscode (or whatever) file and add the following to the end of connections that need root access to files in Visual Studio Code:
This means a given connection that looks like this in ~/.ssh/config:
Should look like this in ~/.ssh/config_vscode:
Then in your Visual Studio Code user settings.json file, make sure to point to your custom ssh config and set enableRemoteCommand to true.
Also, in your Visual Studio Code defaultSettings.json, make sure the following is set to true:
Then make your ssh connection in Visual Studio Code and be amazed as you now have root powers on files that previously refused to submit to your will.
Footnotes
-
This assumes that your login user is in your server’s sudoers list and that sudoers don’t need to enter passwords to perform a
sudo su
. ↩