DEV Community

DEV-AI
DEV-AI

Posted on

Mastering Cloud Storage: How to Mount S3 as a Local Filesystem with Rclone

Interacting with cloud object storage like Amazon S3 can sometimes feel disconnected from your local environment. While web consoles and APIs are powerful, nothing beats the convenience of accessing remote files as if they were in a local folder. Rclone is a powerful command-line utility that bridges this gap, allowing you to not only sync and manage files across dozens of cloud providers but also to mount them directly into your filesystem.

This article provides a comprehensive walkthrough of how to configure rclone for any S3-compatible service, mount it as a local directory with intelligent caching, and configure it as a robust, auto-recovering system service on Linux.

Part 1: Initial Rclone Installation and Configuration

Before you can mount a bucket, you need to install rclone and teach it how to access your S3 storage.

Installing Rclone

Rclone offers a simple installation script for Linux and other Unix-like systems. It's the recommended way to ensure you have the latest version [1].

sudo -v ; curl https://rclone.org/install.sh | sudo bash
Enter fullscreen mode Exit fullscreen mode

For other operating systems like Windows, you can download the appropriate binary from the official rclone website [2].

Configuring Your S3 Remote

Rclone uses the concept of "remotes" to store connection details for your various cloud services. The interactive rclone config command makes this setup straightforward [3].

  1. Run rclone config in your terminal.
  2. Select n to create a new remote.
  3. Give it a memorable name (e.g., my-s3).
  4. You'll be presented with a long list of storage providers. Choose the option for "Amazon S3 Compliant Storage Providers" [4].
  5. Next, select the specific provider (e.g., "Amazon Web Services (AWS) S3", "Minio", "Ceph", etc.) [3]. If your provider isn't listed, "Any other S3 compatible provider" is a safe choice.
  6. Follow the prompts to enter your S3 credentials (access_key_id and secret_access_key), the correct region, and the endpoint URL if you are not using AWS. For most prompts, the default values are sufficient.
  7. Once you confirm the summary, your remote is ready. You can test it by listing your buckets:

    rclone lsd my-s3:
    

Part 2: Mounting the Bucket with VFS Caching

The rclone mount command is the core of this setup. To make it performant and reliable, you should leverage its Virtual File System (VFS) caching capabilities. This stores a local cache of recently accessed files, reducing latency and API calls.

The following command mounts your S3 bucket with a 1GB cache that intelligently manages itself:

rclone mount my-s3:your-bucket-name /mnt/s3 \
  --allow-other \
  --vfs-cache-mode full \
  --vfs-cache-max-size 1G \
  --vfs-cache-max-age 48h \
  --log-file /var/log/rclone.log \
  --log-level INFO
Enter fullscreen mode Exit fullscreen mode

Let's break down these essential flags:

  • my-s3:your-bucket-name: Specifies the remote and bucket to mount.
  • /mnt/s3: The local directory where the bucket will be accessible. This directory must exist before running the command.
  • --allow-other: Allows other users on the system (besides the one running the command) to see and access the mount. This is crucial for applications like Plex or web servers [5]. You may need to enable the user_allow_other option in /etc/fuse.conf.
  • --vfs-cache-mode full: The most compatible cache mode. It buffers all reads and writes to disk, making the remote storage behave much like a local drive. This is ideal for compatibility with a wide range of applications [6].
  • --vfs-cache-max-size 1G: Limits the total size of the local VFS cache. When this limit is reached, rclone automatically purges the least recently used files to make space [6].
  • --vfs-cache-max-age 48h: Evicts files from the cache if they haven't been accessed for the specified duration [6].
  • --log-file & --log-level: Directs logging output to a file for easier troubleshooting.

Part 3: Running as a Systemd Service for Reliability and Recovery

Running the mount command from a terminal is fine for temporary access, but for a permanent setup, you need a system service. This ensures the mount starts automatically on boot and, more importantly, restarts automatically if it ever fails.

You can create a systemd unit file to manage the rclone mount process [7][5].

  1. Create the Service File:
    Create a new file at /etc/systemd/system/rclone-mount.service and add the following content. Remember to replace placeholders like your-user, your-group, my-s3, your-bucket-name, and paths to match your setup.

    [Unit]
    Description=Rclone Mount for S3 Bucket (my-s3:your-bucket-name)
    AssertPathIsDirectory=/mnt/s3
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    User=your-user
    Group=your-group
    Environment=RCLONE_CONFIG=/home/your-user/.config/rclone/rclone.conf
    
    ExecStart=/usr/bin/rclone mount my-s3:your-bucket-name /mnt/s3 \
      --allow-other \
      --vfs-cache-mode full \
      --vfs-cache-max-size 1G \
      --vfs-cache-max-age 48h \
      --log-file /var/log/rclone.log \
      --log-level INFO
    
    ExecStop=/bin/fusermount -u /mnt/s3
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
    
  2. Understanding the Service File:

    • [Unit] Section: After=network-online.target ensures that rclone only attempts to mount after the network is active, preventing startup errors [7].
    • [Service] Section:
      • Type=notify: A best practice for rclone. It allows the rclone process to signal systemd once the mount is actually ready [8].
      • User and Group: Specifies the user and group to run the process under. Running as a non-root user is recommended for security.
      • Environment: Explicitly defines the path to the rclone configuration file, as systemd services run in an isolated environment without access to a user's HOME directory [9].
      • ExecStart: The full mount command.
      • ExecStop: The command to cleanly unmount the filesystem when the service is stopped [9].
      • Restart=always & RestartSec=10: This is the key to automatic recovery. If the rclone process ever crashes or is killed, systemd will automatically try to restart it after a 10-second delay [10][7].
    • [Install] Section: WantedBy=multi-user.target tells systemd to start this service during the normal boot process.
  3. Enable and Manage the Service:
    After creating the file, tell systemd to reload its configuration, then enable and start your new service [11].

    # Reload the systemd manager configuration
    sudo systemctl daemon-reload
    
    # Enable the service to start on boot and start it now
    sudo systemctl enable --now rclone-mount.service
    
    # Check the status to ensure it's running without errors
    sudo systemctl status rclone-mount.service
    

    If the status shows "active (running)", your S3 bucket is now mounted at /mnt/s3 and will automatically remount on reboots and recover from failures.

Citations:
[1] Data Migration with rclone https://docs.plusserver.com/en/storage-backup/s3-storage/tutorials/migration-with-rclone/
[2] A Guide to Mounting Amazon S3 Bucket on Windows ... https://www.cloudthat.com/resources/blog/a-guide-to-mounting-amazon-s3-bucket-on-windows-using-rclone
[3] Amazon S3 Storage Providers https://rclone.org/s3/
[4] How to Mount S3 Storage on Windows, MacOS, and Linux https://docs.rumble.cloud/how_to/storage/object/mount_s3_storage.html
[5] Steps and example RClone systemd with mounted Google ... https://gist.github.com/Gyarbij/4dc1fe668b6e7d804b490bebddd3ac80
[6] Best practices and setting for mounting gdrive and s3 ... https://forum.rclone.org/t/best-practices-and-setting-for-mounting-gdrive-and-s3-services-for-streaming-with-plex/42616
[7] Rclone and Systemd - jdedev.org - Serge Kurian - jdedev.org https://jdedev.org/projects/tophomelabwork/docs/linux/rclone/
[8] rclone mount https://rclone.org/commands/rclone_mount/
[9] Mount with systemd - Ubuntu 20.04 https://www.reddit.com/r/rclone/comments/koo3f2/mount_with_systemd_ubuntu_2004/
[10] SYSTEMD RCLONE SERVICE FAILURE https://www.reddit.com/r/rclone/comments/oz3ph8/systemd_rclone_service_failure/
[11] Mount S3 Storage Compatibility As Local Filesystem https://dev.to/nh4ttruong/mount-s3-storage-compatibility-as-local-filesystem-1meh
[12] How to use Rclone to copy local files to S3 and delete ... https://forum.rclone.org/t/how-to-use-rclone-to-copy-local-files-to-s3-and-delete-files-older-than-n-days/40239
[13] Use Rclone to Effortlessly Sync Cloud Files with S3 Storage https://www.youtube.com/watch?v=Bht9wBzyr3o
[14] A Beginner's Guide To Rclone https://www.youtube.com/watch?v=MwxbX6PNiWA
[15] rclone/cmd/serve/s3/serve_s3.md at master · rclone/rclone https://github.com/rclone/rclone/blob/master/cmd/serve/s3/serve_s3.md
[16] Rclone mount not working as a systemd service https://forum.rclone.org/t/rclone-mount-not-working-as-a-systemd-service/42135
[17] Rclone Command Guide https://storj.dev/dcs/third-party-tools/rclone/rclone-s3
[18] How to Mount Amazon S3 as a Drive for Cloud File Sharing https://www.nakivo.com/blog/mount-amazon-s3-as-a-drive-how-to-guide/
[19] Issue setting up rclone in systemd - Help and Support https://forum.rclone.org/t/issue-setting-up-rclone-in-systemd/48840
[20] How to use rclone to download data from S3 https://stackoverflow.com/questions/68909058/how-to-use-rclone-to-download-data-from-s3
[21] Systemd unit file for rclone inactive (dead) at boot https://forum.rclone.org/t/systemd-unit-file-for-rclone-inactive-dead-at-boot/24627
[22] Using rclone to mount a self hosted Minio “s3” bucket in Linux. https://www.reddit.com/r/rclone/comments/xwvhrb/using_rclone_to_mount_a_self_hosted_minio_s3/
[23] Help starting Rclone mount as Windows Service https://forum.rclone.org/t/help-starting-rclone-mount-as-windows-service/48085
[24] Systemd service for mounting rclone on startup https://www.reddit.com/r/linuxquestions/comments/1i0i1wu/systemd_service_for_mounting_rclone_on_startup/
[25] Migrate data from Google Drive to Amazon S3 using Rclone https://aws.amazon.com/blogs/storage/migrate-data-from-google-drive-to-amazon-s3-using-rclone/
[26] rclone serve s3 https://rclone.org/commands/rclone_serve_s3/
[27] GitHub - nilreml/rclone-mount-systemd: Systemd user service template to automate mounting remote file systems using rclone https://github.com/nilreml/rclone-mount-systemd
[28] Systemd service for rclone mounts/docker autostart https://forum.rclone.org/t/systemd-service-for-rclone-mounts-docker-autostart/39171
[29] Nextcloud with S3 using rclone - Jean's Blog https://blog.jeanbruenn.info/2024/04/08/nextcloud-with-s3-using-rclone/
[30] systemd simple vs. systemd (auto)mount vs. fstab https://forum.rclone.org/t/mount-rclone-on-startup-systemd-simple-vs-systemd-auto-mount-vs-fstab-performance-implications/48051
[31] Restart rclone automatically - Help and Support https://forum.rclone.org/t/restart-rclone-automatically/34065
[32] systemd when user logs in, unmounts @ logout https://forum.rclone.org/t/rclone-mount-w-systemd-when-user-logs-in-unmounts-logout/15101
[33] Rclone Mount using systemd https://docs.ultra.cc/rclone/rclone-mount-using-systemd
[34] Automated Backup with Rclone and a systemd Timer https://codingnotions.com/fully-automated-backup-rclone/
[35] systemctl can't auto mount after version 1.50 when boot. (it's normal by manual exec.) · Issue #4791 · rclone/rclone https://github.com/rclone/rclone/issues/4791

Top comments (0)