How to Set Up a Network Shared Folder on Ubuntu With Samba

How to Set Up a Network Shared Folder on Ubuntu With Samba

If you’re looking to allow multiple users to share files over a network using Ubuntu, setting up a Network Shared Folder with Samba is one of the most effective ways to achieve that. Samba is a free software re-implementation of the SMB/CIFS networking protocol, which provides file and print services for various Microsoft Windows clients and can integrate with a Windows Server domain. In this detailed guide, we will break down the setup process step-by-step, providing you with a comprehensive understanding of how to create a network shared folder using Samba in Ubuntu.

1. Understanding Samba

Samba enables seamless file sharing between different operating systems, primarily between Linux and Windows. By using Samba, you can create shared directories that can be accessed by other systems on the same network. It works using the SMB/CIFS protocol, allowing Windows, macOS, and Linux clients to access shared resources.

2. Prerequisites

Before we begin, make sure you have:

  • An Ubuntu operating system installed on your machine (desktop or server edition).
  • Administrative access to the machine (sudo privileges).
  • Basic knowledge of using the terminal.
  • A network connection with IPv4 connectivity.
  • The IP address or hostname of the Ubuntu machine you are going to use for sharing.

3. Installing Samba

The first step in setting up a shared folder is to install the Samba software. Here’s how you can do that:

Step 3.1: Update Your System

It’s good practice to ensure your system is up-to-date. Open your terminal and run:

sudo apt update
sudo apt upgrade -y

Step 3.2: Install Samba

To install Samba, execute the following command:

sudo apt install samba -y

Verify the installation by checking the samba version:

smbd --version

4. Creating a Shared Directory

After installing Samba, you need to create a directory that you want to share over your network. Here’s how:

Step 4.1: Create a Directory

Open the terminal and create a new directory. For example, to create a shared folder named “shared_folder” in your home directory, run:

mkdir ~/shared_folder

Step 4.2: Set Permissions

Set the appropriate permissions for the directory:

chmod 0777 ~/shared_folder

This command gives read, write, and execute permissions to all users, which might be fine in a trusted network but not recommended in untrusted environments.

5. Configuring Samba

Next, you need to configure Samba to share the folder you just created.

Step 5.1: Backup the Samba Configuration

Before making changes, it is always wise to back up the original Samba configuration:

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

Step 5.2: Open Samba Configuration

Open the Samba configuration file in your preferred text editor. Here, we will use nano:

sudo nano /etc/samba/smb.conf

Step 5.3: Add the Shared Folder

Scroll to the bottom of the file and add the following lines:

[SharedFolder]
   path = /home/yourusername/shared_folder
   browseable = yes
   read only = no
   guest ok = yes
   force user = yourusername

Be sure to replace yourusername with your actual username. This configuration does the following:

  • path: Specifies the directory to share.
  • browseable: Allows users to see the shared folder in their file explorer.
  • read only: When set to no, users can write to this directory.
  • guest ok: Allows guest access without a password.
  • force user: Ensures that all files created in the shared folder will be owned by this user.

Step 5.4: Save and Exit

After entering your configuration settings, save the changes and exit the text editor. If you’re using nano, you can do this by pressing CTRL + O to save and CTRL + X to exit.

6. Restart the Samba Service

To apply the changes you made in the configuration file, restart the Samba services using the following commands:

sudo systemctl restart smbd
sudo systemctl restart nmbd

7. Configuring Firewall

If you have a firewall enabled on your Ubuntu system (UFW), you need to allow Samba through the firewall:

sudo ufw allow samba

8. Accessing the Shared Folder

Step 8.1: From a Windows Machine

Now that you have configured Samba on your Ubuntu, you can access the shared folder from a Windows machine on the same network:

  1. Open File Explorer.
  2. In the address bar, type the IP address of your Ubuntu machine in this format: \192.168.X.XSharedFolder, replacing 192.168.X.X with your actual Ubuntu machine IP address.
  3. Press Enter. You should see the contents of the shared_folder.

Step 8.2: From Another Ubuntu Machine

To access the shared folder from another Ubuntu machine, open the file manager and go to Other Locations. Enter the following in the “Connect to Server” field:

smb://192.168.X.X/SharedFolder

Again, replace 192.168.X.X with the actual IP of your Ubuntu server.

9. Configuring Samba User Access

If you want more control over who accesses your shared folders, you can create specific Samba users.

Step 9.1: Create a Samba User

You can add a user to Samba with:

sudo smbpasswd -a yourusername

You will be prompted to enter and confirm a password for the user. The user must already exist on the system.

Step 9.2: Modify the Configuration File

Change the guest ok = yes line to guest ok = no in the smb.conf file, and use valid users = yourusername to specify which users can access the share:

[SharedFolder]
   path = /home/yourusername/shared_folder
   browseable = yes
   read only = no
   guest ok = no
   valid users = yourusername
   force user = yourusername

After saving the changes, restart the Samba services:

sudo systemctl restart smbd
sudo systemctl restart nmbd

10. Troubleshooting Common Issues

Issue 1: Unable to Access Shared Folder

If you’re unable to access the shared folder, ensure that:

  • Network discovery is enabled on the Windows machine.
  • Samba service is running on Ubuntu (sudo systemctl status smbd).
  • You have the correct IP address.

Issue 2: Permissions Denied Error

Ensure the permissions for the shared directory are set correctly. You can modify them as necessary using:

chmod 0777 ~/shared_folder

Issue 3: Firewall Restrictions

Make sure that your firewall settings (UFW) allow Samba traffic. You can check the status using:

sudo ufw status

If it’s inactive, no additional setup is necessary, but if it is active, ensure Samba is allowed through it.

11. Conclusion

Setting up a Network Shared Folder on Ubuntu using Samba is straightforward and provides an effective way to share files across different platforms. By following the steps outlined in this comprehensive guide, you can establish a reliable file-sharing environment that suits your needs. Whether for personal use or within an organization, Samba simplifies collaboration and resource sharing across networks.

Experiment with the different configurations available in Samba to tailor it to your requirements, and remember to maintain security best practices, especially when exposing shared resources on larger or untrusted networks. With Samba, file sharing has never been easier!

Leave a Comment