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
相关推荐
佳腾_2 天前
【操作系统间文件共享_Samba】一、Samba 技术基础与核心功能剖析
文件共享·访问控制·samba·身份验证·网络服务
心随_风动3 天前
Debian GNU/Linux的新手入门介绍
linux·debian·gnu
华纳云IDC服务商3 天前
Debian服务器环境下env变量丢失怎么办
运维·服务器·debian
翻滚吧键盘5 天前
在 Debian 12 中恢复被删除的 smb.conf 配置文件
运维·数据库·debian
林木木木木木木木木木5 天前
【随身WiFi】随身WiFi Debian系统优化教程
linux·运维·debian·随身wifi
CAE虚拟与现实9 天前
Ubuntu和Debian 操作系统的同与异
服务器·ubuntu·debian
极术社区11 天前
【“星睿O6”AI PC开发套件评测】+ Debian 系统安装及 sysbench 跑分对比
debian
撒旦骑路西法,大战吕布11 天前
如果你在使用 Ubuntu/Debian:使用 apt 安装 OpenSSH
linux·ubuntu·debian
2401_8616152812 天前
debian转移根目录
linux·debian·电脑
去看日出12 天前
Linux系统Debian最新版12.10安装详细教程,虚拟机系统安装(附系统镜像资源)
debian·vmware·linux系统