Debian 使用 systemd 自动挂载 Samba

本文地址:blog.lucien.ink/archives/540

写成了一键脚本,直接执行即可。

需要注意的是,挂载的路径和 systemd service 的名字要对应上,比如挂载的路径为 /mnt/nas 那 service 的文件名应为 mnt-nas.service

shell 复制代码
#!/usr/bin/env bash

SERVER="nas.local"
USERNAME="foo"
PASSWORD="bar"

apt install cifs-utils -y

groupadd -g 17510 nas

cat <<EOF >> /etc/systemd/system/dns-ready.service
[Unit]
Description=Wait for DNS to come up using 'host'
After=nss-lookup.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c "until host ${SERVER}; do sleep 1; done"

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >> /etc/systemd/system/mnt-nas.mount
[Unit]
Description=Mount NAS
Requires=network-online.target
After=network-online.target dns-ready.service

[Mount]
What=//${SERVER}/Data
Where=/mnt/nas
Type=cifs
Options=username=${USERNAME},password=${PASSWORD},dir_mode=0775,gid=nas,rw,file_mode=0664

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl start dns-ready.service
systemctl enable dns-ready.service

systemctl start mnt-nas.mount
systemctl enable mnt-nas.mount
相关推荐
赵民勇2 天前
Debian包systemd服务处理
debian
轻帆向远2 天前
Debian 旧版源配置指南:国内镜像加速与 archive.debian.org 替代方案
网络·debian·apt
tianyuanwo6 天前
Debian 系统 deb 仓库管理与配置完全指南
debian·deb
WXDcsdn6 天前
新安装的Debian 12系统开启root用户和ssh登录权限
运维·debian·ssh
fiveym7 天前
Debian 12 PXE 安装报错:Bad archive mirror 复合型故障排查全记录
运维·服务器·debian·php
fiveym9 天前
PXE安装Debian报错:GRUB安装失败排查指南
运维·debian
NashSKY10 天前
RK3588 Debian 系统安装与WiFi/SSH配置笔记
debian·ssh·rk3588
小尘要自信10 天前
踩过坑才明白:为什么 ZooKeeper 集群才是正经事
分布式·zookeeper·debian
络合白泽11 天前
Debian 13 + NVIDIA Optimus 笔记本:从零配置 Wayland Explicit Sync 完整指南
运维·debian
键盘上的GG小怪兽GG15 天前
Debian 安装CUPS操作
linux·服务器·debian