How to install and configure Samba on Linux To Share Resource on Network

neotam Avatar

How to install and configure Samba on Linux To Share Resource on Network
Posted on :

Overview

A Samba is a file server or service for Linux to share the files and folders over the network across the operating systems using protocol SMB (Server Message Block). Such that shared files, folders and printers can be accessed from MacOS, Windows, IoS and even from Android. SMB protocol is application layer protocol on top of TCP/IP. Windows Operating system supports the network sharing over SMB out of the box but, for Linux systems need to configure the Samba to share resources over the network.

This article will walk you through, installing, configuring and troubleshooting of samba to share resources

Install Samba

Install samba on Ubuntu (Debian based) operating systems as follows

sudo apt update
sudo apt install samba

Install samba on (Redhat/Fedora)

yum update
yum install samba

Check the installed samba

which samba

Or,

whereis samba 

To enable the service

sudo systemctl enable smbd

Check the status

systemctl status smbd

Setup the Shared Folder for Local Network Share

Create the folder at desired location as follows

mkdir /home/<username>/networkshare/

Change the permissions and ownership of the folder, check the following section on how to create use for Samba

sudo chmod -R 770 networkshare 
sudo chmod -R smbuser:smbgrp networkshare
sudo chcon -t samba_share_t networkshare  #Applicable only if you use SELinux

The folder named “networkshare” will be used to share over the network by configuring samba as follows

To share the created folder “networkshare” we need to add configuration to the samba configuration file usually located at “/etc/samba/smb.conf”. Use your favourite command line editor (vim/nano) to open the file

sudo vim /etc/samba/smb.conf

Add the following configuration to the bottom of the file “smb.conf”

[networkshare]
   comment = Comment about shared folder
   path =  /home/username/networkshare   #absolute path 
   read only = no
   browsable = yes 
   guest ok = no 
   writable = yes

Where,

  • Comment: is the description about the shared folder
  • path: is the location of the folder being shared
  • read only: if this directive is no, permission to modify the content of the shared folder is possible
  • browsable: if yes, default file manager will list this folder under the network section

restart the samba service

sudo service smbd restart 

or,

sudo systemctl restart smbd 

Last but not least, we need to whitelist the samba traffic

sudo ufw allow samba

If SELinux is running, make sure to whitelist the samba on SELinux as well

Setting up User to access shared folder

It is essential to create the user to access the network share(shared folder) from different systems. Samba doesn’t use the system account password such that we need to create the separate password for the desired user

Username must be exist on the system to work

First create the account

sudo adduser smbuser

Assign the samba password to be used, which is different from account password

sudo smbpasswd -a smbuser 

Create a group and add the samba user to that group to own shared files and folders

sudo groupadd smbgrp
sudo usermod -g smbgrp smbuser 

Access the shared folder

To access the shared folder from any platform you need IP address of the system on which folder is located then open the following link in file manager or use connect to server on Mac or Iphone and then enter the IP address with username and password

\\ip-address\networkshare

from other Linux system you can access the shared folder as follows

smb://ip-address/networkshare 

Example Configuration

; /etc/smb.conf
;
; Make sure and restart the server after making changes to this file, ex:
; sudo service smbd start
; sudo service smbd stop
; sudo service smbd restart

[global]
; Uncomment this if you want a guest account
; guest account = nobody
   log file = /var/log/samba-log.%m
   lock directory = /var/lock/samba
   share modes = yes

[myhome]
   comment = Home Directories
   browseable = no
   read only = no
   create mode = 0750

[tmp]
   comment = Temporary file space
   path = /tmp
   read only = no
   public = yes

[private]
path = /home/shared/private
guest ok = no
writable = no
browsable = yes
valid users = @smbgrp 
;accessible  by all users in the group "smbgrp", 
; Share folder must be owned by group "smbgrp"

Troubleshoot if there are any errors in samba configuration using the utility ‘testparm‘ .

Accessing Shared Directories using Samba-Client

It is also possible to access the shared files and folders using command line client “samba-client” on Linux.

Install the samba-client

sudo apt install samba-client

Using samba-client to access files & folders

smbclient '\192.168.4.2\path\shared' -U smbuser 

Trouble Shooting

  • Check the folder permissions, if they are not adequate change permissions using chmod
  • Check owner and group if required change the group to sambashare
  • Check and change the owner of the folder to user who is accessing the directory or add user to sambashare group
    • sudo usermod -a -G groupname username
  • Check the smb.conf directory
    • Did you add browsable ?
    • Did you add proper path to the directory
  • Did you restart the samba service ?
  • Are you sharing the folder from external hard drive? There are known issues with sharing folders from NTFS partition, check !
  • Also, if you are sharing folder from external HDD, make sure proper permissions are set to drive mount point
  • As said there are issues with NTFS filesystem drive, due to the fact that it can’t store file permissions. Install the following dependencies for supporting NTFS
    • apt install smbfs ntfs-3g ntfsprogs
  • If you are experiencing error sharing partition from external HDD and partition file system is of type ext4. Try, mounting the drive at desired location as the user(non root) instead root. If you still can’t access make sure mount point folder is having enough permissions with proper group or user assigned

If shared folder is not properly setup you might get error “you don’t have permissions to access this folder” or “This operation cannot be completed original item <name> cannot be found”

Leave a Reply

Your email address will not be published. Required fields are marked *