logo

dmpop

Aircraft photography, spaghetti code, half-baked thoughts

Guestbook Reads
Currently reading: Gli occhi di Venezia, Alessandro Barbero


Automatically mount a storage device with a systemd service

Say, you want to use a USB storage device with your Linux home server. All you have to do is to run the mount /dev/sdbX /mount/point/ command as root to mount the device (sdbX, in this case) in the specified mount point. Easy, but hardly practical — because you have to do it every time you reboot the server. One way to solve the problem is to create a systemd service that automatically mounts the device on boot.

To do this, use the lsblk command to find the name of the storage device. Create then a /etc/systemd/system/mountdev.service file, open it for editing, and specify the following service (replace sdbX with the actual name of the device and /mount/point/ with the directory where the device should be mounted):

[Unit]

Description=Mount external storage
[Service]

ExecStart=mount /dev/sdbX /mount/point/
[Install]
WantedBy=multi-user.target

Save the file, and run the command below as root to enable the service:

systemctl enable mountdev.service

That's all there is to it. Next time you reboot the server, the storage device will be mounted automatically.


Published: 2024-10-29Reads: 386
Back
dmpop

dmpop

Linux user, amateur photographer, aviation enthusiast, clueless developer of ADS-B station, Pellicola and Tinble, author of a bunch of books. I can mix a decent Negroni, too. You can reach me at me@dmpop.xyz