Syncthing Server

From Wiki
Jump to navigation Jump to search

Syncthing is a tool written in Go to keep files synchronised between computers. It's decentralized, meaning instead of syncing to a server, your computers can find each other on the network and sync amongst themselves. While I don't use it outside of my home network, it can run over the internet as well - it uses relay and discovery severs, and can even traverse NAT.

Prior to Syncthing, I used NextCloud, which runs on a web server. I used this to keep some directories on several computers in my home network synchronized. It worked, and I actually still have an install I point my phone too (although Syncthing has an Android app as well). However, sometimes it would be slow, particularly with a lot of small files. The server also stores all of its files in its own directory, and depends on a database (I have used both MariaDB and SQLite), as opposed to Syncthing which is just its own executable.

One advantage of the central model NextCloud uses is that if you only have one of the computers you're syncing online, it can still keep the copy of the server up to date. While Syncthing can deal with conflicts (by renaming the conflicting files), it's nice to not have to have both machines on for them to sync in all cases. (Although again, Syncthing can sync the two if only the two of them are online; NextCloud would still need to talk to its server.) I had already had a machine running with an external hard drive as kind of a stopgap NAS, so I looked into how to get Syncthing going on this as well.

Syncthing on a Headless Server

Syncthing can run just fine on a headless machine, with a caveat - each user requires a running instance. (This is the case as far as I know, I don't know if a multiuser version is in the works at the moment.) Also, on my laptops and desktop, I start it with my desktop environment, whereas my server doesn't have a GUI installed. I'd like it to start up on boot, but running as my user.

I'm running this on Debian 10 on a single board computer (PC Engines Alix, an i386 board), but it should work on a Raspberry Pi as well. In fact, with a Pi 3 or 4, it would probably run better.

Configuring Syncthing

The first thing to do is to actually configure Syncthing. This is based on the Getting Started page in the docs, which covers things pretty well. However, since this is a headless server, you can use SSH to forward the GUI to your local machine:

ssh -L 7000:localhost:8384 servername

That forwards the GUI, which is running on port 8384 on the server, to port 7000 on your local machine. I chose port 7000 since I was already running Syncthing on my local machine. When you log in just run syncthing at the command line to get it started. Then, in a web browser, just connect to http://localhost:7000 on your machine. Reconfiguring Syncthing to listen to the whole network and not just on localhost is another option, but a little less secure. I recommend setting up a username and password for the GUI, even if you only have it listen on localhost.

If you have multiple users who want to sync on your server, they'll each need their own instance. In that case, change the GUI port each listens on so there are no conflicts.

Starting it for the first time gets the initial configuration going. Before configuring any shared folders I set it up to autostart (next section), otherwise you need to leave Syncthing running in your SSH session. After you start it and confirm you can connect, hit Ctrl C in the terminal to stop the server and move on.

Starting the Server

These instructions are from the Syncthing documentation for autostarting on Linux. Basically, I'm using a systemd unit file to start as a normal user. There should be one with syncthing when you installed it; I found it in /usr/lib/systemd/user/. Copy it to your home directory like this, where user is your username:

cp /usr/lib/systemd/user/syncthing.service /home/user/.config/systemd/user/

Next, as a normal user, run these commands to enable the service on boot and start it immediately:

systemctl enable syncthing@user.service
systemctl start syncthing@user.service

You can confirm that it's running by either using ps</> or using systemctl like this:

systemctl --user status syncthing.service

At this point Syncthing should start as your user on boot. When this is configured, SSH in as above and set up folders to sync. In the case of the NAS, I chose to locate the synced folders on an external hard drive, and not my home directory (which is on a CF card). On my desktop and laptops, the synced folders are all in my home directory.