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
相关推荐
搬砖的小码农_Sky9 小时前
Linux操作系统:Ubuntu和Debian的区别
linux·ubuntu·debian
孪生质数-2 天前
Linux高危漏洞通报Copy Fail - CVE-2026-31431
linux·运维·服务器·ubuntu·网络安全·debian·cve-2026-31431
Strugglingler2 天前
【systemctl 学习总结】
linux·systemd·systemctl·journalctl·unit file
千百元6 天前
zookeeper启不来了
linux·zookeeper·debian
pray~7 天前
海外Linux Debian环境临时安装依赖包
linux·运维·debian
neo33018 天前
debian MEDIATEK Corp. Device 7925 无线网卡驱动安装
运维·服务器·debian
vortex58 天前
Debian 包管理全指南:从底层 dpkg 到高层 apt 及其日志追踪
linux·运维·debian
chenqianghqu9 天前
debian系统程序自动运行
debian
韩明君11 天前
OpenClaw安全部署实现
linux·人工智能·安全·debian·本地部署·ai agent·openclaw
嘿嘿潶黑黑12 天前
关于Debian源速度慢的解决方法
运维·debian