> 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-centos7/server-message-block-smb.md).

# Server Message Block (SMB)

SMB (Server Message Block) is a network protocol commonly used by Windows-based computers. It allows sharing of files, printers, and other resources across a network.

***

To install the SMB server:

```bash
yum install samba*
```

The main configuration file for SMB is:

```bash
vim /etc/samba/smb.conf
```

***

### Port Allow in Iptables

To allow the SMB port (445) in the firewall:

1. Edit the iptables configuration:

   ```bash
   vim /etc/sysconfig/iptables
   ```
2. Add the following line:

   ```bash
   -A INPUT -p tcp --dport 445 -j ACCEPT
   ```
3. Restart iptables:

   ```bash
   systemctl restart iptables
   ```

***

### Start SMB Service

To start and enable the SMB service:

```bash
systemctl restart smb.service
```

```bash
systemctl enable smb.service
```

***

### Add User in SMB

To add a user to the SMB service:

```bash
smbpasswd -a Add_User_name
```

***

### Share Directory

#### 1. Create a Share Directory

Create the directory you want to share:

```bash
mkdir /share_directory_name
```

#### 2. Change Directory Permissions

Change the permissions of the directory to allow full access:

```bash
chmod 777 /share_directory_name
```

#### 3. Edit Configuration File

Open the SMB configuration file:

```bash
vim /etc/samba/smb.conf
```

Add the following lines at the end of the file:

```bash
[share_directory_name]
    comment = display_name
    path = /path/to/share_directory
    public = yes
    writable = yes
```

#### 4. Restart Samba Service

To apply the changes, restart the SMB service:

```bash
systemctl restart smb.service
```

***

### Share Directory for Valid Users

#### 1. Create a Share Directory

Create the directory to be shared:

```bash
mkdir /share_directory_name
```

#### 2. Change Directory Permissions

Change the directory permissions:

```bash
chmod 777 /share_directory_name
```

#### 3. Edit Configuration File

Edit the SMB configuration file:

```bash
vim /etc/samba/smb.conf
```

Add the following lines to the end of the file:

```bash
[share_directory_name]
    comment = display_name
    path = /path/to/share_directory
    public = yes
    writable = yes
    valid users = user_name
```

#### 4. Restart Samba Service

Restart the SMB service:

```bash
systemctl restart smb.service
```

***

### Share Directory for Anonymous Users

#### 1. Create an Anonymous Share Directory

Create the directory for anonymous sharing:

```bash
mkdir /anonymous_share_directory_name
```

#### 2. Change Directory Permissions

Set the appropriate permissions for the directory:

```bash
chmod 777 /anonymous_share_directory_name
```

#### 3. Change Directory User and Group

Set the user and group for the directory:

```bash
chown nobody:nobody anonymous_share_directory
```

#### 4. Edit Configuration File

Edit the SMB configuration file:

```bash
vim /etc/samba/smb.conf
```

Add the following lines at the end of the file:

```bash
[share_directory_name]
    path = /path/to/anonymous_share_directory
    writable = yes
    guest ok = yes
    guest only = yes
    read only = no
    create mode = 0777
    directory mode = 0777
    force user = nobody
```

#### 5. Restart Samba Service

Restart the SMB service:

```bash
systemctl restart smb.service
```
