How to keep files and directories synchronized across different devices using syncthing on Linux

Syncthing is defined as a continuous file synchronization program: it can be used to keep files and directories synchronized across different devices or “nodes”. The application uses TLS as encryption method, and it is, together with its protocol, free and open source software. When using Syncthing, our data remains on our device, and is transferred directly to the destination without relaying on a central server (peer to peer). In this tutorial we will see how to install, configure and use Syncthing on Linux.

In this tutorial you will learn:

  • How to install Syncthing on the most used Linux distributions
  • How to setup the firewall for Syncthing to work correctly
  • How to share and keep a directory synchronized across two devices
  • How to austostart the Syncthing daemon automatically on user login
syncthing-logo

Software requirements

CategoryRequirements, Conventions or Software Version Used
SystemDistribution independent
SoftwareSyncthing
OtherRoot permissions
Conventions# – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installation



There are basically two methods we can use to install Syncthing in our favorite Linux distribution: we can use the version packaged in the native repositories, or we can download a tarball from the official Syncthing website. Additionally, only if using Debian or Ubuntu, we can add the official Syncthing repository as an additional software source and install the package from it.

Installing the native version

Syncthing is available in the native repositories of the most used Linux distributions, such as Fedora, Debian/Ubuntu, and Archlinux, therefore we can easily install it with our favorite package manager. This is always the recommended way to obtain software; the only problem is that, especially on distributions like Debian “stable”, the repository can contain outdated versions of the packages. To install Syncthing on Fedora, we can use the dnf package manager, and issue the following command:

$ sudo dnf install syncthing

On Debian and its many derivatives, instead, we can run:

$ sudo apt install syncthing

On Archlinux we can use pacman to install the syncthing package, which is part of the “Community” repository. Obtaining the software it’s just a matter of launching our terminal emulator and run:

$ sudo pacman -Sy syncthing

Downloading a tarball



The second method of installing Syncthing consists into downloading the dedicated Linux tarball from the downloads section of the official site. We should download the archive containing the version compatible with the architecture of our system. If we are using Debian or Ubuntu, we can also add the official repository to our software sources, and then install it using apt.

Setting up the firewall

Before we run Syncthing, for it to work correctly, we must setup some firewall rules in order to allow incoming traffic through certain ports. Recent versions of the Firewalld and Ufw firewall managers, already come with a pre-configured service for Syncthing (here with the word “service” we mean a defined set of rules). If using Firewalld, to activate the service, and thus to enable traffic through the required ports), we should run:

$ sudo firewall-cmd --permanent --add-service=syncthing && sudo firewall-cmd --reload

Notice that with the command above, since no zone is specified, the service will be added to the “default” one. If we want to add the service to a specific zone, we should specify it with the --zone option, passing the zone name as argument. To gather information about the service, and see what ports are included in it, we can run:

$ sudo firewall-cmd --info-service=custom

The output of the command above is the following:

syncthing
  ports: 22000/tcp 21027/udp
  protocols:
  source-ports:
  modules:
  destination:
  includes:
  helpers:



As we can see, the ports “involved” in the service are 22000/tcp and 21027/udp. If we are using Ufw (Uncomplicated Firewall), in order to activate the service, we should run:

$ sudo ufw allow syncthing

Using Syncthing

To start the Syncthing daemon, all we have to do is to invoke the Syncthing binary from our terminal:

$ syncthing

Syncthing comes with a web interface which can be used to manage the application, add shared directories and remote devices. Once we run the command above, the Syncthing daemon is launched, and a web browser tab is automatically opened to the 127.0.0.1:8384 address, which is where the interface can be reached.

The default Syncthing shared directory is ~/Sync; it and all the other ones which should be synchronized, are displayed on the left column of the interface. On the right column, instead, we can see information about the device Syncthing is running on, and a list of associated remote devices. In our case the list is still empty:The Syncthing web interface




To add a remote machine and share a directory with it, what we need to do is to exchange devices IDs. To check a device ID, we can click on the “Actions” button on the top right side of the web interface, and click on “Show ID”. The current device ID will be displayed together with the correspondent QRCode:

syncthing-gtk-id

For the application to work correctly the two (or more devices) should know their respective IDs. In the next section we will see how to add a remote device.

Adding a device

To start synchronizing a directory, we need to add a remote device to our configuration. For the sake of this tutorial, I will use the Linux machine where we installed Syncthing as the synchronization “source”, while the remote system will be an Android device (Syncthing can be easily installed from the Google Play Store); the two devices must be connected to the same LAN.

We click on the “Add Remote Device” button on the bottom right of the web interface. On the fist tab of the popup window that will be opened we need to enter the companion device ID, and an optional human-friendly name:



syncthing-add-device

As you can see, if auto-discover works as it should, the other Syncthing device in the LAN should be automatically discovered and reported under the “nearby devices” list. In such cases all we need to do is to click on the correspondent link, and the ID input field will be filled automatically.

On the second tab, “Sharing”, we can select what directories we want to share with the companion device. In this case we selected the default one:

syncthing-add-device

One convenient thing to do, is to check the “Auto Accept” checkbox: this way the system will not prompt us to confirm if we want to accept the synchronization of the directory offered by the remote system, but it will automatically create it.



The third tab contains “advanced settings”. Here we can choose to enter a static address for the remote device, if it has any and dynamic discovery doesn’t work. We could also choose incoming and outgoing rate limit for the device, and what data should be compressed (all data vs metadata only – the default).

syncthing-remote-status

Once done, we can click on the “Save” button. The remote device should than appear in the list on the main page. On the other device we perform the same operation (notice that on the Android app, by clicking on the “hamburger” menu, we have the option to use the web interface) and add our “source” device ID. Once the two devices are paired correctly their status should begin to sync. Once the synchronization process is finished, they will be reported as “Up to Date”:

syncthing-remote-status

At this point our (bi-directional) synchronization should be ready, and the directory will be kept in sync on the two devices.

Autostart the Syncthing daemon




To make the Syncthing daemon start automatically, we can use Systemd, which is the Linux init manager. In this case we will start it as a per-user service, so we won’t need administrative privileges. When a service is configured this way, it will be started only once the user logs in.

The first thing we want to do is to copy the systemd service file into the appropriate directory. If we installed Syncthing from the native repositories of our Linux distribution, such file should be: /usr/lib/systemd/user/syncthing.service; if we downloaded the tarball from the official Syncthing site, the file should be found inside the etc/linux-systemd/user directory.

We create the ~/.config/systemd/user directory if it doesn’t already exist, and copy the file inside it:

$ mkdir -p ~/.config/systemd/user && cp /usr/lib/systemd/user/syncthing.service ~/.config/systemd/user

Once the file is in place, we run the following command to enable the service:

$ systemctl --user enable syncthing.service

Conclusions

On this tutorial we learned how to install and configure Syncthing, a very handful application which let us keep one or more directories synchronized between multiple devices, without the need to use a “central” server. We saw how to install the application, how to setup the firewall to allow traffic through the required ports, how to share a directory between two devices and how to autostart the Syncthing daemon automatically when a user logs in.

Source: https://linuxconfig.org/how-to-keep-files-and-directories-synchronized-across-different-devices-using-syncthing-on-linux