How to Share Files Between Ubuntu and Windows with Samba

Making file sharing work smoothly between different operating systems is an essential task, especially for those of us managing IT systems. 

While Ubuntu and Windows are both powerful platforms, getting them to cooperate requires a bit of setup. This is where Samba steps in. Think of Samba as the key technology that enables Ubuntu and Windows computers to share files and even printers seamlessly, as if they were native to the same network environment. It acts like a translator, fluent in both the Windows networking language (SMB/CIFS – Server Message Block / Common Internet File System) and the protocols used by Linux/UNIX systems. This guide will walk you through configuring Samba on Ubuntu to bridge this gap and simplify cross-platform file sharing.

1. Quick Access: Browsing Windows Shares from Ubuntu Desktop (No Server Setup Needed!)

Before setting up Samba to *share files from* Ubuntu, it’s useful to know that Ubuntu can easily connect to *existing* Windows shares right out of the box using its default file manager. Here’s how:

  1. Open the Files Application:
    Launch the file manager, typically called “Files” (also known as Nautilus). You can find it in the Ubuntu Dock or search for it via the Activities overview (press the Super/Windows key).
  2. Navigate to “Other Locations”:
    Look in the left sidebar of the Files application and click on Other Locations.
  3. Explore “Windows Network”:
    In the main area, you should find an entry labeled Windows Network. Clicking this tells Ubuntu to scan your local network for machines advertising Windows (SMB/CIFS) shares.
  4. Browse and Authenticate:
    Ubuntu will display a list of discovered Windows workgroups and computers. Double-click a computer name to see its shared folders. If a share requires a login, Ubuntu will prompt you for the necessary Windows username and password.

My Experience: This built-in client capability is incredibly handy for quickly grabbing files from a Windows PC on your network without needing to configure anything complex on the Ubuntu side. It’s perfect for typical home or small office environments where you just need to access existing shares.

2. Samba Server vs. Samba Client: Clarifying the Roles

Understanding Samba involves recognizing two distinct roles:

  • Samba Server (handled by the smbd daemon): This component allows your Ubuntu machine to *offer* its own folders and printers as shares accessible to Windows clients (and other Samba clients). Setting up this server role is the main focus of the configuration steps below.
  • Samba Client (using tools like smbclient, mount.cifs from cifs-utils): These tools enable your Ubuntu system to *connect to* and use shares hosted by Windows machines or other Samba servers. The graphical Files application described above uses these client tools under the hood. We’ll touch on command-line client usage later.

3. Installing Samba on Ubuntu: Setting Up the Server Software

To enable your Ubuntu system to share its own folders with the network, you need to install the Samba server packages.

  1. Launch a Terminal:
    A quick way is to press the keyboard shortcut Ctrl+Alt+T.
  2. Install the Samba Packages:
    Run the following commands. Updating package lists first is always a good habit:

    sudo apt update
    sudo apt install samba samba-common -y

    The `apt` package manager will download and install the core Samba server software and related utilities. The `-y` flag automatically confirms the installation.

4. Adjusting the Ubuntu Firewall for Samba Traffic

For other computers to reach your Samba shares, you must configure Ubuntu’s firewall to permit the necessary network traffic. The specific command depends on which firewall management tool you’re using (most commonly `ufw` on desktop Ubuntu).

  • If using ufw (Uncomplicated Firewall – Default on Desktop):
    This is the simplest method. `ufw` has a pre-defined application profile for Samba:

    sudo ufw allow samba

    This command automatically opens the required ports (TCP 139, 445 and UDP 137, 138).

  • If using firewalld (Common on Server Variants/Other Distros):
    You need to add the service or ports explicitly:

    sudo firewall-cmd --permanent --add-service=samba
    sudo firewall-cmd --reload

    Alternatively, add the ports individually:

    sudo firewall-cmd --permanent --add-port=137/udp
    sudo firewall-cmd --permanent --add-port=138/udp
    sudo firewall-cmd --permanent --add-port=139/tcp
    sudo firewall-cmd --permanent --add-port=445/tcp
    sudo firewall-cmd --reload

    The --permanent flag ensures the rules persist after reboot, and --reload applies them immediately.

Important Network Note: If there’s another firewall positioned between your Ubuntu machine and the Windows clients (like a physical hardware firewall for your network, or security group rules in a cloud environment like AWS or Azure), you must also configure *that* firewall to allow traffic on the Samba ports (UDP 137/138, TCP 139/445).

5. Configuring Samba: Editing the smb.conf File

Samba’s primary configuration lives in the /etc/samba/smb.conf file. This text file dictates Samba’s overall behavior, defines which directories are shared, specifies access controls, and sets numerous other operational parameters.

5.1 Setting Up the [global] Section

The [global] section contains settings that apply to the entire Samba server.

  1. Edit the Configuration File:
    Open the file with root privileges using a text editor. nano is user-friendly for terminal editing:

    sudo nano /etc/samba/smb.conf
  2. Locate the [global] Section:
    Scroll down until you find the line starting with `[global]`.
  3. Adjust Essential Global Settings:
    Ensure the following parameters are set appropriately (modify as needed):

    [global]
       ## Browsing/Identification ###
       workgroup = WORKGROUP
       netbios name = UbuntuSamba
       server string = %h server (Samba, Ubuntu)
    
       ## Networking ###
       # (Defaults usually fine unless advanced config needed)
       # interfaces = 127.0.0.0/8 eth0
       # bind interfaces only = yes
    
       ## Logging ###
       log file = /var/log/samba/log.%m
       max log size = 1000
       logging = file # Use file logging instead of syslog
       # panic action = /usr/share/samba/panic-action %d # Default script for critical errors
    
       ## Security ###
       security = user
       encrypt passwords = yes
       # passdb backend = tdbsam # Default password database format
       # obey pam restrictions = yes # Integrate with PAM (Pluggable Authentication Modules)
    
       ## Authentication ###
       # unix password sync = yes # Try to keep Samba/Unix passwords in sync (requires pam setup)
       # passwd program = /usr/bin/passwd %u # Program for changing Unix password
       # passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* # Interaction script for passwd program
       # pam password change = yes # Use PAM for password changes
    
       # map to guest = bad user # Maps logins with incorrect passwords to guest (if guest allowed)
       usershare allow guests = yes # Allows users to create their own shares (usually disabled for servers)
    
       ## Name Resolution ###
       # (Defaults usually fine unless you use WINS)
       # name resolve order = lmhosts host wins bcast
       # wins support = no
       # wins server = w.x.y.z

    Key Parameters Explained:

    • workgroup = WORKGROUP: **Crucial!** This *must* exactly match the workgroup name used by your Windows computers (often ‘WORKGROUP’ or ‘MSHOME’). Case sensitivity might matter.
    • netbios name = UbuntuSamba: Choose a unique, descriptive name for your Ubuntu server as it will appear in the Windows network browser. Keep it relatively short and without spaces or special characters.
    • server string = %h server (Samba, Ubuntu): An optional description displayed next to the server name. %h is replaced by the hostname.
    • security = user: This is the standard and recommended security mode. It requires clients to authenticate with a valid Samba username and password.
    • encrypt passwords = yes: **Essential for security.** Modern Windows versions require encrypted passwords; setting this to `no` will prevent connections.
    • map to guest = bad user: If a user tries to log in with a valid username but the wrong password, Samba can map them to a ‘guest’ account *if* guest access is enabled on a share. Setting this is common, but be mindful of its interaction with `guest ok` in share definitions.
  4. Save and Close:
    In `nano`, press `Ctrl+X`, then `Y` to confirm saving, and `Enter` to write to the same filename.

5.2 Defining a Shared Folder

Now, let’s define a specific directory on your Ubuntu system that you want to make available to Windows users.

  1. Select or Create the Directory:
    Decide which folder you want to share. For this example, we’ll use /srv/samba/shared_docs. If this directory doesn’t exist, create it:

    sudo mkdir -p /srv/samba/shared_docs
    # Optional: Set appropriate Linux permissions (example: allow group 'sambausers' to write)
    # sudo groupadd sambausers
    # sudo chgrp sambausers /srv/samba/shared_docs
    # sudo chmod 2775 /srv/samba/shared_docs # Allows group members to write, sets SGID

    *Note: Proper Linux file permissions are crucial and work alongside Samba permissions.*

  2. Edit smb.conf Again:
    sudo nano /etc/samba/smb.conf
  3. Add Your Share Definition Block:
    Scroll to the very end of the file and add a new section for your share. The name in square brackets (`[SharedDocs]` below) is how the share will appear to Windows clients.

    [SharedDocs]
       comment = Shared Documents Folder
       path = /srv/samba/shared_docs
       browseable = yes
       read only = no       # Equivalent to writable = yes
       guest ok = no        # Disallow access without authentication
       valid users = jdoe @sambausers  # Allow user 'jdoe' and any user in the 'sambausers' group
       # create mask = 0664  # Optional: Permissions for newly created files
       # directory mask = 0775 # Optional: Permissions for newly created directories

    Share Parameters Explained:

    • [SharedDocs]: The name of the share visible on the network.
    • comment: A brief description of the share.
    • path: The full path to the directory on the Ubuntu filesystem being shared.
    • browseable = yes: Allows the share to be listed when users browse the server. Set to `no` to hide it (users would need to know the exact path).
    • read only = no: Allows users to write/modify files (if they also have the necessary Linux file permissions). Use `read only = yes` for read-only access.
    • guest ok = no: Requires users to authenticate with a valid Samba username/password. Set to `yes` to allow anonymous access (use cautiously!).
    • valid users: Specifies which Samba users and/or groups (prefixed with `@`) are allowed to access this share. If omitted, any authenticated Samba user might access it (subject to file permissions).
    • create mask / directory mask: Control the default Linux permissions assigned to new files/directories created via the Samba share. Useful for ensuring consistent permissions.
  4. Save and Close the file.

5.3 Tidying Up: Removing Default Shares

The default `smb.conf` might include share definitions for printers (`[printers]`, `[print$]`) and user home directories (`[homes]`). If you don’t intend to use these features, it’s good practice for security and clarity to disable them by commenting out their entire sections. Place a semicolon (`;`) or hash (`#`) at the beginning of each line within the section you want to disable.

# Example: Commenting out the [homes] share
# [homes]
#   comment = Home Directories
#   browseable = no
#   read only = no
#   create mask = 0700
#   directory mask = 0700
#   valid users = %S

6. Creating Samba Users and Passwords with smbpasswd

**This is a critical step!** Accessing shares configured with `security = user` requires authentication. Samba maintains its *own separate password database* from the standard Ubuntu system accounts. A user must exist as a regular Ubuntu user *first*, and then you need to explicitly add them to the Samba database and set a Samba-specific password using the `smbpasswd` command.

  1. Verify Ubuntu User Exists:
    Make sure the user account you want to grant Samba access to already exists on your Ubuntu system (e.g., check with `id `). You cannot add a non-existent user to Samba.
  2. Add the User to Samba:
    Use the `smbpasswd` command with the `-a` (add) flag, replacing “ with the actual Ubuntu username:

    sudo smbpasswd -a 

    You will be prompted to enter a new password specifically for Samba access for this user. Enter and confirm the password. This password can be the same as, or different from, the user’s Ubuntu login password.

Example adding user `jdoe`:

sudo smbpasswd -a jdoe
New SMB password:
Retype new SMB password:
Added user jdoe.

7. Validating Your Samba Configuration with testparm

Before activating your new Samba configuration, it’s highly recommended to check it for syntax errors or potential issues using the `testparm` utility.

testparm

Running `testparm` will:

  • Parse your /etc/samba/smb.conf file.
  • Report any syntax errors it finds.
  • Warn about potentially problematic settings.
  • Display the loaded configuration (showing defaults for settings you didn’t explicitly define). Press Enter to see the full dump.
Pro Tip: Always run `testparm` after making changes to `smb.conf`. Address any errors or warnings it reports before restarting the Samba services. This can save you troubleshooting time later.

8. Starting/Restarting Samba Services

With your configuration validated and users added, it’s time to start or restart the necessary Samba services to apply the changes.

Use `systemctl` to manage the services:

sudo systemctl restart smbd
sudo systemctl restart nmbd
  • smbd: The core Samba daemon that handles file and printer sharing connections.
  • nmbd: The NetBIOS name service daemon. This helps make your Ubuntu server discoverable by name in the Windows Network environment (handling things like the workgroup browsing).

To ensure Samba starts automatically whenever your Ubuntu system boots up, enable the services:

sudo systemctl enable smbd
sudo systemctl enable nmbd

9. Accessing Your Ubuntu Samba Shares from Windows

Now, head over to a Windows machine on the same network.

  1. Open **File Explorer**.
  2. Click on **Network** in the left-hand navigation pane.
  3. Your Ubuntu server should appear under the “Computer” section, listed by the `netbios name` you set (e.g., `UbuntuSamba`). (Network discovery can sometimes take a minute or two).
  4. **Troubleshooting:** If the server doesn’t appear automatically:
    • Wait a few minutes.
    • Ensure both machines are in the same `workgroup`.
    • Check firewall settings on both Ubuntu and Windows.
    • Try accessing it directly using its IP address or name in the File Explorer address bar: type `\\YourUbuntuServerName` or `\\192.168.x.y` (replace with the actual name or IP).

     

  5. Double-click your Ubuntu server’s icon. You should see the shares you defined (e.g., `SharedDocs`).
  6. Double-click the share name. Windows will likely prompt you for credentials. Enter the **Samba username** and the **Samba password** you set using `smbpasswd`.
  7. You should now have access to the shared folder!

10. Accessing Windows Shares from Ubuntu (Client Usage)

Your Ubuntu machine can also act as a client to access shares hosted on Windows computers.

Option 1: Graphical Access using Files (Nautilus)

This is the same method described in Section 1 for browsing:

  1. Open the **Files** application.
  2. Click **Other Locations** in the sidebar.
  3. Click **Windows Network** to browse, or use the “Connect to Server” field at the bottom.
  4. To connect directly, enter the server address in the format: `smb://WindowsServerName/ShareName` (e.g., `smb://DESKTOP-PC/Documents`).
  5. Enter the required Windows username and password when prompted.

Option 2: Command-Line Access (`smbclient` and Mounting)

For scripting or more advanced usage, you can use command-line tools. You might need to install `smbclient` and `cifs-utils`: `sudo apt install smbclient cifs-utils`

  • Using smbclient:
    • List available shares on a Windows machine:
      smbclient -L 

      Replace `WindowsServerName` with the hostname or IP of the Windows PC, and `WindowsUsername` with a valid user on that machine. You’ll be prompted for the Windows password.

    • Connect interactively to a specific share:
      smbclient 

      This gives you an `smb: \>` prompt similar to an FTP client. You can use commands like `ls` (list files), `cd` (change directory on the share), `get ` (download file), `put ` (upload file), `!` (run local shell command), and `exit`.

  • Mounting a Windows Share using mount.cifs:
    This makes the Windows share appear like a local directory within your Ubuntu filesystem.

    1. Create a local mount point directory:
      sudo mkdir -p /mnt/windows_docs
    2. Mount the share:
      Basic mount command (replace placeholders):

      sudo mount -t cifs 
      • -t cifs: Specifies the Common Internet File System type.
      • /mnt/windows_docs: The local directory where the share will be mounted.
      • -o ...: Mount options.
        • username=...,password=...: Authentication details. **Warning:** Putting passwords directly in commands or scripts is insecure! Use a credentials file (see FAQ).
        • uid=$(id -u),gid=$(id -g): Maps ownership of the mounted files to your current Ubuntu user and group, making them accessible.

         

    3. Access files: Files from the share are now accessible under `/mnt/windows_docs`.
    4. Unmount when done:
      sudo umount /mnt/windows_docs

11. Wrapping Up: Key Steps for Ubuntu-Windows Sharing

Samba is an incredibly versatile tool for bridging the gap between Ubuntu and Windows networks, enabling straightforward file and printer sharing. This guide provided a walkthrough covering:

  • The core idea behind Samba and SMB/CIFS.
  • Installing the necessary Samba server software on Ubuntu.
  • Configuring essential global settings and defining shares in /etc/samba/smb.conf.
  • Setting up Samba-specific user accounts and passwords using `smbpasswd`.
  • Allowing Samba traffic through the Ubuntu firewall (`ufw` or `firewalld`).
  • Validating your configuration using `testparm`.
  • Managing the `smbd` and `nmbd` services with `systemctl`.
  • How Windows clients can connect to your Ubuntu Samba shares.
  • How Ubuntu can connect to Windows shares (via GUI and command line).

Armed with this knowledge, you should be well-equipped to configure reliable file sharing between your Ubuntu and Windows machines, fostering a more integrated and productive network setup.

Frequently Asked Questions (FAQ)

Why is my Ubuntu Samba share not visible on the Windows network?

Several factors can cause this:

  • Workgroup Mismatch: Double-check that the `workgroup =` setting in `/etc/samba/smb.conf` exactly matches the Windows workgroup name.
  • `nmbd` Service Not Running: The NetBIOS name service daemon (`nmbd`) is responsible for network browsing. Verify its status with `sudo systemctl status nmbd`. If stopped, start/restart it: `sudo systemctl restart nmbd`.
  • Firewall Blocking: Ensure both the Ubuntu firewall (`ufw` or `firewalld`) and any external network firewalls allow UDP ports 137 & 138 (for NetBIOS browsing) and TCP ports 139 & 445 (for SMB). For `ufw`, `sudo ufw allow samba` should suffice.
  • Network Discovery Disabled (Windows): Check network discovery settings on the Windows client machine.
  • Slow Discovery: Network browsing can sometimes be slow. Try accessing the share directly via IP address (`\\192.168.x.y`) or hostname (`\\YourUbuntuServerName`) from Windows File Explorer.

Troubleshooting `ubuntu samba share not visible windows 10` often involves checking these points.

Do I have to create a Samba user for every Linux user on the system?

No, you only need to create Samba users (using `sudo smbpasswd -a `) for those specific Ubuntu user accounts that require authenticated access to the Samba shares from Windows or other SMB clients. Users who don’t need network access to shares don’t need a Samba password entry.

How can I make a Samba share accessible without a password (guest access)?

To allow guest/anonymous access to a specific share:

  1. In the share definition within `smb.conf` (e.g., `[PublicShare]`), set `guest ok = yes`.
  2. Ensure the underlying Linux directory permissions allow access for the ‘guest’ user (often mapped to the `nobody` user or another user specified by `guest account =` in the `[global]` section). You might need `sudo chmod -R 777 /path/to/public/share` (use with caution, 777 is very permissive).
  3. Consider setting `map to guest = Bad User` or `Bad Password` in the `[global]` section depending on the desired behavior for failed logins.
  4. Restart Samba: `sudo systemctl restart smbd nmbd`.

Configuring `samba guest access no password` requires careful attention to both Samba settings and Linux permissions. Use guest access sparingly due to security implications.

I’m getting ‘Access Denied’ errors when trying to connect from Windows, even with the right password. What’s wrong?

This is often a Linux file permission issue, not just a Samba configuration problem. Check these:

  • Linux Permissions: Verify that the Ubuntu user account associated with the Samba login has the necessary read, write, and/or execute permissions on the actual shared directory and its contents on the Ubuntu filesystem. Use `ls -ld /path/to/share` and `ls -l /path/to/share` to check. Use `chmod` and `chown` to adjust if needed.
  • `valid users` / `write list`: Double-check the `valid users` parameter in your `smb.conf` share definition. If you need write access, ensure the user is listed or part of a group listed in `write list` (or that `read only = no`).
  • SELinux/AppArmor: If you’re using SELinux or AppArmor (less common on default Ubuntu desktop), security contexts might be blocking access. Check audit logs (`/var/log/audit/audit.log` or `/var/log/syslog`).
  • Restart Samba: Always restart `smbd` and `nmbd` after making changes to `smb.conf` or Linux permissions affecting the share.

Solving `samba access denied ubuntu check permissions` involves looking at both layers.

How do I automatically mount a Windows/Samba share on Ubuntu startup using fstab?

You can add an entry to `/etc/fstab` to mount shares automatically. This requires the `cifs-utils` package (`sudo apt install cifs-utils`).

  1. **Create Credentials File (Recommended & Secure):** Create a file, e.g., `/etc/samba/wincreds`, owned by root with 600 permissions (`sudo chmod 600 /etc/samba/wincreds`):
    username=YourWindowsUser
    password=YourWindowsPassword
    # Optional: domain=YourWindowsDomain
  2. **Create Mount Point:** `sudo mkdir /mnt/winshare`
  3. **Edit fstab:** `sudo nano /etc/fstab`
  4. **Add the fstab Line:** Add a line like this (adjust paths, server, share, uid/gid):
     
    • `//WindowsServerName/ShareName`: Path to the share.
    • `/mnt/winshare`: Local mount point.
    • `cifs`: Filesystem type.
    • `credentials=/etc/samba/wincreds`: Path to your secure credentials file.
    • `uid=1000,gid=1000`: Set the owner/group of the mounted files locally (use `id yourubuntuuser` to find your UID/GID).
    • `iocharset=utf8`: Handles character encoding.
    • `vers=3.0`: Specifies SMB protocol version (e.g., 3.0, 2.1 – use highest compatible).
    • `_netdev`: Tells the system this is a network device and shouldn’t be mounted until the network is up. Crucial to prevent boot delays/errors.
    • `0 0`: Dump/Pass values for fsck (not applicable for network shares).
  5. **Test Mount:** Run `sudo mount -a` to try mounting all fstab entries. Check for errors and if `/mnt/winshare` now contains the share’s content.

Using `mount cifs fstab ubuntu` is the standard way for persistent network shares.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply