How to Set Up Samba File Share on Ubuntu?

Setting up a local network file share using Samba on Ubuntu allows you to seamlessly share files and directories between Ubuntu and other operating systems, such as Windows or macOS, on the same private network. This guide covers the entire process, from installing the necessary Samba packages to configuring a shared directory, managing user permissions, and connecting to the share from a client machine. By following these steps, you will establish a secure, accessible network storage solution tailored for home or office environments.

Step 1: Install Samba

Before configuring the file share, you must update your system’s package repository and install the Samba software.

Open your terminal and execute the following commands:

sudo apt update
sudo apt install samba

To verify that the Samba service is running correctly after the installation completes, check its status:

sudo systemctl status smbd

Step 2: Prepare the Shared Directory

Next, decide which directory you want to share across the network. You can share an existing folder or create a new one dedicated to network storage.

To create a new directory at a public path like /home/sambahome/share, use these commands:

sudo mkdir -p /home/sambahome/share

To ensure users can read and write to this folder, adjust the directory permissions:

sudo chown -R nobody:nogroup /home/sambahome/share
sudo chmod -R 0775 /home/sambahome/share

Step 3: Configure the Samba Settings

Samba relies on a central configuration file located at /etc/samba/smb.conf. It is safest to back up the original configuration file before making any modifications.

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

Open the configuration file in a text editor with administrative privileges:

sudo nano /etc/samba/smb.conf

Scroll to the bottom of the file and add a new configuration block for your shared folder. Replace /home/sambahome/share with your actual directory path if it differs:

[UbuntuShare]
   comment = Samba on Ubuntu
   path = /home/sambahome/share
   read only = no
   browsable = yes
   guest ok = yes
   force create mode = 0664
   force directory mode = 0775

Save and close the file by pressing Ctrl+O, Enter, and then Ctrl+X.

Step 4: Configure User Accounts and Security

If you want to restrict access to specific users rather than allowing guest access, you need to create a dedicated Samba user password. The user must already exist as a standard Linux user on your Ubuntu system.

To add an existing user (e.g., username) to the Samba database and set a network password, run:

sudo smbpasswd -a username

If you choose a secured setup, update your smb.conf block by changing guest ok = yes to guest ok = no, and add a line specifying allowed users:

   valid users = username

Step 5: Restart Samba and Configure the Firewall

For the configuration changes to take effect, restart the Samba services:

sudo systemctl restart smbd
sudo systemctl restart nmbd

If you have the Uncomplicated Firewall (UFW) enabled on your Ubuntu machine, you must allow Samba traffic through the firewall to let external devices connect:

sudo ufw allow samba

Step 6: Connect to the Share from Client Machines

To connect to your newly created share, you first need to find the local IP address of your Ubuntu server. Run ip a or hostname -I in the terminal to locate it.

Connecting from Windows

  1. Open File Explorer.
  2. In the address bar, type \\ followed by your Ubuntu server’s IP address (for example: \\192.168.1.50) and press Enter.
  3. Double-click the UbuntuShare folder. If prompted, enter the Samba username and password created in Step 4.

Connecting from macOS

  1. Open Finder and press Cmd+K to open the “Connect to Server” window.
  2. Type smb:// followed by your Ubuntu server’s IP address (for example: smb://192.168.1.50) and click Connect.
  3. Select Registered User, enter your Samba credentials, and click Connect to mount the folder.