Systemd is the init system used in Ubuntu and several other Linux distributions. It is used to bootstrap the user space and manage all subsequent processes. In addition to its primary function of initializing the system, systemd also offers the capability to manage mounts and automounts of storage drives. This is a more flexible and modern method than using the /etc/fstab file. This post will guide you on how to mount drives in Ubuntu using systemd.

Create a systemd mount unit

Systemd uses units to manage resources. To mount a drive with systemd, you’ll need to create a mount unit file. Let’s say you have a network drive located at //192.168.1.100/documents and you want to mount it at /mnt/documents.

First, create a new unit file in the /etc/systemd/system directory. The file should be named after the mount point with slashes (/) replaced by dashes (-), and end with .mount. For our example, the file should be named mnt-documents.mount.

sudo nano /etc/systemd/system/mnt-documents.mount

In this file, paste the following configuration:

[Unit]
Description=Mount Documents at boot

[Mount]
What=//192.168.1.100/documents
Where=/mnt/documents
Type=cifs
Options=username=username,password=password

[Install]
WantedBy=multi-user.target

Replace username and password with your credentials. This is a simple unit file and many other options can be configured depending on your needs.

Enable and start the mount unit

Once the unit file is created and configured, you can enable it so that it starts at boot:

sudo systemctl enable mnt-documents.mount

And to mount the drive immediately without rebooting, use the start command:

sudo systemctl start mnt-documents.mount

Using a Credentials File

Just like with the fstab method, you can use a credentials file to securely store your credentials. Create a file with your credentials:

sudo nano /etc/samba/credentials

And add your username and password to the file:

username=username
password=password

Then, modify the Options line in your systemd unit to use the credentials file:

Options=credentials=/etc/samba/credentials

Remember to secure your credentials file:

sudo chmod 600 /etc/samba/credentials

In addition to the mount units explained, systemd introduces the concept of “automount” units. An automount unit configuration corresponds to a filesystem mount point, and it is used to set up on-demand mounting.

Create an Automount Unit

First, create an automount unit file that corresponds to your mount unit file. The file should be named the same as your mount unit, but with a .automount extension. In our example, the file should be named mnt-documents.automount.

sudo nano /etc/systemd/system/mnt-documents.automount

Paste the following configuration into the file:

[Unit]
Description=Automount Documents

[Automount]
Where=/mnt/documents
TimeoutIdleSec=30

[Install]
WantedBy=multi-user.target

This configuration sets up the automount point at /mnt/documents, and automatically unmounts it after 30 seconds of inactivity.

Enable and Start the Automount Unit

Just like with the mount unit, you can enable the automount unit to start at boot:

sudo systemctl enable mnt-documents.automount

And to activate the automount point immediately without rebooting, use the start command:

sudo systemctl start mnt-documents.automount

Using systemd’s automount units, you can set up your system to automatically mount drives on demand, providing flexible and efficient management of your resources.



Buy Me a Coffee