Setup Samba share on Ubuntu

In the following commands change <user_name> and <share_name> to the user you want and the name of the share directory.

Install samba and samba client

sudo apt-get install samba smbclient

Setup Samba user

sudo useradd -m  <user_name>  --shell /bin/false &&  
sudo passwd <user_name>
sudo smbpasswd -a <user_name>

Create Share Directory

sudo mkdir "/home/<user_name>/<share_name>
sudo chown <user_name>:<user_name> /home/<user_name>/<share_name

Make share directory

mkdir /home/<user_name>/<share_name>

Configure Samba conf

Add the following to the bottom of the /etc/smb.conf file. Change the <folder_name>, <user_name> etc to the ones created above.

[<folder_name>] 
path = /home/<user_name>/
<folder_name> valid
users = <user_name>
read only = no

Bash script

You can use the following bash script to automatically install and setup a samba share. Create a file called smb.sh and paste the following in

!/bin/bash

# incredigeek.com
# Ubuntu Samba share auto setup
#
sambaUser="smbuser"
smbFolder="smb_share"
sudo apt-get install samba smbclient
sudo useradd -m ${sambaUser} --shell /bin/false
echo "Enter the password you want to use for the smb user. 4 times."
sudo passwd ${sambaUser}
sudo smbpasswd -a ${sambaUser}
sudo mkdir "/home/${sambaUser}/${smbFolder}"
sudo chown ${sambaUser}:${sambaUser} /home/${sambaUser}/${smbFolder}
sudo echo "[${smbFolder}]" >> /etc/samba/smb.conf
sudo echo "path = /home/${sambaUser}/${smbFolder}" >> /etc/samba/smb.conf
sudo echo "valid users = ${sambaUser}" >> /etc/samba/smb.conf
sudo echo "read only = no" >> /etc/samba/smb.conf
sudo systemctl restart smbd
echo "Samba setup script finished"
echo "Access via $(hostname -I)/${smbFolder} ; username = ${sambaUser} ; password = whatever you put in"

Make executable

chmod +x smb.sh

Execute script

sudo ./smb.sh

Leave a Reply

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