Synology & VS Code
Today we are having a fabulous post about easy editing files on Synology. Quite often, you will run into a situation where you have to edit files on your Synology. For example, configuration files or docker-compose files for your containerized applications. Doing this with SSH and the vi editor can be quite draining. It would be nice if we could use a fancy editor from your computer to edit files on our Synology. So this post will be about how to edit with Visual Studio Code (VS Code) on Synology. So let’s get started.
Let me explain what we are going to achieve with this post. We want to run VS Code on our client computer, connect with VS Code to our Synology over SSH and be able to edit files.
What is Visual Studio Code?
Visual Studio Code is a cross-platform, open-source, lightweight editor which is extremely powerful because it can be extended with features through its plugin marketplace. All are free of charge. Read more about it here.
Prerequisites
So what is that we need? Well, first we should be able to connect to our NAS with SSH. Please read my post here on how to do this. For this guide, I do recommend also configuring key-based login with easy access. You can read how to set that up here. Please make sure you complete the easy access section where you configure an entry in your local .ssh/config
file to connect to your Synology.
And of course, we need Visual Studio Code. Please download and install it from its official website.
Configure SSH Server
Out-of-the-box a remote connection from VS Code to a Synology will not work. This is due to the fact that we need to activate a certain setting to get it working.
Safeguard sshd_config changes
Always be very careful when editing the /etc/ssh/sshd_config
file, if you make a mistake you can lose access to your Synology over SSH. For this, we are going to set up a safeguard.
We need to change a parameter in the SSH server configuration. As mentioned above making mistakes in such changes we might encounter the situation that we will be unable to login to SSH again. And for some people in the past this has even resulted in re-installing DSM to fix it. However, there is a simple way of working around this risk. Work with a safeguard.
SSH Server Safeguard
I created a safeguard to protect your SSH login if you accidentally make mistakes when editing the SSH server configuration. The solution to our problem is quite simple. The command I devised as our safeguard is a background process that will execute a command after a specific time.
When started, this will give us a PID (Process Identifier), which is the ID of a process. Then we will edit our SSH server configuration. And if we are successful, we simply re-login after our changes and stop the background safeguard process we started.
What if we fail, and after the changes, we can no longer log in? Well, that’s the cool trick of this safeguard. We are going to do nothing and get a cup of coffee or thee. Then after the time-out we configured, the safeguard will run; it will restore the SSH server configuration with the original configuration of DSM and restart the SSH Server. So the worst that can happen is that we lose all of our previous successful made configurations, and we have to start over. Take it from me on this one, starting over with your SSH server configuration is a far better scenario than re-installing DSM.
This does however, mean that when editing our SSH server configuration, we are always on the clock. But that’s still a better scenario than losing access.
SSH Server Safeguard Command
The safeguard command must be run under the user root
For the command, the time-out is defined in seconds
, 300 = 5 minutes, 600 = 10 minutes. Please make sure you define a time-out that will give you enough time. I recommend using 10 minutes.
( sleep 600; cp /etc.defaults/ssh/sshd_config /etc/ssh/sshd_config; systemctl restart sshd ) &
Please note that the &
at the end, and the parenthesis have a purpose in this command. This will copy the original sshd_config from the DSM backup to the active location of the SSH server configuration, and then it will restart the SSH server to make sure we can log in again.
When is command is executed it will return the following.
[1] 18367
[1]
a single process was sent to the background and 18367
is the PID
(Process Identifier).
Write down the PID
number we need this later to be able to stop our safeguard if everything went successfully.
Configure TCP Forwarding
So what do we need to use Visual Studio Code in combination with Synology? We need to enable AllowTcpForwarding
. By default, this setting is set to no
and we simply need to change this to yes
. Please note that the SSH server configuration is case-sensitive. So the key AllowTcpForwarding
has capitals in it and its values no
, yes
are lowercase.
There are two ways we can configure this. Either for everybody or only for specific users. The choice is yours, I will provide both configurations.
The configuration of the SSH server is located in this the following file which can only be edited by root
: /etc/ssh/sshd_config
- Login to SSH
- Switch to the
root
withsudo -i
- Start the safeguard
- Write down
PID
number - Update
sshd_config
and close to the bottom, you will findAllowTcpForwarding
.
Change this toAllowTcpForwarding yes
. Do not edit all the way at the bottom of the section that hasMatch User
. That is for individual user settings.
AllowTcpForwarding yes
- Save the file
- Restart the SSH Server with the command:
systemctl restart sshd
- If you do not lose your connection, exit your connection and re-login to SSH
- Login to SSH
- Switch to the
root
withsudo -i
- Start the safeguard
- Write down
PID
number - At the bottom of the
sshd_config
add the following section. ReplaceUSERNAME
with the username, you want to apply the setting for. Use<TAB>
for the indent.
Match User USERNAME
AllowTcpForwarding yes
- Save the file
- Restart the SSH Server with the command:
systemctl restart sshd
- If you do not lose your connection, exit your connection and re-login to SSH
Success
Everything went successful, we have now re-logged in. Kill the safeguard with the following command, replace PID
with the number you wrote down earlier.
kill -9 PID
Failure
We made a mistake, and now we need to get some coffee, if you used the unmodified version of the safeguard command then you have to wait about 10 minutes. So get some coffee, and start over in about 15 minutes from now.
Connect with Visual Studio Code
We are almost ready to connect to our Synology with Visual Studio Code. So what do we need? Well, the final step is very simple. We need to install the office Visual Studio Code extension that gives VS Code the ability to connect to remote machines.
Install the ‘Remote SSH’ extension
We need to install the Remote SSH
extension by Microsoft to give Visual Studio Code the ability to connect over SSH.
- Open the extension panel
- Search for
Remote SSH
by the publisherMicrosoft
- Click Install
- Also, I advise installing the
Remote Explorer
extension by the publisherMicrosoft
. This extension will add a nice menu for browsing your remote connections.
Connect to Synology
There are 2 ways to connect to a remote host with Visual Studio Code. Either through the Remote Explorer
or through the Open a Remote Window
(This is the green icon at the bottom left corner). In you have set up easy access with my previous post here, about an entry in your local .ssh/config
then you can connect. Else you have to set up an entry first.
- Click on the green icon at the bottom left of Visual Studio Code.
- A popup will be shown at the top, click
Connect to Host... [remote-ssh]
- Now select your Synology from the list or scroll down and select
+ Add New SSH Host...
- When you have added or selected your Host, a new Window will open, and it will start connecting.
The first time it might give a warning that the remote host may not meet VS Code Server’s prerequisites forglibc
andlibstd++
, but you can ignore this.
You might are presented with some questions, like selecting your type of Host, do you want to accept this fingerprint, etc. - After a successful connection, at the bottom left in the green bar, it should now say.
SSH: mynas
- Open the File Explorer, Click Open Folder, and provide the path of the folder you want to open.
- Open the
Remote Explorer
in the panel - If your host is not present already, you can add it by hovering over
SSH
, and to the right, you will find a+
to add a new host. You will have to provide the entire SSH command. - When you right-click on a Host you can select either open in the current, or new window, the choice is yours.
Happy Editing
Final thoughts
I hope this guide will help you set up an easy way to edit your files. Please leave a comment or send me an email.