> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/servers-configurations-debian/server-message-block-smb.md).

# Server Message Block (SMB)

SMB (Server Message Block) is a network protocol used primarily by Windows-based systems to share files, printers, and other resources with devices on the network. Linux systems can also host and access SMB shares using Samba.

***

To install Samba on a Linux server:

```bash
apt install samba -y
```

To allow SMB traffic through the firewall:

```bash
iptables -A INPUT -p tcp --dport 445 -j ACCEPT
iptables -A INPUT -p tcp --dport 139 -j ACCEPT
systemctl restart iptables
```

***

To start and enable the SMB service:

```bash
systemctl enable smbd.service
systemctl start smbd.service
```

To restart the SMB service after changes:

```bash
systemctl restart smbd.service
```

***

To add a new SMB user:

```bash
smbpasswd -a username
```

***

### Sharing Directories

#### Creating a Shared Directory

1. Create a directory for sharing:

   ```bash
   mkdir /shared_directory
   ```
2. Set appropriate permissions:

   ```bash
   chmod 777 /shared_directory
   ```
3. Edit the Samba configuration file:

   ```bash
   vim /etc/samba/smb.conf
   ```
4. Add the following lines at the end of the file:

   ```ini
   [shared_directory]
   comment = Shared Directory
   path = /shared_directory
   available = yes
   browseable = yes
   read only = no
   ```
5. Restart the SMB service:

   ```bash
   systemctl restart smbd.service
   ```

***

### Restricting Access to Specific Users

1. Create a directory for restricted sharing:

   ```bash
   mkdir /restricted_shared_directory
   ```
2. Set appropriate permissions:

   ```bash
   chmod 770 /restricted_shared_directory
   ```
3. Edit the Samba configuration file:

   ```bash
   vim /etc/samba/smb.conf
   ```
4. Add the following lines at the end of the file:

   ```ini
   [restricted_shared_directory]
   comment = Restricted Shared Directory
   path = /restricted_shared_directory
   available = yes
   browseable = yes
   read only = no
   valid users = username
   ```
5. Restart the SMB service:

   ```bash
   systemctl restart smbd.service
   ```

***

### Creating an Anonymous Share

1. Create a directory for anonymous sharing:

   ```bash
   mkdir /anonymous_share
   ```
2. Set appropriate permissions:

   ```bash
   chmod 777 /anonymous_share
   ```
3. Edit the Samba configuration file:

   ```bash
   vim /etc/samba/smb.conf
   ```
4. Add the following lines at the end of the file:

   ```ini
   [anonymous_share]
   path = /anonymous_share
   browseable = yes
   read only = no
   guest ok = yes
   ```
5. Restart the SMB service:

   ```bash
   systemctl restart smbd.service
   ```
