--- title: Automatic connection to an SMB share under Ubuntu 22.04 description: Learn how to connect an SMB share under Ubuntu 22.04 and automatically start it as a systemd service. level: intermediate updated_at: 2023-07-05 slug: setup-smb-autoconnect author_name: Konstantin Protzen author_url: https://github.com/cuzimbisonratte author_image: https://github.com/cuzimbisonratte.png author_bio: tags: [linux, ubuntu, samba, smb, share, network, folder, space, connect, autoconnect, automount, mount, ubuntu22.04, 22.04, vps, server, service, systemd, autostart, startup] netcup_product_url: https://www.netcup.de/bestellen/produkt.php?produkt=2991 language: en available_languages: [en, de] --- # 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: Value | Description --- | --- `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-example` | Password (The password with which you log in to the SMB share server) `username-example` | Username (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: ```bash username=username-example password=password-example ``` Please replace `password-example` with your password and `username-example` with the SMB share username. 5. Exit the file by pressing "Ctrl + X" and then "Y" and then "Enter". 6. 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: ```systemd [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 ``` 3. Replace `smb.example.com/user/` with the name of the server under which the SMB share is accessible. 4. 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. 5. Exit the file by pressing "Ctrl + X" and then "Y" and then "Enter". 6. Enter `systemctl daemon-reload` to reload the systemd daemon. 7. Enter `systemctl enable smb-connect` to enable the service. 8. Enter `systemctl start smb-connect` to start the service. 9. 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](https://github.com/netcup-community/community-tutorials/blob/main/LICENSE) 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.