Linux Crumbs
Simbiotic - A Cyberpunk Thriller Novel

Set Up Android File Synchronization with GNU/Linux Using Syncthing

Summary

This tutorial describes how to set up continuous file synchronization using Syncthing between a device running Android and a computer running GNU/Linux. The steps use Free Software, do not use or share your data with external service providers, and work with the stock "non-rooted" Android image.

The steps were developed using Android 10 on a Moto G Fast and Kubuntu 18.04 (Bionic Beaver) GNU/Linux with the K Desktop Environment (KDE). While the steps are specific to the aforementioned environments these should provide a guideline for other distributions with some minor modifications. For example on Fedora replace the debian package manager apt with the fedora package manager dnf. On openSUSE use zypper.

For an alternative file sync method that does not use syncthing and instead uses adb-sync and developer mode on your phone see Set Up Android Two-Way File Synchronization with GNU/Linux.

Update 2021-06-11: Add section Update Syncthing on Linux to enable sync with Android 11.

Contents

Background

Being able to access files on more than one platform can be very convenient. Even more handy is if the changes on one platform are replicated on other platforms. The replication process works well for file formats that can be used on multiple platforms (e.g., text files), but is also useful for backing up files from one platform to another.

Fortunately free software called Syncthing is available to synchronize files among two or more computers. This tutorial focuses on setting up file sync between a computer running GNU/Linux computer and a device running Android.

Choose Text Editors for GNU/Linux and Android

One reason to set up file sync is to be able to share text files between Android and Linux. To do this we need a text editor on Android and on GNU/Linux in order to view and edit the text files.

GNU/Linux provides many text editors, such as kate, gedit, emacs, nano, and vi. I imagine that you already have a favourite (mine is emacs) so I leave the decision of what to use on GNU/Linux to you.

Fortunately Android also provides a broad choice of text editors. For Android 10 I chose Editor from F-Droid. Do feel free to test other text editors. The key thing is that the editor must be able to create, load, view, and save plain text files.

Create Synchronization Folders

We need a master folder to hold all of the subfolders that we wish to synchronize. I chose ~/Sync as the name of my master folder, which is also the "Default Folder" created on Linux when syncthing is run for the first time.

Under the master folder ~/Sync/ create a Notes folder to hold all of our text files.

  1. Create ~/Sync master folder.

     mkdir ~/Sync
  2. Create Notes subfolder.

     mkdir ~/Sync/Notes

Later we will choose a location for the Sync file folder on Android.

Install Syncthing on Linux

  1. Open a terminal window and install syncthing on Linux.

     sudo apt install syncthing
  2. Run syncthing for the first time.

    This will start the syncthing server running interactively and set up a default folder named Sync under your home directory.

     syncthing

    Note that lots of log messages are displayed and the web page http://127.0.0.1:8384/ should be opened in a web browser. If nothing is displayed in the web browser then be sure to enable web browser cookies and ensure javascript is not blocked.

  3. In web browser at Allow Anonymous Usage Reporting? prompt choose No.

  4. Leave syncthing running in the terminal window and leave the web browser open.

Update Syncthing on Linux

When I updated my phone to Android 11, syncthing stopped working. I was able to resolve the issue by upgrading Syncthing using the steps at Syncthing - Debian/Ubuntu Packages.

  1. Add the release PGP keys:

     sudo curl -s -o /usr/share/keyrings/syncthing-archive-keyring.gpg \
       https://syncthing.net/release-key.gpg
  2. Add the "stable" channel to your APT sources:

     echo "deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable" \
       | sudo tee /etc/apt/sources.list.d/syncthing.list
  3. Update and install syncthing:

     sudo apt-get update
     sudo apt-get install syncthing
  4. Visit Syncthing on your PC at http://127.0.0.1:8384/ and respond to some new prompts.

    Following are the choices I made:

    • For Watching for Changes I chose Yes.
    • For Automatic Crash Reporting I chose Disable Crash Reporting.
    • For GUI Authentication: Set User and Passord I chose OK.

After the upgrade I had the following versions of syncthing installed:

  • 1.17.0 on Kubuntu 18.04
  • 1.17.0 on Android 11

Install Syncthing on Android

Here we install Syncthing and configure it to only sync when charging via USB cable and WiFi connected. This will help reduce battery usage.

For reference when I first installed syncthing with default settings and granted requested permissions, the app drained 5% of the phone battery every hour!

  1. Install Syncthing app from F-Droid.

  2. Open Syncthing app.

  3. At Welcome to Syncthing for Android - Introduction choose Continue.

  4. At Welcome to Syncthing for Android - Storage Permission choose Grant Permission.

  5. At Allow Syncthing to access photos, media and files on your device? dialog choose Allow and choose Continue.

  6. At Welcome to Syncthing for Android - Location Permission choose Grant Permission.

  7. At Allow Syncthing to access this device's location? choose Allow all the time and choose Finish.

  8. At Battery Optimization dialog choose Turn off for Syncthing.

  9. At Let app always run in background? choose Allow.

    Can change later from Settings -> Apps & notifications.

  10. Choose three horizontal lines icon in upper left.

  11. Choose Settings and configure the following:

     Run Conditions
       Run on Wi-Fi                          Yes
       Run when device is powered by         AC power
    
     Behaviour
       Start service automatically on boot   Yes
  12. Navigate back to main Syncthing screen

Add Syncthing Devices

This section involves steps performed either from the Syncthing app running on Android or the Syncthing user interface running in the Linux web browser. To make this distinction clear each step is prefixed with either [Android] or [Linux].

  1. [Android] Choose three horizontal lines icon in upper left.

  2. [Android] Choose Show device ID.

    Should display Device ID and a QR code.

  3. [Linux] On http://127.0.0.1:8384/ web page choose Add Remote Device.

  4. [Linux] Under Device ID select one of the nearby devices if and only if it matches the Android device ID. Otherwise type in the long Android device ID.

  5. [Linux] For Device Name enter "your android device name". For example: CGMotoGFast.

  6. [Linux] Under Compression enable Auto Accept.

  7. [Linux] Choose Save.

    Should now see the device we added listed under Remote Devices.

  8. [Linux] Choose Actions -> Show ID.

    Should display Linux Device ID and a QR code that can be scanned.

  9. [Android] Choose Finish to close Device ID dialog.

  10. [Android] On Syncthing screen choose Devices tab.

  11. [Android] Choose + icon in upper right.

  12. [Android] At Add Device screen choose QR code icon to right of Device ID.

  13. [Android] At Allow Syncthing to take pictures and record video choose Allow.

  14. [Android] Aim phone camera at Linux computer screen and it should automatically recognize QR code and enter in the long device ID.

  15. [Android] At Add Device screen for Name enter "your linux computer name". For example use the Linux computer hostname. Mine is kaby.

  16. [Android] Choose checkmark icon in upper right.

  17. [Linux] At Device Identification screen choose Close to close the QR code display.

Add Linux Folder to Sync

Here we share the ~/Sync/Notes/ folder for file synchronization.

  1. On http://127.0.0.1:8384/ web page choose Add Folder.

  2. At Add Folder dialog configure as follows:

    
         Folder Label           Notes
         Folder ID              <don't change this>
         Folder Path            /home/your-userid/Sync/Notes
         Share With Devices     <Enable device name from above, e.g., CGMotoGFast>
    

    Note that under Advanced settings, folders can be shared as Folder Type(s) Send & Receive. Also available is Send Only from the computer that provides the source folder contents (and correspondingly receive only on the computers that act only as a destination).

  3. Choose Save.

Because I prefer not to have the entire "Default Folder" (~/Sync) synchronized, but instead just the subfolders synchronized I remove the "Default Folder" as follows:

  1. On http://127.0.0.1:8384/ web page choose Default Folder.

  2. Choose Edit.

  3. Choose Remove.

  4. At Remove Folder dialog choose Yes.

Accept Folder to Share on Android

  1. Check for a notification on Android (swipe down from top of screen).

  2. Double-tap on notification Syncthing - Device "kaby" wants to share folder "Notes (..."

  3. At Create Folder screen choose Directory. A folder manager screen is displayed.

  4. Choose three vertical dots (...) icon in upper right and choose Show internal storage.

    If instead you see Hide internal storage then internal storage is already available so no change needed.

  5. Choose three horizontal lines icon in upper left and choose the name for your device (mine is CGMotoGFast).

  6. At your device name screen choose three vertical dots (...) icon and choose New Folder.

  7. At New folder dialog enter Sync and choose OK.

  8. At Sync folder manager screen choose three vertical dots (...) icon and choose New Folder.

  9. At New folder dialog enter Notes and choose OK.

  10. At bottom of screen choose Allow Access To 'Notes'.

  11. At Allow access to 'Notes' in 'your device name'? choose Allow.

    On my phone this looks like "/storage/emulated/0/Sync/Notes".

  12. At Create Folder screen choose checkmark icon in upper right.

Test Sync

Test that creation of a text file on one device will replicate to the other device.

  1. Create a text file in the Sync/Notes folder on either Android or Linux.

    Note that for the sync to occur the phone must be connected to power via the USB cable and WiFi must be active. The syncthing process must also still be running on GNU/Linux.

  2. Check to see if text file is replicated on the other device.

    Before proceeding to the next step make sure that the text file is replicated properly and exists on both Android and Linux. If not then some trouble shooting is required.

After confirming that syncthing is working we stop the syncthing process running in a terminal window.

  1. Click on the terminal window running syncthing to make the window active.

  2. Type Ctrl+C to stop syncthing running.

Configure Desktop to Start Syncthing at Login

Because it is inconvenient to start syncthing every time we logout and login, configure syncthing to start at login time. The method used in these steps is specific to the K Desktop Environment (KDE). For other desktop environments try an Internet for "autostart script" with the name of your desktop environment.

  1. Create a syncthing.daemon script under the ~/bin folder (if ~/bin folder missing then create with mkdir ~/bin).

    For example:

     kate ~/bin/syncthing.daemon
  2. Copy the following syncthing.daemon script lines and paste the lines into our editor.

    ----- Begin syncthing.daemon script -----

     #!/bin/bash
     #
     #         Name:  syncthing.daemon
     #  Description:  Script to start syncthing without invoking
     #                a web browser tab.
    
     #  Start syncthing
     syncthing -no-browser

    ----- End syncthing.daemon script -----

  3. Save the file and exit the editor.

  4. Make the syncthing.daemon script executable.

     chmod a+x ~/bin/syncthing.daemon
  5. Add syncthing.daemon script to start at KDE login.

    Open K -> Settings -> System Settings

  6. Under Workspace section choose Startup and Shutdown.

  7. Choose Autostart

  8. Choose Add Script...

  9. In the dialog that pops up for Shell Script path enter ~/bin/syncthing.daemon and choose OK.

  10. Close System Settings window.

Test Syncthing Starts at Login

  1. Logout and login

  2. Open a terminal window and check for the syncthing.daemon process.

     ps -ef | grep syncthing.daemon

    The output should show the syncthing.daemon process running and look something like:

    gedakc 7075 6761 0 10:32 ? 00:00:00 /bin/bash /home/gedakc/.config/autostart-scripts/syncthing.daemon
    gedakc 7633 7513 0 10:34 pts/2 00:00:00 grep --color=auto syncthing.daemon

    The important line to see is the bash invocation of the syncthing.daemon process.

Other Sync Possibilities

Some apps can benefit from file synchronization. For example a password manager with file sync permits passwords to be accessed on both Linux and Android. Another example is a TV show tracking app on Android with backups stored on Linux.

Basic setup guidelines for these two examples follows.

Note that changes to the syncthing configuration are made in a web browser at the following link:

http://127.0.0.1:8384/

KeePass Password Manager

To use keepass password manager on both Linux and Android:

Install KeePassX on Linux (sudo apt install keepassx) and KeePassDroid on Android.

On Linux save the KeePassX data file keyring2.kdbx in ~/.keepass/ folder.

Set up sync between ~/.keepass/ on Linux and /storage/sdcard0/keepass on Android.

DroidShows TV Show Tracker

To use DroidShows on Android and keep a backup file on Linux:

Install DroidShows on Android.

Set up sync between ~/Sync/DroidShowsBackup/ on Linux and /storage/sdcard0/DroidShows/ on Android. Consider setting the Android side of the file sync to "Send Only" as the file on Linux is a backup.

Ignoring Files

If some files should not be synchronized to (or from) other devices, a file called .stignore can be created containing file patterns to ignore. The .stignore file must be placed in the root of the folder.

For example emacs creates temporary files with the patterns *~ and #*#. To ignore these files I created ~/Sync/Notes/.stignore file with the following contents:

----- begin .stignore file -----

*~
#*#

----- end .stignore file -----

References

While researching this article I found the following references useful:

The Medusa Deception novel - free first in series
News   Documentation     Copyright (c) 2013-2024 Curtis Gedak

Get GParted at SourceForge.net. Fast, secure and Free
	Open Source software downloads Valid HTML 4.01! Correct CSS!