Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

Samba on Ubuntu: Share Files Over a Local Network

Set up a Samba file share on Ubuntu in six steps, connect from Windows, macOS and Linux, add a guest share, and keep the share off the internet.

2 min readBy Daniel

Reviewed by the AmazingRDP technical team

Sharing files with Samba on Ubuntu

Need files reachable from anywhere, not only inside one office? A Linux VPS with SFTP does that safely. See Linux VPS from $6.99

Samba makes an Ubuntu machine speak the Windows file-sharing language, SMB. A folder on Ubuntu then appears as a network drive on Windows PCs, Macs and other Linux machines in the same network, with user names and passwords. Setting it up takes about ten minutes.

1. Install Samba

sudo apt update
sudo apt install samba

2. Create the folder to share

sudo mkdir -p /srv/samba/shared
sudo chown -R $USER:$USER /srv/samba/shared

3. Describe the share

Open the configuration file:

sudo nano /etc/samba/smb.conf

Add this block at the end, with your own user name:

[shared]
   path = /srv/samba/shared
   browseable = yes
   read only = no
   valid users = alex
   create mask = 0664
   directory mask = 0775

The name in square brackets is what other computers see. valid users lists who may open it.

4. Give the user a Samba password

Samba keeps its own passwords, separate from the Linux login. The user must already exist in Linux.

sudo smbpasswd -a alex

5. Check the file and restart

testparm
sudo systemctl restart smbd

testparm reads the configuration and reports mistakes before they cause trouble.

6. Allow it in the firewall

sudo ufw allow from 192.168.1.0/24 to any app Samba

Use your own network range. This lets computers in your network in, and nobody else.

Connect

FromHow
WindowsIn File Explorer's address bar, type \\192.168.1.20\shared and sign in. To keep it, right-click This PC, Map network drive.
macOSIn Finder, Go, Connect to Server, enter smb://192.168.1.20/shared.
Linux desktopIn the file manager, open smb://192.168.1.20/shared.
Linux terminalsmbclient //192.168.1.20/shared -U alex

Replace the address with your Ubuntu machine's; ip -br addr shows it.

A share for several people

Create a group, add the users, and give the group the folder:

sudo groupadd office
sudo usermod -aG office alex
sudo chgrp -R office /srv/samba/shared
sudo chmod -R 2775 /srv/samba/shared

In the share block, write valid users = @office. Each person still needs their own smbpasswd -a.

A read-only guest share

For files everyone in the network may read without a password:

[public]
   path = /srv/samba/public
   guest ok = yes
   read only = yes

Current Windows versions refuse guest shares by default, for safety. A share with a password is the dependable choice.

Problems and fixes

  • Windows asks for the password again and again. The user has no Samba password (step 4), or Windows is sending your PC's user name. Enter it as UBUNTUNAME\alex, with the Ubuntu machine's name in front.
  • You can open the share but not write. The Linux permissions on the folder do not allow it. Check with ls -ld /srv/samba/shared.
  • The share does not appear in "Network". Browsing is unreliable on current Windows; type the address directly.

Keep SMB inside your network

Never open the Samba ports (445 and 139) to the internet. They are scanned constantly, many internet providers block them, and the protocol was not designed for hostile networks. To reach files on a server from outside, use SFTP, which every SSH server already offers, or connect through a VPN first. For that kind of server, see our Linux VPS plans. Related guides: open or block a port in the Ubuntu firewall, set a static IP on Ubuntu Server and transfer files with WinSCP.

Questions

What is Samba used for?

Sharing folders and printers from a Linux machine with Windows, macOS and other Linux computers, using the SMB protocol.

Which ports does Samba use?

TCP 445, and 139 for older clients. Open them only inside your own network.

Why can Windows not connect to my Samba share?

Usually the user has no Samba password, the firewall blocks port 445, or Windows refuses a guest share.

Is it safe to use Samba over the internet?

No. Use SFTP or a VPN for access from outside.