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
相关推荐
污斑兔5 天前
Debian 普通用户无 sudo 权限问题排查与解决
运维·debian
竹之却7 天前
【Linux】Linux 中 .service 文件核心介绍
linux·运维·服务器·systemd·.service 文件
tobebetter95277 天前
Debian / Ubuntu openclaw 浏览器自动化
ubuntu·自动化·debian
allway29 天前
Debian Regular Expressions
运维·debian·scala
brucelee18610 天前
Debian 安装 Ollama 教程
debian·ollama·open claw
AiGuoHou110 天前
Debian/Ubuntu 各个版本一键更换国内镜像源
linux·ubuntu·国内源·debian·镜像源·换源
木子欢儿16 天前
在 Debian 12 上安装多个版本的 php(7.3、7.4、8.1、8.2)
运维·开发语言·debian·php
無法複制16 天前
debian安装Postgresql-14.x
运维·postgresql·debian
liulilittle21 天前
OPENPPP2静态隧道UDP中断问题排查与解决
网络·网络协议·ubuntu·udp·debian·信息与通信·通信
liulilittle21 天前
Debian/Ubuntu 18.04 上安装 GLIBC 2.28 (2026)
linux·运维·服务器·开发语言·c++·ubuntu·debian