fbpixel
Tags: ,

It is possible to access a folder on a remote machine by installing a Samba server under Linux. This server lets you access a certain file folder from any machine connected to the same network.

Installing Samba

To install Samba,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get update && sudo apt-get install samba -y
sudo apt-get update && sudo apt-get install samba -y
sudo apt-get update && sudo apt-get install samba -y

Check version

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
smbd --version
smbd --version
 smbd --version

Before configuration, check that the Samba service is disabled.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl stop smbd
sudo systemctl stop smbd
sudo systemctl stop smbd

Configuring Samba under Linux

To share a folder securely, you need to enter certain information in the configuration file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak

To modify the configuration file, you can use a text editor or nano in the terminal.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo nano /etc/samba/smb.conf
sudo nano /etc/samba/smb.conf
sudo nano /etc/samba/smb.conf

The information to enter is the path, which is the address of the folder to be shared. Don’t forget to replace with the user’s name. To obtain the user name, enter the command whoami

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[smbSharedFolder]
comment= Folder Shared by Samba Server
path = /home/<USERNAME>/smbSharedFolder
public = yes
read only = no
[smbSharedFolder] comment= Folder Shared by Samba Server path = /home/<USERNAME>/smbSharedFolder public = yes read only = no
[smbSharedFolder]
comment= Folder Shared by Samba Server
path = /home/<USERNAME>/smbSharedFolder
public = yes
read only = no

Restart the Samba service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl restart smbd
sudo systemctl restart smbd
sudo systemctl restart smbd

To check the status of the service and that there are no errors in the configuration file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl status smbd
sudo systemctl status smbd
sudo systemctl status smbd
 

N.B.: Multiple shared folders can be created by adding a configuration block or section (starting with [sharefolder]) to the

Create and modify user rights

To access the Samba shared folder with your user account, you need to add it to the Samba group

sudo smbpasswd -a -a

The system then asks you to enter and confirm the user password.

Check user list

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo pbdedit -L -v
sudo pbdedit -L -v
sudo pbdedit -L -v

Accessing the Samba folder from a remote machine

To access the folder from another computer, you need to know the IP address of the computer on which Samba is installed. To retrieve the IP address, enter the following command from a terminal

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
hostname -I
hostname -I
hostname -I

In Windows Explorer, enter the machine’s IP address, followed by the folder name \.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
\\192.168.1.95\smbSharedFolder
\\192.168.1.95\smbSharedFolder
\\192.168.1.95\smbSharedFolder
samba-access-folder-windows Creating a shared folder with Samba under Linux

It is also possible to access the file using a web browser

samba-access-folder-browser Creating a shared folder with Samba under Linux

Add a log file

It’s not necessary, but I advise you to add a log file so that you can monitor what’s happening with Samba, especially in the event of an error.

In the

Ajouter une section “global” au début du fichier et ajourer la ligne log file. Le paramètre “%m” spécifie de créer un fichier log pour chaque machine qui se connecte au dossier partagé Samba. Le paramètre log level à 1 permet d’activer le logging (defaut à 0).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[global]
log file = /var/log/samba/log.%m
log level = 1
[smbSharedFolder]
comment= Folder Shared by Samba Server
path = /home/<USERNAME>/smbSharedFolder
public = yes
read only = no
[global] log file = /var/log/samba/log.%m log level = 1 [smbSharedFolder] comment= Folder Shared by Samba Server path = /home/<USERNAME>/smbSharedFolder public = yes read only = no
[global]
    log file = /var/log/samba/log.%m
    log level = 1

[smbSharedFolder]
    comment= Folder Shared by Samba Server
    path = /home/<USERNAME>/smbSharedFolder
    public = yes
    read only = no

Use the Ctrl+X shortcut to save and close the file, then restart the service.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl restart smbd
sudo systemctl restart smbd
sudo systemctl restart smbd

To find the log file to view

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ls /var/log/samba/
ls /var/log/samba/
ls /var/log/samba/

To read the log file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat /var/log/samba/log.<IP_CLIENT>
cat /var/log/samba/log.<IP_CLIENT>
cat /var/log/samba/log.<IP_CLIENT>

Bonus: Access the folder using an Alias

Rather than using the IP address, it’s possible to define an alias for the machine. To do this, we’ll find the machine name and modify the file

To find the machine name

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
hostname
hostname
hostname

In the

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo nano /etc/hosts
sudo nano /etc/hosts
sudo nano /etc/hosts
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
127.0.0.1 localhost
127.0.1.1 MyMachine
192.168.1.95 MyMachine
127.0.0.1 localhost 127.0.1.1 MyMachine 192.168.1.95 MyMachine
127.0.0.1 localhost
127.0.1.1 MyMachine
192.168.1.95 MyMachine

To access the folder, enter the following command in Windows Explorer

\MyMachine\smbSharedFolder

samba-access-folder-windows-alias Creating a shared folder with Samba under Linux

Once the alias has been defined, you can create a network location on your computer

In the This PC folder, right-click and select Add network location

windows-add-network-1 Creating a shared folder with Samba under Linux

In the field specified the address of the shared folder

\MyMachine\smbSharedFolder

windows-network-shared-folder-samba-configure Creating a shared folder with Samba under Linux

You can now access your shared folder directly from your Windows browser.

windows-network-shared-folder-samba Creating a shared folder with Samba under Linux

Samba installation and configuration file (Advanced users)

To make life easier, you can create a Samba installation file, which you can run on all your Linux installations.

Don’t forget to update the FOLDERNAME and USER names to match your needs.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo nano install_samba.sh
chmod +x install_samba.sh
sudo ./install_samba.sh
sudo nano install_samba.sh chmod +x install_samba.sh sudo ./install_samba.sh
sudo nano install_samba.sh
chmod +x install_samba.sh
sudo ./install_samba.sh
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#folder user name
FOLDERNAME=sambashare
USER=pi
#install samba
sudo apt-get update && sudo apt-get install samba -y
sudo systemctl stop smbd
#get machine info
HOSTNAME=$(hostname)
IPADDR=$(hostname -I | cut -f1 -d' ')
SMBVER=$(smbd --version)
#create samba folder
mkdir $FOLDERNAME
#save conf file for later
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
#write conf file
#echo > /etc/samba/smb.conf
printf "[global]
#log file = /var/log/samba/log
log level = 1
[$FOLDERNAME]
comment= Folder Shared by Samba Server
path = /home/$USER/$FOLDERNAME
public = yes
read only = no
" > /etc/samba/smb.conf
#create host alias (optional)
echo $IPADDR $HOSTNAME >> /etc/hosts
#add user
sudo smbpasswd -a $USER
#start service
sudo systemctl restart smbd
#display result
echo --------------------- SUCCESS -----------------------
echo SAMBA $SMBVER was installed correcly
echo you can access your folder with
echo \\\\$IPADDR\\$FOLDERNAME
echo or
echo \\\\$HOSTNAME\\$FOLDERNAME
echo with username $USER
echo ------------------------------------------------------
#folder user name FOLDERNAME=sambashare USER=pi #install samba sudo apt-get update && sudo apt-get install samba -y sudo systemctl stop smbd #get machine info HOSTNAME=$(hostname) IPADDR=$(hostname -I | cut -f1 -d' ') SMBVER=$(smbd --version) #create samba folder mkdir $FOLDERNAME #save conf file for later sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak #write conf file #echo > /etc/samba/smb.conf printf "[global] #log file = /var/log/samba/log log level = 1 [$FOLDERNAME] comment= Folder Shared by Samba Server path = /home/$USER/$FOLDERNAME public = yes read only = no " > /etc/samba/smb.conf #create host alias (optional) echo $IPADDR $HOSTNAME >> /etc/hosts #add user sudo smbpasswd -a $USER #start service sudo systemctl restart smbd #display result echo --------------------- SUCCESS ----------------------- echo SAMBA $SMBVER was installed correcly echo you can access your folder with echo \\\\$IPADDR\\$FOLDERNAME echo or echo \\\\$HOSTNAME\\$FOLDERNAME echo with username $USER echo ------------------------------------------------------
#folder user name
FOLDERNAME=sambashare
USER=pi

#install samba
sudo apt-get update && sudo apt-get install samba -y
sudo systemctl stop smbd

#get machine info
HOSTNAME=$(hostname)
IPADDR=$(hostname -I | cut -f1 -d' ')
SMBVER=$(smbd --version)

#create samba folder
mkdir $FOLDERNAME
#save conf file for later
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.bak

#write conf file
#echo > /etc/samba/smb.conf
printf "[global]
    #log file = /var/log/samba/log
    log level = 1

[$FOLDERNAME]
    comment= Folder Shared by Samba Server
    path = /home/$USER/$FOLDERNAME
    public = yes
    read only = no
" > /etc/samba/smb.conf

#create host alias (optional)
echo $IPADDR $HOSTNAME >> /etc/hosts

#add user
sudo smbpasswd -a $USER

#start service
sudo systemctl restart smbd

#display result
echo --------------------- SUCCESS -----------------------
echo SAMBA $SMBVER was installed correcly
echo you can access your folder with
echo \\\\$IPADDR\\$FOLDERNAME
echo or
echo \\\\$HOSTNAME\\$FOLDERNAME
echo with username $USER
echo ------------------------------------------------------

N.B.: If this file doesn’t run correctly after copying and pasting, you can try using dos2unix to convert the file into a

Applications

  • Access your files from anywhere with port forwarding
  • Upload files from one machine to another on the same network

For example, I use it to transfer files from my Windows PC to my Linux PC, which manages my laser engraver.

Sources