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
相关推荐
greenery1 小时前
debian 12 安装 NVIDIA 390驱动记录
debian·nvidia·飞牛
zeandon1 小时前
VMWare安装Debian操作系统
debian
zeandon5 小时前
Debian系统终端输入ifconfig报错
debian
翻滚吧键盘8 小时前
配置终端代理
debian
yellow俊的linux之路11 小时前
debian 12 系统容器更换阿里源和用户权限
运维·debian
Christal_pyy2 天前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
fareast_mzh5 天前
Build OpenSSL 1.1.1w from source on Debian 11 (x86_64),
运维·debian
sjh21006 天前
学习笔记之debian的thonny开发(尚未验证)--从stm32裸机到linux嵌入式系统
linux·stm32·嵌入式硬件·debian·硬件工程
机器人梦想家7 天前
如何启动 Linux Debian/Ubuntu 等 SSH 服务器
linux·debian
啊略略wxx9 天前
debian和ubuntu安装python3.8并修改默认python版本
运维·ubuntu·debian