Samba Installation

Samba is an open-source software suite that provides file and print services to SMB/CIFS clients. Follow the steps below to install and configure Samba on your Linux system.

Package name: samba
Demon name: smbd
Configuration file: /etc/samba/smb.conf
Default port: 139, 445
Default user: nobody
Default group: nogroup
Default directory: /var/lib/samba/usershares
Default log directory: /var/log/samba
Default configuration directory: /etc/samba

Types of samba access method

Types of samba share

Types of samba user

How to access samba share

  1. Browser: smb://<serverIP>
  2. Terminal access: smbclient //<serverIP>/sharename
  3. Mounting: cisf-utils

samba configuration file commented methods

Samba Installation on Ubuntu

Step 1: Update the System

Before installing Samba, update your system packages:


        sudo apt update 
        sudo apt upgrade -y
    

Step 2: Install Samba

Install Samba using the package manager:


        sudo apt install samba -y
    

Step 3: Verify Installation

Check if Samba is installed correctly:


        samba --version
    

Step 4: Configure Samba

Edit the Samba configuration file to set up shared directories:


        sudo nano /etc/samba/smb.conf
    

Add the following configuration for a shared directory:


        [shared]
        comment = Shared Directory  # Description of the share
        path = /path/to/shared/directory
        browseable = yes
        writable = yes
        guest ok = yes
        read only = no
    

Step 5: Restart Samba Service

Restart the Samba service to apply the changes:


        sudo systemctl restart smbd
    

Step 6: Test the Configuration

Test the Samba configuration for errors:


        testparm
    

Step 7: Access the Shared Directory

Access the shared directory from a Windows or Linux client using the following format:


        \\<server-ip>\shared
        

Step 8: To set Private Samba configuration

To set up a private Samba share, you can create a user and set permissions:


        sudo useradd sambauser # Create a new user for any name
        sudo smbpasswd -a sambauser # Set a password for the Samba user
        sudo chown sambauser:sambauser /path/to/shared/directory # Change ownership to the Samba user
        sudo chmod 770 /path/to/shared/directory    # Set permissions to allow only the owner and group to access
    

Then, modify the Samba configuration file to restrict access:


        [private]
        comment = Private Directory
        path = /path/to/private/directory
        browseable = no
        writable = yes
        valid users = sambauser
        read only = no
    

Restart the Samba service again:


        sudo systemctl restart smbd
    

Step 9: Firewall Configuration

If you have a firewall enabled, allow Samba traffic:


        sudo ufw allow samba
    

Step 10: Check Samba Status

Check the status of the Samba service:


        sudo systemctl status smbd
    

Ensure that the service is active and running.

Samba Installation on Redhat

Step 1: Update the System

Before installing Samba, update your system packages:


        sudo yum update -y
        sudo yum upgrade -y
    

Step 2: Install Samba

Install Samba using the package manager:


        sudo yum install samba samba-client samba-common -y
    

Step 3: Verify Installation

Check if Samba is installed correctly:


        samba --version
    

Step 4: Configure Samba

Edit the Samba configuration file to set up shared directories:


        sudo vim /etc/samba/smb.conf
    

Add the following configuration for a shared directory:


        [shared]
        comment = Shared Directory
        path = /path/to/shared/directory
        browseable = yes
        writable = yes
        guest ok = yes
        read only = no
    

Step 5: Restart Samba Service

Restart the Samba service to apply the changes:


        sudo systemctl restart smb
        sudo systemctl restart nmb
    

Step 6: Test the Configuration

Test the Samba configuration for errors:


        testparm
    

Step 7: Access the Shared Directory

Access the shared directory from a Windows or Linux client using the following format:


        \\<server-ip>\shared
    

Step 8: To set Private Samba configuration

To set up a private Samba share, you can create a user and set permissions:


        sudo useradd sambauser
        sudo smbpasswd -a sambauser
        sudo chown sambauser:sambauser /path/to/shared/directory
        sudo chmod 770 /path/to/shared/directory
    

Then, modify the Samba configuration file to restrict access:


        [private]
        comment = Private Directory
        path = /path/to/private/directory
        browseable = no
        writable = yes
        valid users = sambauser
        read only = no
    

Restart the Samba service again:


        sudo systemctl restart smb
        sudo systemctl restart nmb
    

Step 9: Firewall Configuration

If you have a firewall enabled, allow Samba traffic:


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

Step 10: Check Samba Status

Check the status of the Samba service:


        sudo systemctl status smb
        sudo systemctl status nmb
    

Ensure that the service is active and running.

step 11: If we have SElinux enabled

To set SELinux to allow Samba to share files, you can use the following command:


        chcon -R -t samba_share_t /path/to/shared/directory
    

Samba Clinet side installation:

To install Samba client on Ubuntu, you can use the following command:


    sudo apt install samba-client -y

or


    sudo apt install cifs-utils -y

To install Samba client on Redhat, you can use the following command:


    sudo yum install samba-client -y

or


    sudo yum install cifs-utils -y

Some basic commnad to use in SMB terminal


    smbclient -L //server_ip -U username
    smbclient //server_ip/shared_directory -U username
    
    -> After login
    smb:/> ls                                       # List files and directories
    smb:/> cd directory_name                        # Change directory
    smb:/> get file_name                            # Download file
    smb:/> put local_file_name                      # Upload file
    smb:/> mget file_name1 file_name2               # Download multiple files
    smb:/> mput local_file_name1 local_file_name2   # Upload multiple files
    smb:/> del file_name                            # Delete file
    smb:/> rmdir directory_name                     # Remove directory
    smb:/> mkdir directory_name                     # Create directory
    smb:/> rename old_file_name new_file_name       # Rename file
    smb:/> chmod 755 file_name                      # Change file permissions
    smb:/> chown user_name file_name                # Change file owner
    smb:/> ldc directory_name                       # Change to local directory
    smb:/> exit                                     # Exit smbclient

Samba mount point via CISF commnad


    sudo mount -t cisf //server_ip/shared_directory /mnt/mount_point -o guest   # public share directory
    sudo mount -t cifs //server_ip/shared_directory /mnt/mount_point -o username=username,password=password     # private share directory

To unmount the Samba share, use the following command:


    sudo umount /mnt/mount_point

To mount Samba share automatically at boot, add the following line to /etc/fstab:


    -> Method 1
    //server_ip/shared_directory /mnt/mount_point cifs username=username,password=password,uid=1000,gid=1000,iocharset=utf8 0 0
    OR
    //server_ip/shared_directory /mnt/mount_point cifs username=username,password=password 0 0
    
    -> Method 2 # using credentials file (First create a file with username and password)
    //server_ip/shared_directory /mnt/mount_point cifs credentials=/path/to/credentials_file,uid=1000,gid=1000,iocharset=utf8 0 0
    OR
    //server_ip/shared_directory /mnt/mount_point cifs credentials=/path/to/credentials_file 0 0
    # where credentials_file contains: