Using WEBDAV to mount a NextCloud account on Ubuntu or macOS

; Date: Sat Jan 20 2024

Tags: Linux

NextCloud, as an open source alternative to Google Drive, offers file storage on personally-owned cloud infrastructure. While we primarily access our files with the NextCoud web user interface, it supports the WEBDAV. That means we can easily mount a NextCloud account on our desktop computer as a filesystem.

NextCloud is a complete open-source self-hostable platform of applications, including file storage, roughly equivalent to Google Workspace. As cool as that is, out of the box accessing your files is done through the web browser. In many ways it's better, and more convenient, to access files stored in NextCloud with regular applications on our desktop computer.

For example, while the NextCloud ecosystem includes WYSIWYG text editors, we'd have a better editing experience in regular Libre Office. The same goes with spreadsheets and images. Desktop applications for spreadsheets, drawing pictures, creating presentations, managing e-mail, and the like are better than what NextCloud offers. The same holds true for Google Workspace, because no matter how good Google Docs is, it's doesn't have the same fidelity as a desktop word processor application.

The solution is to mount our NextCloud account on our desktop computer as a filesystem. Once the files are available as regular files in the filesystem, we can use regular desktop applications to work on those files. In theory.

That's where we get to WEBDAV. This is an Internet protocol offering the ability to access filesystems on a remote server. While command-line tools can access/manipulate files over WEBDAV, filesystem drivers for WEBDAV exist. That means, after installing the WEBDAV filesystem driver, you can mount a NextCloud instance as a filesystem. And, it's not just NextCloud, since there are open source WEBDAV servers.

I'm doing this task from Ubuntu. I first tried the hard way, using the davfs2 driver directly. That required getting past a couple difficult obtuse issues. Turns out that Ubuntu has a simple method that we'll get to at the bottom of this article.

Mounting a NextCloud instance over WEBDAV using davfs2, the hard way

This software is billed as: "davfs2 is a Linux tool for connecting to WebDAV shares as though they were local disks."

What that means is:

$ df -h
...
https://cloud.EXAMPLE/remote.php/dav/files/USERNAME/   16E   45M   16E   1% /home/david/dav/david

This is a regular filesytem mount, clearly showing the URL for the WEBDAV URL of a NextCloud instance. This is mounted to a local directory. Normal Ubuntu tools like ls and cat can access those files just like any other file.

This computer also has a couple S3 buckets mounted as filesystems, as discussed earlier: Mounting S3 (compatible) buckets as a filesystem on Linux or macOS using S3FS or Rclone

The trick is to install (github.com) the davfs2 driver

Another tool that might be of interest is Cadaver. This tool is for low-level interaction with WEBDAV servers. It was not used in the following project, but was useful when debugging a related protocol, CALDAV, in: How to import or export contacts data on Android and iPhone (iOS) devices

Installing davfs2

The GitHub repository only discusses installing davfs2 from source. For Ubuntu (and other Debian derivatives):

$ sudo apt-cache search davfs
[sudo] password for david: 
davfs2 - mount a WebDAV resource as a regular file system
$ sudo apt-get install davfs2

Check the package management system for your distro. You'll probably find that it's already available.

The package installs two commands, mount.davfs and umount.davfs, meaning it integrates with the normal mount and umount commands. On Ubuntu, a configuration file is installed here:

$ ls /etc/davfs2/
certs  davfs2.conf  secrets

Some tutorials also instruct one to copy the secrets file to your home directory:

$ ls ~/.davfs2/
secrets

But, I could not get that to do anything.

The davfs2.conf file contains configuration settings, and secrets contains passwords.

Using davfs2 to mount a NextCloud instance on Ubuntu

The next step is to run the mount command to mount a WEBDAV filesystem from the NextCloud instance:

$ sudo mount -t davfs -o noexec  https://cloud.EXAMPLE/remote.php/dav/ ~/dav/david
Please enter the username to authenticate with server
https://cloud.EXAMPLE/remote.php/dav/ or hit enter for none.
  Username: reikiman
Please enter the password to authenticate user reikiman with server
https://cloud.EXAMPLE/remote.php/dav/ or hit enter for none.
  Password:  
/sbin/mount.davfs: Mounting failed.
Could not authenticate to server: rejected Basic challenge

This was stumped me for quite awhile. It failed the same way with the https://cloud.EXAMPLE/remote.php/dav/USERNAME URL as well.

One possible cause for "rejected Basic challenge" is if your NextCloud password contains a - or : character. If that's the case, change the password.

My working configuration is adding this entry in /etc/fstab

https://cloud.EXAMPLE/remote.php/dav/files/USERNAME/ /home/david/dav/david davfs user,rw,noauto 0 0

The noauto setting means a failure to mount the filesystem - e.g. if the computer is not connected to the Internet - is not a fatal failure.

Next is adding this entry in /etc/davfs2/secrets:

https://cloud.EXAMPLE/remote.php/dav/files/USERNAME/ USERNAME PASSWORD

If you want to use a per-user secrets file:

$ sudo cp  /etc/davfs2/secrets ~/.davfs2/secrets
$ sudo chown USER.USER ~/.davfs2/secrets 

You then add the above line to this file. However, I was unable to get this to do anything.

With the above /etc/fstab entry you might still be asked for a user-name/password:

$ sudo mount ~/dav/david
Please enter the username to authenticate with server
https://cloud.EXAMPLE/remote.php/dav/files/USERNAME/ or hit enter for none.
  Username: USERNAME
Please enter the password to authenticate user USERNAME with server
https://cloud.EXAMPLE/remote.php/dav/files/USERNAME/ or hit enter for none.
  Password:  
/sbin/mount.davfs: warning: the server does not support locks

Make sure the user-name and password in the secrets file are correct.

To clear the message about locks, edit /etc/davfs2/davfs2.conf and make this change:

use_locks       0

To clear this message:

$ umount ~/dav/david
umount: /home/david/dav/david: must be superuser to unmount.

Your user must be in the davfs2 group. This may have already been handled with apt-get install davfs2 above. If not run this:

$ sudo dpkg-reconfigure davfs2
$ sudo usermod -a -G davfs2 USER

This sets the SUID bit on relevant binaries. That allows any user in the davfs2 group to mount/umount WEBDAV filesytems. You'll need to logout then login again to be in the group.

Or, you can simply use sudo

$ sudo mount ~/dav/david
# no message is printed
$ sudo umount ~/dav/david
/sbin/umount.davfs: waiting for mount.davfs (pid 874785) to terminate gracefully .. OK

While it's cleaner to add yourself to the davfs2 group, using sudo also works.

A careful read of the (docs.nextcloud.com) NextCloud documentation for WEBDAV is recommended.

Two useful search phrases were:

  • davfs could not authenticate to server: rejected Basic challenge
  • mount nextcloud webdav ubuntu

And this blog post was very helpful: (www.linuxbabe.com) https://www.linuxbabe.com/cloud-storage/access-nextcloud-files-webdav-ubuntu

Mounting a NextCloud instance over WEBDAV, the easy way

All that was interesting to learn. But, on Desktop Ubuntu there is a trivially easy way to proceed.

In the Settings application on Ubuntu, there is a pane named Online Accounts. One of the choices is NextCloud.

Simply click on the button, and you're asked to fill in a server URL, a username, and a password.

With that filled in, your filesystem shows up in the sidebar for Nautilis, the default filemanager for Ubuntu. Click on it, and the filesystem mounts. There's an unmount button available making it easy to unmount the filesystem.

The filesystem shows up in the Nautilis file browser. You can right-click to bring up a menu while in the file browser. For example, a Libre Office file can be opened from that menu.

This is not a regular filesystem mount

But, the filesystem does not show up as a regular mount, it does not show up in df -h, and the filesystem is not usable by regular command-line tools.

In Nautilis, the properties dialog for a file mounted this way shows:

davs://USERNAME@cloud.EXAMPLE/remote.php/webdav/PARENT/DIRECTORY/PATH

Whereas, for a file in a regular directory, the properties dialog shows a file-system path.

Go to the command line and run the df -h or mount commands, and you'll see that it hasn't mounted the filesystem.

After some reading, I learned that what we're seeing is the GNOME Virtual File System which is layered on top of the GIO subsystem. The documentation describes some commands like gvfs-mount which handles mounting and unmounting GVFS filesystems. An additional piece is GVFSD, which is a FUSE driver that makes GVFS mounted filesystems appear in the filesystem.

Indeed, look at this:

$ ls -l /run/user/1000/gvfs
total 0
drwx------ 1 david david 0 Mar  3 11:04 'dav:host=cloud ... ssl=true,user-USERNAME ... '

The pathname is /run/user/USER-ID/gvfs, and contains a directory corresponding to the mounted drive. The name of that directory contains mount parameters. In the past, a directory ~/.davfs also contained these directories, but no longer.

Since that directory path is not user-friendly, let's think of how to simplify its use:

$ ln -s /run/user/1000/gvfs gvfs
$ ls -l gvfs
lrwxrwxrwx 1 david david 19 Mar  3 13:19 gvfs -> /run/user/1000/gvfs

In this case you access the files as:

$ ls ~/gvfs/dav\:host\= ...

That simplifies getting to the parent directory holding the GVFS mounts. Another way might be

$ mkdir ~/gvfs
$ ln -s /run/user/1000/gvfs/dav\:host\= ... ~/gvfs/USER@CLOUD-HOST
$ ls ~/gvfs/USER@CLOUD-HOST

This second approach seems a little easier. But, what if the mount directory name in /run/user/NNNNN/gvfs changes? The symbolic link will no longer work.

Mounting NextCloud as WEBDAV using rclone

Rclone supports mounting many types of remote filesystems, and synchronizing files between local and remote filesystems. It can also be used to mount a remote filesystem into a local system. It was discussed in Mounting S3 (compatible) buckets as a filesystem on Linux or macOS using S3FS or Rclone

In rclone, create a new filesystem, and in the list of supported filesystem types you'll find WEBDAV. On my rclone install, that had a code number of 31, and it knew to ask about NextCloud. Enter the WEBDAV URL for your NextCloud instance, then enter the user-name and password. The advanced configuration is not necessary.

This gives you a "remote" name, such as nextcloud-USERNAME. With the rclone GUI, you simply open the remote, and in its window you find a Mount button with which you can mount the filesystem. At the command-line:

$ mkdir -p  ~/rclone/USERNAME@nextcloud
$ rclone mount nextcloud-USERNAME: ~/rclone/USERNAME@nextcloud
2024/03/03 13:47:24 NOTICE: webdav root '': --vfs-cache-mode writes or full is recommended for this remote as it can't stream

This command does not run in the background, but in the foreground. To fix that message, and to also put the command into the background, run this instead:

$ rclone mount nextcloud-USERNAME: ~/rclone/USERNAME@nextcloud --vfs-cache-mode=full &

This shows up in df -h as so:

nextcloud-USERNAME:      1.1P   45M  1.0P   1% /home/david/rclone/USERNAME@nextcloud

The mount is listed in the Nautilis file browser, and regular command-line tools work perfectly.

When to use davfs2, rclone or the Ubuntu Online Accounts feature

The Online Accounts feature is slick. A few clicks, and you're ready to go. But ... Using this online accounts feature, the files are not in the regular filesystem.

If your use-cases involve using regular command-line tools you must use davfs2 or rclone.

If your use-cases are on a headless system, or a system lacking the equivalent to this online accounts feature, you must use davfs2 or rclone.

Otherwise, you can use the online accounts feature.

That we were able to find the gvfs mount directory is useful. But the implementation leaves a lot to be desired.

The online accounts feature is super cool if your use-cases include integrating the NextCloud calendar with the Ubuntu, or the NextCloud contacts with contacts applications on Linux. After setting up the NextCloud online account, calendar entries immediately showed up.

Summary

Three methods were discussed for mounting the NextCloud instance using WEBDAV:

  • davfs2 is a regular filesystem driver which supports entries in /etc/fstab and excellent integration with the regular filesystem and regular tools.
  • rclone is an application for synchronizing files between local and remote drives, which can be used for mounting a remote drive on the local host. This doesn't support adding an entry to /etc/fstab but it otherwise integrates with the regular filesystem and regular tools.
  • The online accounts feature supports mounting NextCloud and several other popular cloud services. It does not integrate with the regular filesystem, nor does it support use by regular applications, unless you happen to find the semi-hidden semi-secret directory.

These techniques have their pros and cons. For myself, I have created a file ~/bin/mounts.txt listing the commands for mounting remote filesystems. When required, I will use rclone to mount NextCloud or the other filesystems. Using rclone is preferred over davfs2 because the rclone mount shows up in the Nautilis sidebar.

I want to like the online accounts feature, but it is not easily integrated with the regular filesystem. As a result, I've disabled mounting Files for the NextCloud online account. I've also removed the /etc/fstab entry using davfs2 to mount NextCloud.

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.