This tutorial describes how to set up an SMB share under Ubuntu 22.04 and automatically start it as a systemd service.
This tutorial does NOT describe how to provide an SMB share, but only how to connect (mount) it automatically!
The tutorial was tested on Ubuntu 22.04 (July 2023).
Example values for this tutorial:
|SMB share server (The server on which the SMB share is provided)|
|Mount point (The folder in which the SMB share should be mounted)|
|Password (The password with which you log in to the SMB share server)|
|Username (The username with which you log in to the SMB share server)|
All these values must be replaced by your own values!
First you have to log in to your server via SSH. (If you don't want to use your login user, you can also create a new user and log in with it.)
mkdir smb-share-mountto create the folder for the SMB share.
nano .smbcredentialsand press
Enterto create a new file.
password-example with your password and
username-example with the SMB share username.
chmod 600 .smbcredentialsto protect the access data file.
sudo -sto get root rights.
apt update -y && apt upgrade -y && apt autoremove -y && apt install -y cifs-utils keyutilsto update the server.
Enteronce to continue.
nano /etc/systemd/system/smb-connect.serviceand press
Enterto create a new systemd service file.
[Unit] Description=Automatically connect to SMB share After=network.target [Service] WorkingDirectory=/root/ User=root ExecStart=mount -t cifs -o credentials=/root/smb-share-mount/.smbcredentials //smb.example.com/user/ /root/smb-share-mount/ RemainAfterExit=true Type=oneshot [Install] WantedBy=multi-user.target
smb.example.com/user/with the name of the server under which the SMB share is accessible.
/root/smb-share-mount/(x2) with the path under which the SMB share should be mounted, - This is the folder in which you created the
systemctl daemon-reloadto reload the systemd daemon.
systemctl enable smb-connectto enable the service.
systemctl start smb-connectto start the service.
systemctl status smb-connectto check if the service is running.
You have now set up an SMB share under Ubuntu 22.04 and started it as a systemd service.
Thanks for reading!
Copyright (c) 2023 Konstantin Protzen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.