How to Install and Configure Samba Server on Ubuntu 16.04 for File Sharing

In this tutorial, we’re going to learn how to install and configure a samba server on Ubuntu 16.04. Samba is a free and open-source SMB/CIFS protocol implementation for Unix and Linux that allows for file and print sharing between Unix/Linux and Windows machines in a local area network.

Samba contains several programs that serve different but related purposes, the most important two of which are:

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

How to Install Samba Server on Ubuntu 16.04

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

sudo apt install samba samba-common-bin

The latest stable version available is 4.5.3, released on December 19, 2016. To check your Samba version, run

sudo smbstatus

or

smbd --version

Sample output:

Samba version 4.3.11-Ubuntu

To check if Samba service is running, issue the following commands.

systemctl status smbd

systemctl status nmbd

To start these two services, issue the following commands:

sudo systemctl start smbd

sudo systemctl start nmbd

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

  • TCP 139: used for file and printer sharing and other operations.
  • TCP 445: the NetBIOS-less CIFS port.
  • UDP 137: used for NetBIOS network browsing.
  • UDP 138: used for NetBIOS name service.

Create a Private Samba Share

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

sudo nano /etc/samba/smb.conf

In the [global] section, make sure the value of workgroup is the same with the workgroup settings of Windows computers.

workgroup = WORKGROUP

samba share ubuntu

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

samba ubuntu server

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

[Private]

comment = needs username and password to access
path = /srv/private/
browseable = yes
guest ok = no
writable = yes
valid users = @samba

Explanation:

  • Private is the folder name that will be displayed on the Windows network.
  • The comment is a description for the shared folder.
  • The path parameter specifies the path to the shared folder. I use /srv/private/ as an example. You can also use a folder in your home directory.
  • browseable = yes: Allow other computers in the network to see the Samba server and Samba share. If set to no, users have to 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: Disable guest access. In other words, you need to enter username and password on the client computer to access the shared folder.
  • writable = yes: Grants both read and write permission to clients.
  • valid users = @samba: Only users in the samba group are allowed to access this Samba share.

Save and close the file. (To save the file in nano text editor, press Ctrl+O, then press Enter to confirm the file name 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 username with your desired username.

sudo adduser username

You will be prompted to set an 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 username samba

Create the private share folder.

sudo mkdir /srv/private/

The samba group needs to have read, write and execute permission on the shared folder. You can grant these permissions by executing the following command.

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

Next run the following command to check if there’s syntactic errors.

testparm

Now all left to do is to 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 username and password, the following conditions must be met.

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

As a matter of fact, the first two conditions are already met as Samba by default uses these two settings.

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

sudo nano /etc/samba/smb.conf

In the [global] section, make sure the value of workgroup is the same with the workgroup settings of Windows computers.

workgroup = WORKGROUP

samba share ubuntu

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

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/public/
browseable = yes
writable = yes
guest ok = yes

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

sudo mkdir /srv/public

Then make sure the nobody account has read, write and execute permission on the public folder by executing the following command.

sudo setfacl -R -m "u:nobody:rwx" /srv/public/

Restart smbd and nmbd.

sudo systemctl restart smbd nmbd

Accessing Samba Shared Folder From Windows

On a Windows computer that is in the same network, open File Explorer and click Network on the left pane. Then double-click the hostname of your Ubuntu computer. For example, the hostname of my Ubuntu 16.04 computer is xenial.

samba ubuntu setup

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

samba public share

Once connected, you can read, write and delete files in the Samba shared folder. Note that if you access the private share and public share at the same time, you may not be able to create files or folders in the public share.

Connecting Error

If you get the following error:

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

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

net use \\hostname\share-name /delete

Next, connect to the Samaba share with the following command:

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

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

Drive Mapping

One feature of the Windows operating system is the capability to map a drive letter (such as S:) to a remote directory. To map the drive letter S: to the Samba share, open up command prompt and run the following command.

net use s: \\hostname\share-name

For example, my hostname is xenial and the name of my Samba shared folder is home, so I run

net use s: \\xenial\home

Once the drive mapping is established, applications can access the files in the Samba share through the drive letter S:.

Accessing Samba Share Folder From a Ubuntu Computer

In your file manager, click the Network tab on the left pane and click Windows Network.

samba server ubuntu 16.04

Select the workgroup, your Samba server and the shared folder, then enter the Samba username and password.

samba ubuntu

That’s it! I hope this tutorial helped you set up Samba server on Ubuntu 16.04. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+ Twitter or like our Facebook page.

Rate this tutorial
[Total: 85 Average: 3.4]