How to Sync Microsoft OneDrive with Linux

sync-onedrive-linux-featured

Sometimes lately it can be necessary to use propriety products. Open source enthusiasts would love to have a cloud-based storage facility, but the fact remains that companies like Google, Amazon, Dropbox and Microsoft have cornered the cloud storage market. They offer reliable products for free, and this means that yes, they will scan or read your files in order to comply with legal requests or to serve adverts. It is a fact of life, but this doesn’t mean you shouldn’t use them.

OneDrive is Microsoft’s cloud-based storage offering, and even though it is naturally suited to the Windows platform, it will connect with Linux-based machines with little effort. There are a number of ways to do this, and this is where the onedrive script comes into play.

OneDrive

Onedrive is a CLI-based client that allows you to sync quickly and easily with OneDrive.

In order to install onedrive, first you need to install some dependencies and set up git, unless you already have it. Head to the Terminal and enter one of the following.

For Ubuntu:

If you are using Ubuntu 18.04 or newer, use snap to install dmd:

If not:

For Fedora:

Arch Linux:

Installation

After installing the dependencies, fetch the files with git:

And compile and install it:

After you have done this, now you need to authorize onedrive with Microsoft so it can access your account. Simply enter the following in the Terminal:

And it will prompt you to visit the URL to get authorization.

onedrive-authorize-url

Log in to your OneDrive account, and grant the app permission to access your account.

ms-auth

Once this is done, you will be presented with a blank white page. Copy the URL and paste it into the Terminal at the prompt.

onedrive-response-url

This will complete the setting up.

To start synchronizing OneDrive to your local folder, run

Onedrive will start downloading all your files in the cloud to your local folder.

Alternatively, you can run:

to get it to monitor your local folder for changes.

Configuration

You can find the “config” file at the onedrive git folder. To make it active, move it to the “~/.config/onedrive/” folder.

Open the config file. There are three options that you can configure: “sync_dir” and “skip_files.”

  • sync_dir: the location to store your OneDrive files. All files placed in/removed from this folder will be synced to the cloud.
  • skip_files: the file type (or file pattern) that won’t be synced.
  • monitor_interval: the time interval between sync operations.

Once you have made the changes, save and restart onedrive.

At any time, you can check your configuration with the command:

onedrive-display-config

Sync List

If you prefer to use selective sync rather than sync the whole OneDrive folder, you can create a file named “sync_list,” and place it in the “~/.config/onedrive” folder. Inside the file add the relative path (from the synced OneDrive folder) to the files/folders you want to sync.

Starting onedrive at login

By default, onedrive doesn’t start when the user logs in. To get it to run automatically when you log in, you can add onedrive --monitor to your startup application.

Now your OneDrive will sync with your Linux system. How do you sync OneDrive to your setup, and do you prefer other tools? Let us know in the comments section below.

This post was updated in March 2019 with a working repository.

Image credit: Microsoft OneDrive website under a magnifying glass by DepositPhotos

54 comments

  1. Hey! I’m getting an error upon compiling:
    user:~/onedrive $ make
    dmd -g -ofonedrive -O -L-lcurl -L-lsqlite3 -L-ldl -J. src/config.d src/itemdb.d src/log.d src/main.d src/monitor.d src/onedrive.d src/qxor.d src/selective.d src/sqlite.d src/sync.d src/upload.d src/util.d
    make: dmd: Command not found
    Makefile:29: recipe for target ‘onedrive’ failed
    make: *** [onedrive] Error 127

    1. You are missing the dlang complier (dmd) in your system. You can download and install it from here (https://dlang.org/download.html).

    2. I had the exact same issue on my first time running “make” after cloning the repo.
      I’m glad you asked this question it was the only real hangup that I had to deal with.

      And thanks to Damien for his quick follow-up response!!

  2. I am having an issue with the starting onedrive at login. The part right here:

    sudo systemctl –user enable onedrive

    Returns this at the terminal:
    Failed to connect to bus: No such file or directory

    what step should I take next?

    1. Can you run the command: export XDG_RUNTIME_DIR=/run/user/`id -u` and enable the autostart command again.

  3. systemctl –user commands should be run (and will work properly) without sudo

  4. Is possible to run multiple client simultaneously? I have 1 personal and 2 bussiness account, is possible to have all 3 synchronized ?

    1. Yes. You need to create a different configuration file for each instance. For example, you may have:
      ~/.config/onedrive
      ~/.config/onedrivebusiness1
      ~/.config/onedrivebusiness2

      Copy the config file from the build directory to each of the above 3, then modify them accordingly.

      Initialize each with:

      onedrive –synchronize
      onedrive –confdir ~/.config/onedrivebusiness1 –synchronize
      onedrive –confdir ~/.config/onedrivebusiness2 –synchronize

      Start each one from the command line, or if from rc.local with something like:

      sudo -u yourusername /usr/local/bin/onedrive –monitor 2>1 > /dev/null &
      sudo -u yourusername /usr/local/bin/onedrive –confdir ~/.config/onedrivebusiness1 –monitor 2>1 > /dev/null &
      sudo -u yourusername /usr/local/bin/onedrive –confdir ~/.config/onedrivebusiness2 –monitor 2>1 > /dev/null &

  5. Very informative and descriptive article. It took me a few, but Onedrive is up and running without any issues thanks to your post!!

  6. Hi Damien,
    I’m running into an error while trying to install onedrive
    user:~/onedrive$ make

    cc: No such file or directory
    Error: linker exited with status 255
    Makefile:29: recipe for target ‘onedrive’ failed
    make: *** [onedrive] Error 1

    1. Do you have Ubuntu 18.04 Server edition? You probably don’t have the g++ package installed.

      Enter the following command into a Terminal window

      sudo apt install g++

      Then you should be good to go.

  7. Hi all,

    The git clone command is referring to git clone https://github.com/skilion/onedrive.git which appears to have been abandoned and contains issues that have not been fixed.

    If you wish to use the latest code which contains numerous fixes and enhancements you need clone from here: https://github.com/abraunegg/onedrive

    @Ren – Your issue is fixed in the updated branch – it is a coding defect as to why it does not work
    @Honza – Yes it is, you need 3 separate configuration files / config directories but no issue in doing that.

    1. Hi @abraunegg
      This git is indeed great. I am using it to sync my onedrive account, which has a fair large amount of files (~150k), and the sync is a bit slow. Is there any way of boosting it somehow?

      1. I would suggest that you ensure you are using the latest version (2.3.3) or later as some performance enhancements were included in the last few releases.

        Also – monitor the github site for known issues and current WIP branches for fixes

  8. I get and error when I do the Configuration part. When I run “cp ~/onedrive/config ~/ .config/onedrive/config” I get the following error: cp: target ‘.config/onedrive/config’ is not a directory

    Any help you can propose? Thanks a lot!

    1. @Gert Kruger There is a space too much in the command, it should read “cp ~/onedrive/config ~/.config/onedrive/config”

  9. Hi Damien,
    I followed your instructions and onedrive was working and syncing fine. Then I changed my microsoft account password.
    I now get an authentication error:

    “error_description”: “AADSTS70000: The user could not be authenticated as the grant is expired. The user must sign in again.\r\nTrace ID: 335b3c36-b284-41e0-86a1-b2fa77f52700\r\nCorrelation ID: 251bfd43-68fa-455e-bda0-987a3c6938cc\r\nTimestamp: 2019-01-27 08:13:42Z”

    What command do I use to change my password?

    1. Craig, the onedrive application is using token to communicate with your OneDrive account. It doesn’t store your password. You can go to “~/.config/onedrive” folder and remove the “refresh_token” file (optionally the sqlite file too). If you want, just move them to another location (instead of deleting them). Then run “onedrive” command again and it should prompt you to reauthorize access to your OneDrive account.

  10. Hi Damien,
    I renamed the “refresh_token” and sqlite files, ran onedrive and when I entered the URI response provided by microsoft I got the following message:

    “Delta link expired, resyncing
    std.json.JSONException@/usr/lib/gcc/x86_64-linux-gnu/7/include/d/std/json.d(551): JSONValue is not an object”

    I did a bit of reseach that resulted in running the “onedrive –resync –verbose” command, then all my files started sycning.

    Everything is now all good. Thank you very much for your help – people like you make the world a better place.

    Regards
    Craig Taylor

    1. This is a known issue and fixed in my branch

  11. $ sudo systemctl –user enable onedrive
    Failed to connect to bus: No such file or directory

    1. Try the command again without sudo. ie

      systemctl -user enable onedrive

  12. Thanks for this, and thanks to abraunegg for the update! This works beautifully.

    However, how do I avoid re-uploading files I’ve deleted from OneDrive itself but are stored on the local machine?

    1. This is a known bug and fixed in my fork – you need to modify your config and resync

  13. All is well until after starting onedrive and copy the onedrive url after logging in.
    After pasting and enter it saysL:
    Invalid uri. Could not initialize onedrive API

  14. Thanks Damien. I was able to get OneDrive working. I used the following repo.

    https://github.com/abraunegg/onedrive

    I had only one issue:

    https://github.com/abraunegg/onedrive
    Error: Failed to connect to bus: No such file or directory

    I tried your suggested cmd: export XDG_RUNTIME_DIR=/run/user/`id -u`

    I still get the same error. Since this is to only enable autostart on login. I am fine if I have to manually start the service.

    It’ll help if you can suggest on how can I deal with the error or what are the steps I can follow to manually start the service. (I guess I should use onedrive –synchronize. Correct me if I am wrong)

    Thank you for the detailed article. This was the last thing I was looking for to completely move from Windows to Ubuntu.

    1. Instead of using systemd, you might just want to add the code onedrive --synchronize to your startup application list.

  15. Hello Damien, I quite new in Linux, and I was installing OneDrive and after pasting the url of the white blank page into the terminal it says:

    “–synchronize or –monitor missing from your command options or use –help for further assistance”

    So the –synchronize command doesn’t work.

    What should I do?

  16. Hi,

    Found a typo;

    onedrive –sychronize >> onedrive –synchronize

  17. Hell again,

    “monitor_interval: the time interval between sync operations.

    Once you have made the changes, save and restart onedrive.”

    Restart how?
    This info needs to be added.

    1. To enable the onedrive service

      systemctl –user enable onedrive

      To start the service

      systemctl –user start onedrive

      To restart the service

      systemctl –user restart onedrive

      1. Thank you!

  18. Hello Damien

    How can you skill multiple folders
    i have tried but it did not work

    thank you

  19. Hi Damien

    I have tried this method/tool out and got semi-good results. It is syncing the folders and files I had on my OneDrive, EXCEPT it will not sync OneNote files. They are just ignored.

    Am I out of luck here? Or is there something I can do to enable OneNote….which is my primary objective in getting OneDrive to sync.

    Thanks,
    …Patrick

  20. Hi Damien

    I ran `onedrive –resync –synchronize -v+` and found some entries like the following:
    This item type (Patrick’s Woodshop) is not supported

    Since this item is a OneNote file I am concluding that I will not be able to sync OneNote files with this tool.
    Do you have any further insight?

    Thank you!
    …Patrick

  21. Hi Damien!

    I’m using Linux Mint and ran into the same problem as Dante did – neither the synchronize nor the monitor commands are available. I got

    –synchronize or –monitor missing from your command options or use –help for further assistance

    No OneDrive sync will be performed without either of these two arguments being present

    What do I do to rectify this?

    1. It is best you refer to the readme to assist with commands and configuration: https://github.com/abraunegg/onedrive/blob/master/README.md

  22. Thanks..

    It works!

  23. Thanks for this. One point though …

    This section:
    cd onedrive
    make
    sudo make install

    … didn’t work for me out the box on Ubuntu 19.04. I had to do this instead:
    cd onedrive
    ./configure
    make
    sudo make install

    1. I had the same issue, thanks for posting this

  24. Does anyone know how to sync additional folders (OneDrive – Company) into this OneDrive CLI? I’m only available to sync personal files within OneDrive, which renders it almost useless.

    Regards

    1. This is supportes in my version. Refer to https://github.com/abraunegg/onedrive/blob/master/README.md for details

  25. This may sound like an idiot’s question but does the client for linux encrypt data in transit like the Windows onedrive client does?

    1. All traffic is https / tls 1.2 protected

  26. Hi Damien,

    When I run the command “onedrive”, I didn’t get the link to verify my account info. Instead, it gave “Delta link expired, resyncing” and some other staff (it ends with “Segmentation fault (core dumped)”). Do you happen to know what this might be?

    Thanks,
    Rui

    1. This is a known bug with the skilion version which is fixed in my fork

    2. If you have any issues with the client, rather than posting a comment here – please post an issue ticket here – https://github.com/abraunegg/onedrive/issues

      1. Thank you! I got it fixed somehow. It seems the reason is that I installed before and had verified my onedrive account back then.

  27. Great for users with small OneDrive but I get error message in the middle of synchronizing. Enabling verbose mode I get:

    Downloading: Pictures/Camera Roll/20151017_192622.jpg
    sync.SyncException@src/sync.d(182): HTTP request returned status code 429 (Too Many Requests)
    —————-
    0x55990725491a ???
    ???:0
    0x55990724a002 ???
    ???:0
    0x559907249a72 ???
    ???:0
    0x55990739d8be ???
    ???:0
    0x55990739da2e ???
    ???:0
    0x55990739ddea ???
    ???:0
    0x55990739da2e ???
    ???:0
    0x55990739dbc0 ???
    ???:0
    0x559907248ea3 ???
    ???:0
    0x7f6662f1cb96 __libc_start_main
    ???:0
    0x559907240c99 ???
    ???:0
    0xffffffffffffffff ???
    ???:0
    onedrive.OneDriveException@src/onedrive.d(396): HTTP request returned status code 429 (Too Many Requests)
    —————-
    0x55990724e9b3 ???
    ???:0
    0x55990724df3d ???
    ???:0
    0x55990724c829 ???
    ???:0
    0x55990725556d ???
    ???:0
    0x5599072551d8 ???
    ???:0
    0x55990725446c ???
    ???:0
    0x55990724a002 ???
    ???:0
    0x559907249a72 ???
    ???:0
    0x55990739d8be ???
    ???:0
    0x55990739da2e ???
    ???:0
    0x55990739ddea ???
    ???:0
    0x55990739da2e ???
    ???:0
    0x55990739dbc0 ???
    ???:0
    0x559907248ea3 ???
    ???:0
    0x7f6662f1cb96 __libc_start_main
    ???:0
    0x559907240c99 ???
    ???:0
    0xffffffffffffffff ???
    ???:0

  28. sergey@sergey-inspiron-5770:~$ git clone https://github.com/abraunegg/onedrive.git
    Cloning into «onedrive»…
    remote: Enumerating objects: 62, done.
    remote: Counting objects: 100% (62/62), done.
    remote: Compressing objects: 100% (41/41), done.
    remote: Total 2698 (delta 39), reused 39 (delta 21), pack-reused 2636
    Получение объектов: 100% (2698/2698), 1.66 MiB | 1.84 MiB/s, готово.
    Определение изменений: 100% (1798/1798), готово.
    sergey@sergey-inspiron-5770:~$ cd onedrive
    sergey@sergey-inspiron-5770:~/onedrive$ make
    make: *** No targets set and no makefile found. Stop.

    1. I had the same issue. Run the following :
      ./configure
      make
      sudo make install

  29. Hi there!
    I still get the trouble with the ./configue && make && make install. It did synchronized but I cannot set up the configuration.

    see the error below:

    checking for a BSD-compatible install… /usr/bin/install -c
    checking for pkg-config… no
    checking for dmd… dmd
    checking for curl… no
    configure: error: in `/home/taotoa/onedrive’:
    configure: error: The pkg-config script could not be found or is too old. Make sure it
    is in your PATH or set the PKG_CONFIG environment variable to the full
    path to pkg-config.

    Alternatively, you may set the environment variables curl_CFLAGS
    and curl_LIBS to avoid the need to call pkg-config.
    See the pkg-config man page for more details.

    To get pkg-config, see .
    See `config.log’ for more details

  30. Thanks for the write-up!

    One small thing – I had to run `./configure` before being able to run `make`.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.