Root-Server Tutorial

ROOT-SERVER TUTORIAL

Automatic connection to an SMB share under Ubuntu 22.04

Learn how to connect an SMB share under Ubuntu 22.04 and automatically start it as a systemd service.

Introduction

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:

ValueDescription
smb.example.com/user/SMB share server (The server on which the SMB share is provided)
/root/smb-share-mount/Mount point (The folder in which the SMB share should be mounted)
password-examplePassword (The password with which you log in to the SMB share server)
username-exampleUsername (The username with which you log in to the SMB share server)

All these values must be replaced by your own values!

Step 1 - Set up SMB share access data

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.)

  1. Move to the directory where you want to mount the SMB share using cd.
  2. Enter mkdir smb-share-mount to create the folder for the SMB share.
  3. Enter nano .smbcredentials and press Enter to create a new file.
  4. Paste the following code into this file:
username=username-example
password=password-example

Please replace password-example with your password and username-example with the SMB share username.

  1. Exit the file by pressing "Ctrl + X" and then "Y" and then "Enter".
  2. Enter chmod 600 .smbcredentials to protect the access data file.

Step 2 - Prepare server

  1. Enter sudo -s to get root rights.
  2. Enter your password and press Enter.
  3. Now enter apt update -y && apt upgrade -y && apt autoremove -y && apt install -y cifs-utils keyutils to update the server.
  4. If a pink window appears, press Enter once to continue.

Step 3 - Set up SMB share

  1. Enter nano /etc/systemd/system/smb-connect.service and press Enter to create a new systemd service file.
  2. Paste the following code into this 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
  1. Replace smb.example.com/user/ with the name of the server under which the SMB share is accessible.
  2. Replace /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 smb-share-mount folder earlier.
  3. Exit the file by pressing "Ctrl + X" and then "Y" and then "Enter".
  4. Enter systemctl daemon-reload to reload the systemd daemon.
  5. Enter systemctl enable smb-connect to enable the service.
  6. Enter systemctl start smb-connect to start the service.
  7. Enter systemctl status smb-connect to check if the service is running.

Conclusion

You have now set up an SMB share under Ubuntu 22.04 and started it as a systemd service.

Thanks for reading!

License

MIT

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.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. 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

  2. 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

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. 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.

Published 05/07/2023 by Konstantin Protzen

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.