Files
docudjeex/content/en/2.general/1.networking/3.samba.md
T

225 lines
4.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Samba
description: Set up Samba on Debian to share folders over your local network and access them from Windows, macOS, or Linux.
---
:ellipsis{left=0px width=40rem top=10rem blur=140px zIndex=60}
Samba is a protocol that allows access to a folder located on a network drive. It can be configured on macOS, Windows, or Linux.
There are many tutorials for setting up Samba on Windows or on NAS systems like Synology, but here we focus on Debian.
![samba](/img/global/smb.svg)
## Create and configure a Shared Network Folder
::note
In this example, we will share the `/video` folder from a remote machine called `remote-machine`. We will access this folder from a machine called `local-machine`. The user connecting to the network drive will be `sambauser`.
::
::steps{level="3"}
### Install Samba Server
```bash [Terminal]
sudo apt update && sudo apt upgrade
sudo apt install samba smbclient cifs-utils
```
### Create the `/video` Folder
```bash [Terminal]
sudo mkdir /video
```
### Configure the Share
Now, edit the file `/etc/samba/smb.conf`.
```bash [Terminal]
sudo nano /etc/samba/smb.conf
```
::tip{icon=""}
✨ __Tip:__ You can use [File Browser Quantum](/serveex/files/file-browser-quantum) to navigate and edit your files instead of using terminal commands.
::
Find the `workgroup` variable and name your workgroup (e.g., `workgroup = WORKGROUP`).
Then scroll to the end of the file and add the following configuration:
```properties [smb.conf]
[video]
comment = Video folder
path = /video
writable = yes
guest ok = no
valid users = @smbshare
force create mode = 770
force directory mode = 770
inherit permissions = yes
```
Press :kbd{value="Ctrl+O"}, then :kbd{value="Enter"} to save, and :kbd{value="Ctrl+X"} to exit.
### Done !
::
### Create a Samba User and Group
Since we're using a secured share, we need to create a user and group to access it remotely.
::steps{level="3"}
### Create the group
```bash [Terminal]
sudo groupadd smbshare
```
Give the group control over the `/video` folder:
```bash [Terminal]
sudo chgrp -R smbshare /video
```
Set inherited permissions:
```bash [Terminal]
sudo chmod 2775 /video
```
### Create the user
Now add a no-login user: this user cannot log into the server but can access Samba.
```bash [Terminal]
sudo useradd -M -s /sbin/nologin sambauser
```
Add the user to the `smbshare` group:
```bash [Terminal]
sudo usermod -aG smbshare sambauser
```
Set a Samba password:
```bash [Terminal]
sudo smbpasswd -a sambauser
```
### Enable the Samba account
```bash [Terminal]
sudo smbpasswd -e sambauser
```
### Done !
::
## Accessing a Shared Folder
::steps{level="3"}
### Install Required Packages
```bash [Terminal]
sudo apt update && sudo apt upgrade
sudo apt install cifs-utils
```
### Create the Mount Destination
We will create a folder on our local machine where the remote `/video` folder will be mounted, e.g. `/mnt/video`.
```bash [Terminal]
sudo mkdir /mnt/video
```
### Prepare the .credentials File
To avoid typing our username and password every time, create a `.credentials` file storing the login info.
Create it in the `/smb` folder:
```bash [Terminal]
sudo mkdir /smb
sudo nano /smb/.credentials
```
Write:
```properties [.credentials]
username=smbuser
password=password
```
* `smbuser`: the user we created on the `remote-machine`
* `password`: the password set earlier
Press :kbd{value="Ctrl+O"}, then :kbd{value="Enter"} to save, and :kbd{value="Ctrl+X"} to exit.
Set proper file permissions:
```bash [Terminal]
sudo chmod 600 /smb/.credentials
```
### Mount the Shared Folder
::warning
__Warning:__ If you're using ufw as firewall, you need to add a rule to allow your remote server to access to your share.
```bash [Terminal]
sudo ufw allow from <your-remote-ip> to any app Samba
```
::
Now mount the folder:
```bash [Terminal]
sudo mount -t cifs -o credentials=/smb/.credentials //remote-ip/video /mnt/video
```
Replace `remote-ip` with your `remote-machine`'s IP address.
Verify the mount:
```bash [Terminal]
sudo mount -t cifs
```
Youll see details confirming the mount is successful.
Now you can access the `/video` folder of the `remote-machine` from your `local-machine`!
### (Optional) Auto-mount on Boot
By default, shares aren't auto-mounted after reboot. To automate this, edit the `/etc/fstab` file.
First, back it up:
```bash [Terminal]
sudo cp /etc/fstab /etc/fstab.bak
```
Then add the mount configuration line:
```bash [Terminal]
sudo echo //remote-ip/video /mnt/video cifs _netdev,nofail,credentials=/smb/.credentials,x-systemd.automount,x-systemd.device-timeout=15 0 0 >> /etc/fstab
```
Reboot the machine:
```bash [Terminal]
sudo reboot
```
After rebooting, verify the mount:
```bash [Terminal]
sudo mount -t cifs
```
### And done!
::
::tip
__Unmount the Shared Folder__
```bash [Terminal]
sudo umount -t cifs /mnt/video
```
::