Set up the open-source file synchronization tool Syncthing

Syncthing

Syncthing is a continuous file synchronization program. It synchronizes files between two or more computers in real time, safely protected from prying eyes. Your data is your data alone and you deserve to choose where it is stored, whether it is shared with some third party, and how it’s transmitted over the internet.

Setup Environment and Preparation

  • ✅ Debian 12.6
  • ✅ Docker version:27.1.2
  • ✅ Docker compose

Build

Install the Syncthing server

1#Download the Syncthing server
2wget https://github.com/syncthing/syncthing/releases/download/v1.27.13-rc.1/syncthing-linux-amd64-v1.27.13-rc.1.tar.gz
3#Extract the downloaded Syncthing server
4tar xzvf syncthing-linux-amd64-v1.27.13-rc.1.tar.gz
5#Enter the extracted directory
6cd syncthing-linux-amd64-v1.27.12/
7#Copy Syncthing to the /usr/local/bin directory, and then you can start the server directly using the syncthing command
8cp syncthing /usr/local/bin

Then, simply enter the syncthing command to start the Syncthing server

1syncthing

You can access the Syncthing Web GUI via the address 127.0.0.1:port (default is 8384)

(Optional)Modify Syncthing’s configuration (such as port number, etc.)

Syncthing usually generates a configuration file in the current user’s directory. For example, if I am under the root user

1nano /root/.local/state/syncthing/config.xml
(Optional)Start Syncthing using systemd

Create a systemd configuration file

1sudo nano  /etc/systemd/system/syncthing.service

The systemd configuration file is as follows:

 1[Unit]
 2Description=Syncthing - Open Source Continuous File Synchronization for %I
 3Documentation=https://docs.syncthing.net/
 4After=network.target
 5
 6[Service]
 7User=root #Start as the root user
 8ExecStart=/usr/local/bin/syncthing -no-browser -home=/home/null/.local/state/syncthing
 9Restart=on-failure
10SuccessExitStatus=3 4
11
12[Install]
13WantedBy=default.target

ctrl+oExit,ctrl+xSave Start Syncthing with systemd

1systemctl daemon-reload
2systemctl enable syncthing
3systemctl start syncthing
(Optional)Configure Nginx reverse proxy and TLS certificates for the Syncthing Web GUI
Configure Nginx reverse proxy
1#`syncthing` is a custom name because it's named after the Syncthing setup
2nano /etc/nginx/sites-available/syncthing

The Nginx site configuration file is as follows:

1server {
2        listen 80;
3        server_name example1.com;
4        location / {
5        proxy_pass http://127.0.0.1:8384; #'8384' is the default listening port for the Web GUI
6       }
7     }

ctrl+oExit,ctrl+xSave
Create a symbolic link to enable the Nginx site configuration file

1sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/
2#Test if the Nginx configuration is correct
3sudo nginx -t
4#Reload Nginx configuration
5sudo systemctl reload nginx
Configure TLS certificates using Certbot
1#Install Certbot
2sudo apt install certbot python3-certbot-nginx
3#Request a certificate
4sudo certbot --nginx -d your_domain_or_ip

When you see successful, refresh the page and check for a 🔒icon next to the domain. This indicates that the TLS configuration is successful

Deploy Syncthing with Docker Compose to discover and use relay servers

1cd syncthing-linux-amd64-v1.27.12/
2#Create Docker Compose configuration
3nano docker-compose.yaml

docker compose配置如下:

 1services:
 2   syncthing_discovery_server: 
 3    image: syncthing/discosrv
 4    container_name: syncthing-discovery-server
 5    command: -debug -listen=":8443" 
 6    environment:
 7      - PUID=1000
 8      - PGID=1000
 9    volumes:
10      - ./syncthing/discosrv:/var/stdiscosrv
11    ports:
12      - 8443:8443 # Listen address (default “:8443”)
13    restart: always
14
15   syncthing_relay_server: 
16    image: syncthing/relaysrv:latest
17    container_name: syncthing-relay-server
18    command: -debug -pools="" -listen=":22067"
19    environment:
20      - PUID=1000
21      - PGID=1000
22    volumes:
23      - ./syncthing/strelaysrv:/var/strelaysrv
24    ports:
25      - 22067:22067  # Data connection port for the relay server (must be open)
26      #- 22070:22070  # Used for the public relay server pool to display data transfer, client counts, and other statuses; this can be left closed
27    restart: always

ctrl+oExit,ctrl+xSave
Pull the image

1docker compose pull

Start the Docker Compose project and run it in the background

1docker compose up -d

Obtain the Server device ID for Syncthing discovery and relay services

1# Check the syncthing-discovery-server logs to obtain the Server device ID
2docker logs syncthing-discovery-server

Server device ID isFollowing that is the Server device ID (the discovery and relay service IDs are the same)

1stdiscosrv v1.27.12 "Gold Grasshopper" (go1.22.6 linux-amd64) [email protected] 2024-09-06 07:15:45 UTC [noupgrade, purego]
2Server device ID is RUA24CQ-SVNBBBJ-3UKXRJZ-11111-111111-JH7YJNQ-S4KCO4W-YHYIPQL

Configure reverse proxy and TLS certificates for the discovery and relay servers

The Nginx site configuration file is as follows:

1server {
2        listen 80;
3        server_name example2.com;
4        location / {
5        proxy_pass http://127.0.0.1:8443;
6       }
7     }

Create symbolic links and configure TLS certificates by following the same steps as above

Configure discovery and relay servers in Syncthing

Open the Syncthing web page, click ⚙操作->⚙设置->连接

1# Protocol listening address, relay service URI
2relay://public_IP:22067?id=relay_server_device_ID
3
4# Global discovery server
5https://public_IP:8443/?id=discovery_server_device_ID

Reference

Built with Hugo
Theme Stack designed by Jimmy