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
相关推荐
belldeep2 天前
WSL 安装 Debian 12 后,Linux 如何安装 curl , quickjs ?
linux·运维·debian·curl·quickjs
l1t2 天前
Debian上安装PostgreSQL的故障和排除
运维·postgresql·debian
后端木木2 天前
一键 Ubuntu、Debian、Centos 换源(阿里源、腾讯源等)
ubuntu·centos·debian
ergevv3 天前
在 RK3588 上通过 VSCode 远程开发配置指南
vscode·debian·ssh·rk3588·远程开发
XMAIPC_Robot4 天前
基于 NXP + FPGA+Debian 高可靠性工业控制器解决方案
运维·人工智能·fpga开发·debian·边缘计算
行之文8 天前
Debian 11之解决daemon.log与syslog文件占用空间过大问题
运维·debian
行之文8 天前
Debian 11 之使用hostapd与dnsmasq进行AP设置
运维·网络·debian
sz66cm12 天前
Docker基础 -- 构建 RK3588 Debian 根文件系统
docker·容器·debian
darkchink12 天前
[BUG]Debian/Linux操作系统中 安装 curl等软件显示无候选安装(E: 软件包 curl 没有可安装候选)
linux·运维·服务器·debian·bug
fengfeng N12 天前
cannot access ‘/etc/mysql/debian.cnf‘: No such file or directory
数据库·mysql·debian