24/7/365 Support

Using WebDAV for file sharing in CentOS

The Web-based Distributed Authoring and Versioning (WebDAV) open standard can be used for sharing files over the network. It is a popular protocol to conveniently access remote data as an online hard disk. There are a lot of online storage and e-mail providers who offer online space through WebDAV accounts. Most graphical Linux or Windows systems can access WebDAV servers in their file managers out-of-the-box. For other operating systems, there are also free options available. Another big advantage is that WebDAV is running over normal HTTP or HTTPS ports, so you can be sure that it will work in almost any environment, even behind restricted firewalls.

Here, we will show you how to install and configure WebDAV as an alternative for the FTP protocol for your file sharing needs. We will use HTTPS as our communication protocol for secure connections.

To Start With: What Do You Need?

To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice. You will need a working Apache web server with SSL encryption enabled and reachable in your network; see Providing Mail Services for how to install the HTTP daemon, and especially the process of Setting up HTTPS with SSL. Also, some experience working with the Apache config file format is advantageous.

The Process

  1. Create a location for sharing your data and for a WebDAV lock file:
    mkdir -p /srv/webdav /etc/httpd/var/davlock
  2. Since WebDAV is running as an Apache module over HTTPS, we have to set proper permissions to the standard httpd user:
    chown apache:apache /srv/webdav /etc/httpd/var/davlock chmod 770 /srv/webdav
  3. Now, create and open the following Apache WebDAV configuration file:
    vi /etc/httpd/conf.d/webdav.conf
  4. Put in the following content:
    DavLockDB "/etc/httpd/var/davlock"
    Alias /webdav /srv/webdav
    <Location /webdav>
          DAV On
          SSLRequireSSL
          Options None
          AuthType Basic
          AuthName webdav
          AuthUserFile /etc/httpd/conf/dav_passwords
          Require valid-user
    </Location>
  5. Save and close the file. Now, to add a new WebDAV user named john (enter a new password for the user as prompted):
    htpasswd -c /etc/httpd/conf/dav_passwords john
  6.  Finally, restart the Apache2 web server:
    systemctl restart httpd
  7. To test if we can connect to our WebDAV server, you can use a graphical user interface (most Linux file managers support WebDAV browsing) from any client in your network, or we can mount the drive using the command line.
  8. Log in on any client machine as root in the same network as our WebDAV server (on CentOS, you need the davfs2 filesystem driver package to be installed from the EPEL repository, and the usage of file locks must be disabled as the current version is not capable of working with file locks), enter the password for our DAV user account named john, and confirm the self-signed certificate when asked:
    yum install davfs2
    echo "use_locks 0" >> /etc/davfs2/davfs2.conf
    mkdir /mnt/webdav
    mount -t davfs https://<WebDAV Server IP>/webdav /mnt/webdav
  9. Now, to see if we can write to the new network storage type:
    touch /mnt/webdav/testfile.txt
  10. If you’ve got connection problems, check the firewall settings on your WebDAV server for the services http and https, as well as on your client.

How Does It Work?

Here in this process, we showed you how easy it is to set up a WebDAV server for easy file sharing.

So, what did we learn from this experience?

We started our journey by creating two directories: one, where all the shared files of our WebDAV server will live, and one for creating a lock file database for the WebDAV server process. The latter is needed so that users can block access to documents to avoid collisions with others if files are currently modified by them. As WebDAV runs as a native Apache module (mod_dav) that is already enabled by default in CentOS 7, all we need to do is create a new Apache virtual host configuration file, where we can set up all our WebDAV settings. First, we have to link our WebDAV host to the full path of the lock database that is used to track user locks. Next, we defined an alias for our WebDAV sharing folder, which we then configured using a Location directive. This will be activated if someone is using specific HTTP methods on the /webdav path URL. Within this area, we specified that this URL will be a DAV-enabled share, enabled SSL encryption for it, and specified basic user-based password authentication. The user account’s passwords will be stored in a user account database called /etc/httpd/conf/dav_passwords. To create valid accounts in this database file, we then used the Apache2 htpasswd utility on the command line. Finally, we restarted the service to apply our changes.

For testing, we used the davfs filesystem driver, which you need to install on CentOS 7 using the davfs2 package from the EPEL repository. There are many other options available, such as the cadaver WebDAV command-line client (also from the EPEL repository); alternatively, you can access it directly using integrated WebDAV support in a graphical user interface such as GNOME, KDE, or Xfce.

 

Help Category:

What Our Clients Say