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
相关推荐
木子欢儿1 天前
在 Debian 12 上安装多个版本的 php(7.3、7.4、8.1、8.2)
运维·开发语言·debian·php
無法複制2 天前
debian安装Postgresql-14.x
运维·postgresql·debian
liulilittle6 天前
OPENPPP2静态隧道UDP中断问题排查与解决
网络·网络协议·ubuntu·udp·debian·信息与通信·通信
liulilittle6 天前
Debian/Ubuntu 18.04 上安装 GLIBC 2.28 (2026)
linux·运维·服务器·开发语言·c++·ubuntu·debian
csdn_life1810 天前
# Debian 10 升级到 Debian 13 指南
运维·debian·php
k7Cx7e10 天前
Debian安装 curl 时提示插入 DVD 光盘
运维·windows·debian
k7Cx7e11 天前
Debian执行ssh root@localhost 提示认证失败
运维·debian·ssh
欧云服务器24 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian
~远在太平洋~24 天前
Debian系统如何删除多余的kernel
linux·网络·debian
~远在太平洋~24 天前
debian系统已安装python3.12却无法执行python命令
chrome·python·debian