How to mount the Storage units automatically for Linux(Debian) servers

Reza Azimi
1 min readApr 10, 2021

First step: Choose a permanent directory for placing the Automount.py For example: /etc/automount/

Make a file named automount.py

and put this code in it:

import os
import time

time.sleep(2)
def mount_sdb1():
os.system("sudo mount /dev/sdb1 /mnt" )
def mount_sdb2():
os.system("sudo mount /dev/sdb2 /mnt")

os.system("sudo fdisk -l")
print("fdisk command run")

if True:

mount_sdb1()
print("sdb1 has mount")
else:
mount_sdb2()
print("sdb2 has mount")

Go to /lib/sysstemd/system Directory.

Step 2: We are going to make a system service.

Make a file named : automount.service Write the code bellow down in automount.service:

[Unit]
Description= automount service.
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
Type=simple
ExecStart=python3 /etc/automount/automount.py
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target

cp automount.service /etc/systemd/system/automount.service chmod 644 /etc/systemd/system/automount.service chhmod +x /etc/automount/automount.py

Step3: Now you can start and make the mounter restartable with codes bellow:

sudo systemctl deamon-reaload sudo systemctl start automount sudo systemctl enable automount sudo systemctl restart automount

--

--