Share folder on local network with ubuntu

I try to share folder on ubuntu and connect to it through windows, currently i know how to do it by these steps (which based on GUI): https://linuxhint.com/share-folder-on-local-network-with-ubuntu/

but, since I have a lot of computers I need to do it on, I want to build a bash file that will do it automatically on each computer i will run it, I don't find on internet a way to share the folder with all the following checked (at "local network share picture" below) by a Command Line Interface.

these are the steps I wnat o do using CLI:

  1. Choose the “Local Network Share” selection from the displayed list items: Local Network share picture

  2. check the boxes displayed in the following attached screenshot and then click on the “Create Share” button: Folder sharing picture

Lets say i want to share the following folder: /home/mkdahan/Desktop/Share_Folder

which terminal instruction can make this?

I tried to build a script that will share the folder /home/mkdahan/Desktop/Share_Folder but it still keep the folder "un-shared" as the GUI keep show, even after reboot:


#!/bin/bash

sudo apt-get update
sudo apt-get install samba
sudo apt-get install smbclient

sudo cp /etc/samba/smb.conf ~/home/mkdahan/Desktop/Share_Folder


if sudo grep -Fxq '[Share_Folder]'  /etc/samba/smb.conf
then
    # code if found
    echo the '[Share_Folder] >> /etc/samba/smb.conf' exist at samba.conf
else

    echo [Share_Folder] | sudo tee -a /etc/samba/smb.conf
    echo path =  /home/mkdahan/Desktop/Share_Folder | sudo tee -a /etc/samba/smb.conf
    echo valid users =  salab  | sudo tee -a /etc/samba/smb.conf
    echo read only = no | sudo tee -a /etc/samba/smb.conf
fi


if sudo grep -Fxq 'server min protocol = NT1' /etc/samba/smb.conf
then
    echo the "server min protocol = NT1" exist at /etc/samba/smb.conf
else
    # append after [Global] the line "server min protocol = NT1"
    echo try to write to smb.conf    
    sudo cp /etc/samba/smb.conf /home/mkdahan/Desktop/Share_Folder
    sudo sed -i '/^\[global\]/a\server min protocol = NT1' /home/mkdahan/Desktop/Share_Folder/smb.conf 
    sudo mv /home/mkdahan/Desktop/Share_Folder/smb.conf /etc/samba/smb.conf 


fi

# Restart the samba
sudo service smbd restart
# check your smb.conf for any syntax errors
testparm

also, I see that if I share the folder through the GUI, the smb.conf file don't have the changes the script do, so i believe this is not the right method to do the requiered share (I used this method since this what i found on the NET).

LongStoryShort: How can i do using CLI the two steps can be done using GUI demonstrated above?

No metter what thanks a lot!


Solution 1:

You can create a samba share 2 different ways.

[1] Through the File manager using Local Network Share

This creates a share definition in /var/lib/samba/usershares NOT /etc/samba/smb.conf. It will also add a "share" emblem on the folder being shared.

[2] Through /etc/samba/smb.conf

This will not create a "share" emblem on the folder being shared.

To verify that you created the share without errors you can run:

net usershare info --long for shares created through Local Network Share

testparm -s for shares created in smb.conf itself.

I would advise creating the shares in smb.conf itself since you are altering the base settings there anyway with your "server min protocol" adjustment.

You can use the "net usershare add ..." command to create a share similar to how the File Manager does but I find the syntax awkward: https://ubuntuforums.org/showthread.php?t=2469977&p=14071704#post14071704

Just remember not to share the same folder using both methods at the same time.