Install and Configure Samba Server on Ubuntu for File Sharing

In this tutorial, we are going to learn how to install and configure a Samba server on Ubuntu 22.04/20.04 to share files on the local network. Samba is a free and open source SMB/CIFS protocol implementation for Unix and Linux that allows file and printer sharing between Unix/Linux, Windows, and macOS machines on a local area network.

Samba usually installs and runs on Linux. It comprises several programs that serve different but related purposes, the two most important of which are

: smbd: provides

  • SMB/CIFS (file sharing and printing) service, it can also act as a Windows domain controller. nmbd:
  • This daemon provides the NetBIOS name service, listens for requests from the name server. It also allows the Samba server to be found by other computers on the network.

How to Install

Samba Server on Ubuntu

Samba is included in most Linux distributions. To install Samba on Ubuntu, simply run the following command in the terminal.

sudo apt install samba samba-common-bin

The latest stable version available is 4.12.0, released on March 03, 2019. To check your Samba

version, run smbd -version

Sample output

: Version 4.7.6-Ubuntu

To check if the Samba service is running, run the

following command. systemctl smbd status nmbd

To start these two services, issue the following command

: sudo systemctl start smbd nmbd

Once started, smbd will be listening on TCP ports 139 and 445. nmbd will listen on UDP ports 137 and 138. TCP

  • 139: Used for file and printer sharing and other operations
  • .

  • TCP 445: The CIFS port without NetBIOS
  • . UDP 137: Used for NetBIOS

  • network browsing. UDP 138: Used for
  • the NetBIOS name service.

If you have enabled UFW firewall on Ubuntu, you need to open the above ports on the firewall with the following command

. sudo ufw allow samba

Create

a Samba private share

In this section, we’ll look at how to create a Samba private share that requires the client to enter the username and password to gain access. The main Samba configuration file is located at: /etc/samba/smb.conf. You can edit it in the terminal with a command-line text editor such as nano.

sudo nano /etc/samba/smb.conf In the

[global] section, make sure that the workgroup setting is the same as the workgroup settings on Windows computers

. Workgroup = WORKGROUP <img src="https://www.linuxbabe.com/wp-content/uploads/2017/01/samba-share-ubuntu.png" alt="samba

share ubuntu

” />

You can find the settings on your Windows computer by going to Control Panel > System and System Security > System.

<img src="https://www.linuxbabe.com/wp-content/uploads/2017/01/samba-ubuntu-server.png" alt="samba Ubuntu Server" />

Then scroll down to the bottom of the file. (In the nano text editor, you can achieve this by pressing CTRL+W and then CTRL+V.) Add a new section like the following.

[Private] comment = you need username and password to access the path = /srv/samba/private/ browseable = yes guest ok = no writable = yes valid users = @samba

Explanation: Private

is the name of the folder

  • that will be displayed on the Windows network
  • . The

  • comment is a description of
  • the shared folder.

  • The path parameter specifies the path to the shared folder. I use /srv/samba/private/ as an example. You can also use a folder in the home directory.
  • browseable = yes: Allows other computers on the network to view the Samba server and Samba share. If set to no, users must know the name of the Samba server and then manually enter a path in the file manager to access the shared folder.
  • Guest OK = No: Disables guest access. In other words, you need to enter the username and password on the client computer to access the shared folder.
  • writable = yes: Grants read and write permission to clients.
  • Valid users = @samba: Only users in the Samba pool can access this Samba share.

Save and close the file. (To save the file in the nanotext editor, press Ctrl+O, and then press Enter to confirm the name of the file to write. To close the file, press Ctrl+X.) Now we need to create a Samba user. First, we need to create a standard Linux user account with the following command. Replace the user name with the desired user name.

sudo adduser

username You will be prompted to set a Unix password. After that, you also need to set a separate Samba password for the new user with the following command:

sudo smbpasswd -a username

Create the

samba group. sudo groupadd samba

And add this user to the

samba group. sudo gpasswd -a samba username

Create the private shares

folder. sudo mkdir -p /srv/samba/private/

The samba group needs to have read, write and execute permission on the shared folder. You can grant these permissions by running the following command. (If your system does not have the setffl command, you must install the acl package with sudo apt install acl.)

sudo setfacl -R -m “g:samba:rwx” /srv/samba/private/

Then run the following command to check for syntactic errors

. testparm

Now all that’s left to do is

restart smbd and nmbd daemon. sudo systemctl restart smbd nmbd How to create a Samba public share without

authentication To create a public share without requiring a

user

name and password, the following conditions must be met

.

  • Set security = user in the global section of the Samba configuration file. Although you can create a public share with security=share mode, this security mode is deprecated. It is strongly recommended that you avoid sharing mode.
  • Set map to guest = bad user in the global section of the Samba configuration file. This will cause smbd to use a guest account to authenticate clients that do not have an account registered on the Samba server. As it is a guest account, Samba customers do not need to enter the password.
  • Set guest ok = yes in the share definition to allow guest access.
  • Grant read, write, and execute permission from the public folder to anyone’s account, which is the default guest account.

In fact, the first two conditions are already met, as Samba uses these two settings by default

.

Here’s a step-by-step guide to creating a public share. First, open and edit the Samba configuration file.

sudo nano /etc/samba/smb.conf In the

[global] section, make sure that the workgroup setting is the same as the workgroup settings on Windows computers

. Workgroup = WORKGROUP <img src="https://www.linuxbabe.com/wp-content/uploads/2017/01/samba-share-ubuntu.png" alt="samba

share ubuntu

” />

You can find the settings on your Windows computer by going to Control Panel > System and System Security > System.

<img src="https://www.linuxbabe.com/wp-content/uploads/2017/01/samba-ubuntu-server.png" alt="samba Ubuntu Server" />

Then scroll down to the bottom of the file and paste the following lines

. [public] comment = public share, no need to enter username and password path = /srv/samba/public/ browseable = yes writable = yes guest ok = yes

Save and close the file. Next, create the /srv/samba/public/ folder.

sudo mkdir -p /srv/samba/public

Then, make sure that the no one account has read, write, and execute permission on the public folder by running the following command. (If your system does not have the setffl command, you must install the acl package with sudo apt install acl.)

sudo setfacl -r -m “u:nobody:rwx” /srv/samba/public/ Restart smbd and nmbd. sudo systemctl restart

smbd nmbd

Accessing the Samba shared folder from

Windows

On a Windows computer that is on the same network, open File Explorer and click Network in the left pane. If you see the following message, you must click the message and turn on network discovery and file sharing.

File sharing is turned off. Some computers and devices on the network may not be visible. Next, enter \\ followed by the IP address of the

Samba server in the address bar of File Explorer, like this: \\192.168.0.102. You will see a list of shares on the Samba server.

Access samba share from windows

Then double-click the shared folder. To access the private share, you need to enter the samba username and password. You do not need to do this to access the public share.

Access Samba Share from Windows 10

Once connected, you can read, write, and delete files in the Samba shared folder

.

Connection

error

If you receive the following error:

You do not have permission to access \\hostname\share-name. Contact your network administrator to request access.

You can try to connect to the Samba share from the command prompt. Open a command prompt, then run the following command to close the current Samba session.

net use \\samba-server-ip\share-name /delete Next, connect to the Samba

share with the following command

: net use \\samba-server-ip\share-name /user:samba-username password

Once the above command has completed successfully, go to the Network tab in File Explorer and you should now be able to access the Samba share. Drive mapping

in

Windows

A feature of the Windows operating system is the ability to assign a drive letter (such as S:) to a remote directory. To assign the drive letter S: to the Samba share, right-click the Samba shared folder and select Map Network Drive. Then choose a drive letter and click Finish.

map network drive samba

Once the drive

mapping is set, applications can access files on the Samba share via the drive letter S:. And this Samba share will be mounted automatically when you log in to your Windows computer.

Accessing Samba shared folder

in Nautilus File Manager on Linux

If you are using the Nautilus file manager, click Other Locations in the left pane. At the bottom, you will see an option to connect to the server. To access your Samba share, type smb:// followed by the IP address of the Samba server and press Enter. For example:

  • smb://192.168.0.102

<img src="https://www.linuxbabe.com/wp-content/uploads/2020/03/samba-shares-password-authentication.png" alt="

Samba-file-share-ubuntu

” />

You will see a list of shares on the Samba server.

samba shares

If you click on the private shared folder, you will need to enter the Samba username and password. If you click the public shared folder, choose Connect as anonymous.

If you see the following error message

, the server share list could not be retrieved

You can try to fix this error by mounting the Samba share from the command line, described below

.

Automatically mount Samba Share from the command line on Linux

Note: Automatic mounting of the Samba share is performed on clients. These commands must be run in a Samba client, if the Samba client is running Linux. You should not do this on the Samba server itself.

If you need to automatically mount the

Samba share at boot time, you can use the command line to mount and then add an entry in the /etc/fstab file. To do that, you need to install the cifs-utils package.

CentOS/RHEL

sudo dnf install cifs-utils

Debian/Ubuntu

sudo apt install cifs-utils

Next, create a mount point for the

Samba share. sudo mkdir /mnt/samba-private

You can now use the following command to mount a private shared folder

. sudo mount -t cifs -o username=your_samba_username //192.168.0.102/private /mnt/samba-private/

It will ask you to enter the Samba password. After that, it will be mounted in the /mnt/samba-private/ directory.

To automatically mount the Samba share, edit the /etc/fstab file.

sudo nano /etc/fstab

Add the following line to the file

. 192.168.0.102/private /mnt/samba-private cifs x-systemd.automount,_netdev,credentials=/etc/samba-credential.conf,uid=1000,gid=1000,x-gvfs-show 0 0

Where

: //

  • 192.168.0.102/private: The IP address of the Samba server and the name of the
  • share. /

  • mnt/samba-private: Mount point for the Samba share
  • .

  • CIFS:
  • File System Type x-systemd.automount : This option instructs systemd to create an automount drive for the file system. We use this because it ensures that the remote file system is mounted only after there is network access.
  • _netdev: This specifies that the mount requires networking.
  • credentials=: Linux must look for credentials in the /etc/samba-credential.conf file.
  • uid=1000,gid=1000: By default, the mounted file system would be owned by the root user. We use uid and gid to change the ownership of the file system. You normally use your own uid and gid, which are 1000 by default.
  • x-gvfs-show: If you are using the GNOME desktop environment or its derivatives, you can use this option to display the mounted file system in the file manager.

Save and close the file. Next, create the credentials file.

sudo nano /etc/samba-credential.conf Add the

following lines to the

file. username=your_samba_username password=samba_password domain=WORKGROUP

Save and close the file. Make sure that only the root user can read this file.

sudo chmod 600 /etc/samba-credential.conf

If you restart your Linux computer now, the Samba share will be mounted automatically. You can also run the following command to mount the Samba share without rebooting.

sudo mount -a If you see the

permission denied error and can find the following line by running the sudo dmesg

, VFS command: cifs_mount failed with return code = -13,

it’s probably because you have a typo in the /etc/samba-credential file

.

Further reading: How to automatically mount file systems on Linux.

Can’t write to the Samba share?

The CIFS mount described above allows you to write to the Samba share. If you see the following error when creating a file:

Read-only file system

Verify that you have set writable = yes in the Samba configuration file. Sometimes the Samba shared folder is on an external hard drive, then make sure to mount the external hard drive in read and write mode on Samba server. For example, I mounted my btrfs hard drive with the following line in /etc/fstab.

LABEL=5TB /mnt/5TB btrfs defaults 0 0

It turns out that the defaults option does not allow the write operation. To make it typeable, add the rw option.

LABEL=5TB /mnt/5TB btrfs defaults,rw 0 0

Then unmount the hard disk. You must use your own mount point.

sudo umount /mnt/5TB

And mount it again

. sudo mount -a

troubleshooting tip

If your Samba server is not working as expected, you can check the log files in the /var/log/samba/ directory. You can add the following line in the [global] section of the /etc/samba/smb.conf file to increase the logging level if you want to record more information.

Logging Level = 2

A Simple Trick

to Increase Samba Performance You can enable the TCP BBR algorithm to increase the performance of

the server network. Easily increase

  • Ubuntu network performance by enabling TCP BBR

Wrapping Up

That’s it! I hope this tutorial helped you set up Samba server on Ubuntu. As always, if you found this post useful, subscribe to our free newsletter. And you may also want to read the following article to share the printer on the local network.

  • Configure CUPS print server on Ubuntu (Bonjour, IPP, Samba, AirPrint)

Do you want to run Samba over the Internet? You must configure WireGuard VPN to encrypt the SMB/CIFS protocol. Since WireGuard uses peer-to-peer public key authentication, it also allows you to remove password authentication in Samba, as long as you configure the firewall to allow Samba only for VPN clients and ban all other IP addresses.

  • Set up your own WireGuard VPN server on Ubuntu 22.04/20.04/18.04

Want better performance? You can use NFS (Network File System) instead of Samba.

  • How to Set Up NFS Share in Ubuntu 20.04, 18.04