2025-06-04 15:04:16 +00:00

227 lines
4.4 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.

---
navigation: true
title: Samba
main:
fluid: false
---
:ellipsis{left=0px width=40rem top=10rem blur=140px}
# Samba
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.
::alert{type="info"}
🎯 __Objectives:__
- Create a network folder on a remote machine
- Access the network folder from our server
::
![samba](/img/global/smb.svg)
## Sharing a Network Folder
---
::alert{type="info"}
:::list{type="info"}
- 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`.
:::
::
### Install Samba Server
```shell
sudo apt update && sudo apt upgrade
sudo apt install samba smbclient cifs-utils
```
### Create the `/video` Folder
```shell
sudo mkdir /video
```
### Configure the Share
Now, edit the file `/etc/samba/smb.conf`.
**Tip:** You can use [File Browser](/serveex/files/file-browser) to navigate and edit your files instead of using terminal commands.
\::
```shell
sudo vim /etc/samba/smb.conf
```
Find the `workgroup` variable, press `i` to enter insert mode, and name your workgroup (e.g., `workgroup = WORKGROUP`).
Then scroll to the end of the file and add the following configuration:
```properties
[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 `Esc` to exit insert mode, then type `:x` and press `Enter` to save and exit.
### 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.
Create the group:
```shell
sudo groupadd smbshare
```
Give the group control over the `/video` folder:
```shell
sudo chgrp -R smbshare /video
```
Set inherited permissions:
```shell
sudo chmod 2775 /video
```
Now add a no-login user — this user cannot log into the server but can access Samba.
```shell
sudo useradd -M -s /sbin/nologin sambauser
```
Add the user to the `smbshare` group:
```shell
sudo usermod -aG smbshare sambauser
```
Set a Samba password:
```shell
sudo smbpasswd -a sambauser
```
Enable the Samba account:
```shell
sudo smbpasswd -e sambauser
```
```shell
sudo ufw allow from remote-ip to any app Samba
::
```
## Accessing a Shared Folder
---
\::
### Install Required Packages
```shell
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`.
```shell
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:
```shell
sudo mkdir /smb
sudo vi /smb/.credentials
```
Enter insert mode (`i`) and write:
```properties
username=smbuser
password=password
```
* `smbuser`: the user we created on the `remote-machine`
* `password`: the password set earlier
Press `Esc`, then `:x` and `Enter` to save and exit.
Set proper file permissions:
```shell
sudo chmod 600 /smb/.credentials
```
### Mount the Shared Folder
Now mount the folder:
```shell
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:
```shell
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`!
### 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:
```shell
sudo cp /etc/fstab /etc/fstab.bak
```
Then add the mount configuration line:
```shell
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:
```shell
sudo reboot
```
After rebooting, verify the mount:
```shell
sudo mount -t cifs
```
And done!
### Unmount the Shared Folder
```shell
sudo umount -t cifs /mnt/video
```