Create a Network Share in Linux using Samba via CLI and access using Samba Client
Setting up the share on Linux
Install Samba:
$ sudo apt-get update
$ sudo apt-get install samba
For creating a secured share, make a system account for authentication to access samba shares, here I named the user “shareuser” in an obvious way you can, of course, pick any name:
$ sudo useradd shareuser
$ sudo passwd shareuser
Check if the user has been added successfully:
$ awk -F':' '{ print $1}' /etc/passwd
Add this account in smbpasswd file to be used by samba authentication:
$ sudo smbpasswd -a shareuser
Configure the “shared” directory (or whatever you choose to name your directory that is to be shared):
$sudo mkdir /opt/shared
Make a safe backup copy of the original smb.conf file to your home folder, in case you make an error:
$ sudo cp /etc/samba/smb.conf ~
Edit Samba configuration file “/etc/samba/smb.conf” and add the following at the very end of the file:
Restart smb and nmb services of the system:
$ systemctl restart smbd.service
$ systemctl restart nmbd.service
Use testparm command to check your smb.conf for any syntax errors:
$ testparm
Thus, we created a shared folder named “shared” with “shareuser” as the username to be used for accessing this folder on another machine.
Access the SMB network share on Linux:
Installing Samba client will give you a CLI to work with your SMB share:
$ sudo apt-get install smbclient
List all shares available on the host using the IP address or hostname:
$ smbclient -L //<HOST_IP_OR_NAME>/<folder_name> -U <user>
Here <user> is the user we created in the host machine. Connect to the shared folder using:
$ smbclient //<HOST_IP_OR_NAME>/<folder_name> -U <user>
Note: The default user group of samba is "WORKGROUP".
You can type ‘h’ to get help with smbclient. For instance ‘ls’ would show the contents of the shared folder:
Troubleshoot Option- If you’re getting permission denied error when trying to modify the content of the shared folder, modify ownership and permission for the folder:
$ sudo chown <username>:<groupname> /<path to shared folder>
$ sudo chmod 770 /<path to shared folder>