Installing Samba

1
2
apt update
apt install samba

Configuring Samba

Samba’s configuration file is usually located at /etc/samba/smb.conf. You can use a text editor (such as nano or vim) to edit this file. Add shared files.

Remember to back up the file before modifying

1
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
1
2
3
4
5
6
7
8
[shared]
comment = Shared Folder
path = /home/user/shared
valid users = user
read only = no
browsable = yes
create mask = 0777
directory mask = 0777
  • comment: Description of the shared directory.
  • path: Path of the shared directory.
  • valid users: Users allowed to access this shared directory.
  • read only: Set to no to allow writing.
  • browsable: Set to yes to make the shared directory visible in the network.
  • create mask and directory mask: Set the permissions for files and directories.

Creating Samba User

Samba uses its own user database for authentication. You need to create a user for Samba and set a password.

1
2
3
4

adduser user

sudo smbpasswd -a user

The system will prompt you to enter and confirm the password.

Starting and Enabling Samba Service

After configuration is complete, you need to start the Samba service and set it to start automatically on system boot.

On Debian/Ubuntu:

1
2
3
4
sudo systemctl start smbd
sudo systemctl enable smbd
sudo systemctl start nmbd
sudo systemctl enable nmbd

On CentOS/RHEL:

1
2
3
4
sudo systemctl start smb
sudo systemctl enable smb
sudo systemctl start nmb
sudo systemctl enable nmb

Firewall Configuration

If your system has a firewall enabled (such as ufw or firewalld), you need to allow the Samba service through the firewall.

On Debian/Ubuntu (using ufw):

1
sudo ufw allow samba

On CentOS/RHEL (using firewalld):

1
2
sudo firewall-cmd --permanent --add-service=samba
sudo firewall-cmd --reload

Testing Samba Configuration

You can use the testparm command to check for syntax errors in the Samba configuration file.

1
testparm

If there are no errors, the Samba service should be successfully configured and running.

Accessing Shared Directory

On Windows, you can access the Samba shared directory by entering \\<server IP address> in File Explorer. On Linux, you can use smbclient or mount command to access the shared directory.

1
2
3
4
5
6
7
8
9
10
11
12
# Using smbclient
apt install smbclient
smbclient //<server IP address>/shared -U user
# List shared directories
smbclient -L //<server IP address> -U user
# Download files
smbclient //<server IP address>/shared -U user -c 'get <remote file> <local file>'
# Upload files
smbclient //<server IP address>/shared -U user -c 'put <local file> <remote file>'

# Using mount
sudo mount -t cifs //<server IP address>/shared /mnt/shared -o username=user,password=yourpassword

If you encounter any problems, you can check the Samba log files (usually located at /var/log/samba/) for more information.