Using systemd to mount S3 buckets into your home directory
If you do web development or use cloud storage for backups, you may have heard of Amazon S3 which offers dirt-cheap storage for both private and public use. Data in S3 is split into "buckets", which can be configured individually and may contain folder structures.
Then there's S3FS (S3 File System) that allows accessing S3 just as easily as local files. The few tutorials I found used /etc/fstab to auto-mount buckets, but that's not very smart on a desktop computer with multiple users, each managing buckets of their own. Besides, editing /etc/fstab can be very dangerous to newbies. That's why I looked for a way to achieve the same and more without touching any system-wide configuration. The only part that even needs sudo privileges is installing the S3FS package.
If you've used Linux servers or desktops for some time, you've probably heard of systemd, which allows creating your own background services (daemons) relatively easily. The real beauty about it is that adding user-specific services is equally easy, and in my experience they are more reliable than processes started using the classic "Startup Applications" tool.
I'll walk you through to combine those three building blocks into an automatic service that brings your S3 files right under your home directory on a Linux desktop computer. This should work as-is on any recent Debian, Ubuntu or a derivative operating system, and with some changes on others, too (forget about Windows, though).
Don't get scared away by the length of this guide. It's really quite simple, but I'll take time to explain many details to make this how-to both educative and beginner-friendly.
Don't forget to replace all occurrences of YOUR_BUCKET with the name of your actual S3 bucket. Also, instead of gedit you can use your favorite desktop or terminal text editor.
1. Get yourself a bucket
If you already have a bucket and access credentials for it, you can skip this step.Go to aws.amazon.com/s3, sign up and follow instructions to create a bucket. Write down the AWS root credentials created for you and keep them in a safe place. You can either use those with S3FS or, to enhance account security, create a separate IAM user with a policy limited to accessing your S3 bucket(s). That's beyond the scope of this tutorial, though.
Note which AWS region you choose and write it down along with the name of your bucket, because you'll need them later.
2. Create a mount point for the bucket
Let's create a directory named S3 in your home folder to hold configuration files and mount points for your buckets. This command does both (don't forget to replace YOUR_BUCKET with the actual name of your S3 bucket):mkdir -p ~/S3/YOUR_BUCKET
3. Install S3FS and store credentials
S3FS does all the heavy lifting here, communicating with an S3 server to make remote files behave much like local ones. Let's install it first (if you don't have sudo privileges, ask your administrator to do this):sudo apt install s3fs
Now fire up a text editor to create a hidden file for the AWS credentials you got in step 1:
gedit ~/S3/.credentials
Place your access key ID and secret access key on a single line, separated with a colon. The line should look something like this:
AKIXXXXXXXXXXXXXXXXX:abcdefghijklmnopqrstuvwxyz0123456789
Save the file and close the editor. Let's add a little security by ensuring that no other user on your computer (except a sneaky administrator) can read your credentials:
chmod 0600 ~/S3/.credentials
4. Testing
Now we're ready to test mounting your bucket. Here's the command (note that it's a single long line), but let me explain it first before running:s3fs YOUR_BUCKET ~/S3/YOUR_BUCKET -o passwd_file=~/S3/.credentials,endpoint=eu-west-1,default_acl=public-readendpoint=eu-west-1This part defines the AWS region your bucket resides in. Change it if yours is elsewhere.
default_acl=public-readThis defines that anyone with a URL to your S3 file can download it. If you don't want that and only use S3 for private data such as backups, change this into
default_acl=private. Note that if you decide to change this later, you must save or copy all the files in the bucket again to apply the change. It may be easier with the S3 console, if you have many files.S3FS has a built-in caching mechanism to reduce bandwidth usage. If your S3 files are somewhat large and you access them frequently, you may want to activate the cache by appending this to the command line, without a space:
,use_cache=/tmpThere are many more S3FS and FUSE options you can tinker (and fail) with. See their man pages for more info.
Once you've made sure you've added your bucket name to the command, run it. If you've done everything as instructed, you won't see any error messages, and you can now point your file browser to the bucket directory (S3/YOUR_BUCKET) under your home folder. Copy or create a file there, so you can check if it gets transferred to S3 correctly. Try with a small file first, so it won't take ages to transfer.
Open the S3 console and open the bucket. If your file is shown there, you win! If not, check your credentials, mount point (step 2), log information (
journalctl -xe), etc. and try again. Note that there have been some region-specific problems in S3FS, so it's wise to do some googling, too.If you chose to make your S3 files public, you should also be able to open (or download) the file in a web browser with a URL like this (replace FILE_NAME with the actual name and change "eu-west-1" to the region your bucket is in):
http://s3-eu-west-1.amazonaws.com/YOUR_BUCKET/FILE_NAME
This may work, too, but I'm not sure if subdomain access is available in all AWS regions:
http://YOUR_BUCKET.s3.amazonaws.com/FILE_NAME
To unmount the bucket, use this command:
fusermount -u ~/S3/YOUR_BUCKET5. Creating and starting the systemd unit
Now we're ready to make all this fully automatic. Systemd service configuration files are called units. The directory that hosts user units may not exist by default, so let's create it first:mkdir -p ~/.config/systemd/userThen you can create a unit file:
gedit ~/.config/systemd/user/s3fs.service
Now copy the below content into the editor and once again replace the bolded bits with your own values, and don't forget to append any extra S3FS options you want, such as the one for caching:
[Unit]
Description=S3FS mounts
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/s3fs YOUR_BUCKET ${HOME}/S3/YOUR_BUCKET -o passwd_file=${HOME}/S3/.credentials,endpoint=eu-west-1,default_acl=public-read
ExecStop=/bin/fusermount -u ${HOME}/S3/YOUR_BUCKET
[Install]
WantedBy=default.targetSave the file and close the editor.
Note that systemd commands are not run in a shell (bash, dash, etc.), so some shell features such as tilde (~, shortcut to your home directory) don't work. That's why
${HOME} must be used instead. Also note that executables need full paths.The
network-online.target lines ensure that mounting is not attempted until there's a network connection available.Since the s3fs command won't leave a process running, this unit needs
Type=oneshot and RemainAfterExit=yes. This way systemd considers the service active after the mounting is done.If you want to mount more buckets, simply add more ExecStart and ExecStop lines after the existing ones. Note that each can have different options, most importantly the public/private status. Don't forget to create a mount point for every bucket (see step 2)!
Now let's enable and start the service. Make sure you've first unmounted your bucket(s) as instructed in the end of step 4. Note that in the below systemctl commands "s3fs" refers to the s3fs.service unit file you created, not the s3fs executable.
systemctl --user enable s3fs
systemctl --user start s3fsThat's it! From now on systemd will mount your buckets automatically when you log onto your computer.
If you ever want to unmount the buckets, simply stop the service:
systemctl --user stop s3fsIf you make changes to the unit file later on, run these commands to make sure systemd acknowledges the changes:
systemctl --user daemon-reloadsystemctl --user restart s3fs
Wait, there's more loot!
The usefulness of S3FS isn't limited to backups and publishing files. For example, this can be an easy way to share files between your computer and mobile devices with apps like BucketAnywhere.I'm also thinking of using Nautilus Actions (or actually Caja Actions, as nowadays I primarily use Ubuntu MATE) to add a context menu item for S3 files to copy their public URLs to clipboard (IIRC, the discontinued Ubuntu One service had something similar). That would make sharing files really quick, so stay tuned for more. Edit: Done!
It's a bit confusing that mounted S3 buckets get listed like hard drive partitions in file managers, especially since their icons look like hard drives, and I couldn't figure out how to change them or even hide them, so I'll have to dig deeper.
There's probably much room for improvement, so please leave a comment, if you know something that could make this tutorial better, or if you spot a mistake. Unfortunately I don't have much time for answering questions, so please don't expect much support.
This blog gives a lots of Information, It's Useful,Thanks for the Information
ReplyDeleteAWS Online Training
Thanks. Everything worked except for the last line. The test worked great, I then unmounted and ran systemctl --user enable s3fs with no error.
ReplyDeleteThen on the last line I get the following. (I'm on a raspberry pi btw)
Please help!
pi@piSecurity:~ $ systemctl --user start s3fs
Job for s3fs.service failed because the control process exited with error code.
See "systemctl status s3fs.service" and "journalctl -xe" for details.
pi@piSecurity:~ $ systemctl status s3fs.service
Unit s3fs.service could not be found.
Nevermind,it was a typo in my s3fs.service file. Working great, thanks!
DeleteFor mounting file systems using SystemD, it is better to use Mount units instead of relying on services. This allows SystemD to manage the mount paths automatically, create directories as needed, handle mount dependencies and even auto-mount on access.
ReplyDeleteTo create a mount unit, the unit name end with `.mount` and the begin with the path to the wanted mount directories - where slashes (/) are converted to dashes (-). It is easy to create FUSE mount units with FUSE's (built-in) subtype resolver helper.
The main disadvantage of using SystemD mount units, is that users cannot create mount units, even for FUSE file systems - if you try, it will fail due to limitations of the mount model in Linux (relevant bugs to track may be https://github.com/systemd/systemd/issues/13741 and https://bugs.freedesktop.org/show_bug.cgi?id=73809). So mount units must be system units.
So to mount an s3fs-fuser under `/home/user/S3/YOUR_BUCKET`, one can create this unit file as `/etc/systemd/system/home-user-S3-YOUR_BUCKET.mount` with the following content:
```
[Unit]
Description=S3 Storage
After=network.target
[Mount]
What=YOUR_BUCKET
Where=/home/user/S3/YOUR_BUCKET
Type=fuse.s3fs
Options=passwd_file=/home/user/S3/.credentials,endpoint=eu-west-1,default_acl=public-read
[Install]
WantedBy=multi-user.target
```
You can then enable and start this unit normally.
Thanks for mentioning this. I thought about discussing this alternative, but the post was already pretty long and I specifically wanted to introduce a method that doesn't involve messing with system-wide configurations.
DeleteBTW - as of Ubuntu 22.04 LTS, a user mount unit works great for me. I'm not sure what changed as all relevant bug reports are still listed as "not fixed", this currently works for me (for mounting a Wasabi S3 compatible bucket):
Delete```
# /home/odeda/.config/systemd/user/home-odeda-mnt-wasabi\x2dmedia.mount
[Unit]
Description=The Wasabi S3API personal bucket
[Mount]
What=my-personal-stuff
Where=%h/mnt/wasabi-media
Options=_netdev,umask=0007,uid=1000,gid=1000,use_xattr,use_rrs,profile=wasabi,url=https://s3.eu-central-1.wasabisys.com
Type=fuse.s3fs
```
Here's the status:
```
$ systemctl --user status home-odeda-mnt-wasabi\\x2dmedia.mount
● home-odeda-mnt-wasabi\x2dmedia.mount - The Wasabi S3API personal bucket
Loaded: loaded (/home/odeda/.config/systemd/user/home-odeda-mnt-wasabi\x2dmedia.mount; static)
Active: active (mounted) since Mon 2024-07-29 14:39:36 IDT; 1s ago
Where: /home/odeda/mnt/wasabi-media
What: s3fs
Tasks: 10 (limit: 37951)
Memory: 21.7M
CPU: 147ms
CGroup: /user.slice/user-1000.slice/user@1000.service/home-odeda-mnt-wasabi\x2dmedia.mount
└─326145 s3fs my-personal-stuff /home/odeda/mnt/wasabi-media -o rw,umask=0007,uid=1000,gid=1000,use_xattr,use_rrs,profile=wasabi,url=https://s3.eu-central-1.wasabisys.com,dev,suid
$ mount | grep wasabi
s3fs on /home/odeda/mnt/wasabi-media type fuse.s3fs (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
$ ps aux | grep s3f[s]
odeda 326145 1.2 0.3 1352000 98804 ? Ssl 14:39 0:06 s3fs my-personal-stuff /home/odeda/mnt/wasabi-media -o rw,umask=0007,uid=1000,gid=1000,use_xattr,use_rrs,profile=wasabi,url=https://s3.eu-central-1.wasabisys.com,dev,suid
```
Excellent tutorial! Automating S3 mounts with systemd is a game-changer for reliable cloud storage integration. At DB Computer Solutions Ltd, we value practical guides like this that streamline data workflows. Thanks for sharing!
ReplyDeleteThis is a very informative tutorial on using systemd to automatically mount S3 buckets into a Linux home directory.
ReplyDeleteI found the combination of S3FS and user-level systemd services especially useful for managing cloud storage without changing system-wide configuration.
The step-by-step explanation makes the process easier to understand, particularly for users working with Debian or Ubuntu systems.
The focus on practical and user-friendly solutions also connects with the work of a ui/ux design agency in usa when creating intuitive digital experiences.
A mobile app development company in new york can apply similar principles of reliability and usability when building cloud-connected applications.
These ideas are also relevant to mobile app development houston projects where efficient cloud storage integration can support application functionality.
A mobile app development company in Chicago can benefit from scalable cloud-based approaches when developing applications that manage files and data.
For a mobile app development company in Austin, combining flexible architecture with dependable cloud services can help create efficient digital products.
The same focus on cloud integration and reliable technology is valuable in mobile application development in usa for applications that handle remote data and storage.
Overall, this is a detailed and practical guide that provides useful insights into S3FS, systemd, Linux, and automated cloud storage management.
ui/ux design agency in usa
mobile app development company in new york
mobile app development houston
mobile app development company in Chicago
mobile app development company in Austin
mobile application development in usa
This is a very useful tutorial for anyone working with Linux, cloud storage, and Amazon S3.
ReplyDeleteI found the explanation of combining S3FS with systemd particularly helpful for automating S3 bucket mounts.
The step-by-step approach makes the process easier to understand, especially for users working with Debian or Ubuntu.
The discussion of user-specific systemd services is also a practical alternative to modifying system-wide configuration files.
It is interesting to see how cloud storage can be integrated into everyday desktop workflows.
Businesses exploring mobile application development los angeles can similarly benefit from reliable technology solutions.
A ui ux design company in usa can help create intuitive interfaces for applications that work with complex digital services.
Companies considering mobile app development companies in seattle can combine strong development practices with cloud-based technologies.
Likewise, mobile app development houston and mobile app development services usa can support scalable and dependable applications.
A professional mobile app development company Austin can help businesses turn technical ideas into practical and user-friendly
mobile application development los angeles
ui ux design company in usa
mobile app development companies in seattle
mobile app development houston
mobile app development services usa
mobile app development company Austin
This tutorial on mounting S3 buckets with systemd provides useful technical information for developers and system administrators.
ReplyDeleteI found the explanation helpful, especially for understanding how cloud storage can be integrated into a Linux environment.
The practical approach makes the topic easier to follow for readers working with cloud and server technologies.
A website designing company in delhi can help businesses create professional websites with responsive layouts and useful features.
A best mobile app development company in bangalore can build mobile applications tailored to different business requirements.
A website designing company in noida can provide modern and responsive website solutions for growing businesses.
A best website design company in ahmedabad can help create engaging websites with intuitive navigation and practical functionality.
Businesses can explore website development companies in mumbai for customized and scalable web development solutions.
An enterprise software company can develop technology solutions that support complex business processes and operational needs.
Combining software development services in bangalore with a professional user experience agency can help deliver effective and user-friendly digital products.
website designing company in delhi
best mobile app development company in bangalore
website designing company in noida
best website design company in ahmedabad
website development companies in mumbai
best web development company in lucknow
enterprise software company
software development services in bangalore
This tutorial on mounting S3 buckets with systemd provides useful information for developers and system administrators.
ReplyDeleteThe practical explanation makes it easier to understand how cloud storage can work with Linux systems.
I found the technical approach helpful for readers exploring cloud infrastructure and server management.
A website designing company in delhi can help businesses create professional and responsive websites.
A best mobile app development company in bangalore can develop useful applications for different business requirements.
A website designing company in noida can provide modern website solutions with clean layouts and practical features.
A best website design company in ahmedabad can help businesses build attractive and user-friendly online platforms.
Businesses can explore website development companies in mumbai for customized and scalable web solutions.
An enterprise software company can develop digital solutions that support complex business processes and operational needs.
Combining software development services in bangalore with a professional user experience agency can help create smooth and engaging digital experiences.
website designing company in delhi
best mobile app development company in bangalore
website designing company in noida
best website design company in ahmedabad
website development companies in mumbai
best web development company in lucknow
enterprise software company
software development services in bangalore