How To Set Up a Samba Share on Ubuntu 16.04

By Sajid Qureshi, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Samba is a free and open-source SMB/CIFS networking protocol implementation. It is popularly used for file sharing between Unix and Windows machine in a local area network. Samba services are implemented as two daemons:

  1. smbd, which provides the file and printer sharing services, and
  2. nmbd, which provides the NetBIOS-to-IP-address name service. NetBIOS over TCP/IP requires some method for mapping NetBIOS computer names to the IP addresses of a TCP/IP network.

In this tutorial, we will learn how to install and setup Samba share on an Alibaba Cloud Elastic Compute Service (ECS) instance with Ubuntu 16.04. We will be looking at installing Samba and adding users to access the share. Then, we will be configuring the Samba server so these users can access their share directories.

Prerequisites

  1. You must have Alibaba Cloud Elastic Compute Service (ECS) activated and verified your valid payment method. If you are a new user, you can get a free account in your Alibaba Cloud account. If you don抰 know about how to set up your ECS instance, you can refer to this tutorial or quick-start guide. Your ECS instance must have at least 1GB RAM and 1 Core processor.
  2. A domain name registered from Alibaba Cloud. If you have already registered a domain from Alibaba Cloud or any other host, you can update its domain nameserver records.

Update the System

It is recommended to install any new package on a freshly updated server. To update all the available packages run the following command.

Install Samba

There are no dependencies required to install Samba. So, now let’s install Samba using the following command.

The above command will install both Samba server smbd and the Samba NetBIOS server nmbd. We don't need nmdb as of now, so stop and disable it for security purpose. You can do so using the following commands.

You should see the following output.

You’ll need to configure a few things before using samba share for production purpose. So, it is recommended to stop the Samba share server until then. Execute the following command and it will do the job for you.

Next, you will need to edit the Global option which is available in the configuration file of Samba. This configuration file has two parts: a [global] section and a [shares] section. The [global] section configures the behavior of the Samba server, and the [shares]sections configure the file shares.

First backup the original samba configuration file and create a new file. You can do so using the following command.

We recommend you to check the available interfaces before modifying any configuration.

You should see something similar to this as your output.

The above output indicates that lo is the loopback interface and eth0 is the external network interface.

Next, you will need to edit the Samba configuration file using any text editor. Here we are using nano text editor, you can also install it using the following command.

Now, edit the configuration file.

Next, edit or modify the directive settings as described below.

Save the file and exit from the text editor.

Execute the following command to check there are no errors.

The above command will give you result like this:

Hit the Enter button and you will get the following output.

Adding Users

You will need to add a user to access the shares. Who will need access as Samba and system users in order to authenticate with the Samba server when they log in and read and write to the file system. You can add as many users as you want to share your files. Here we are creating and adding a new user called Paul.

First of all create a new directory /samba/ where the Samba data will be stored, at the root of the file system. Execute the following command to create a new directory.

Next, you will need to change the ownership rules to sambashare, a group that was created when you installed Samba. Execute the following command and it will do the job for you.

Next, create paul’s home directory under the /samba/ directory:

Next, you will need to add paul as a system user using the following command:

You have successfully added a new user. Now you can set the ownership and permissions on his Samba home directory. Execute the following commands and they will do the job for you.

Samba maintains its own database of its users and passwords. In order to log in, you must add all the users to the Samba server and enabled.

Execute the following command to add them to the Samba server.

The above command will add the user to the Samba server. Now you will need to enable the user using the following command.

The user paul now exists as a system user without the ability to SSH into the server. He has a home directory at /samba/paul, and is registered and enabled as a Samba user.

If in case you want more users like Jackson, Halli and Sam then repeat this process for every Samba user.

Next, you will need to create an admin user. Admin user will access and manage all the personal shares.

Create a new directory /samba/everyone/ using the following command.

Now create and add the admin user. Execute the following commands one by one and they will do the job for you.

Next, you will need to create a group called admins to make the management of the server easier. You can create a new group called admins and add the user admin to this group, using the following commands.

The system configurations are now complete.

Configuring the Samba Shares

In the main Samba server configuration file /etc/samba/smb.conf, each share will have its own section. You will need to edit the configuration file using any text editor.

Add the following content to the file for user Paul.

You will need to add this share block for every user you want to share like Jackson, Halli and Sam.

Next, you will need to add a share block for everyone and it will be different from other users. Simply add the following content to the file.

So the complete configuration file /etc/samba/smb.conf will be like this:

Save the file and exit from the text editor.

Execute the following command to make sure that there are no errors in the configuration file.

If you get the Loaded services file OK at the end of the output then it indicates there is no error in the configuration.

Everything is configured so now you are ready to start Samba server services. You can start the Samba server using the following command.

The Samba server is now running active and it is ready to accept logins.

Accessing Your Samba Shared Server

Next, you will need to log into the Samba server to test that it is working as expected. Now we will access the Samba shares we created from Linux and Windows machines.

Linux ?The Command Line

You can access the Samba shares from the command line using smbclient tool. You will need to install it using the following commands.

Debian and Ubuntu servers

On CentOS

There is a specific format defined to access Samba shares:

You can replace YourServerIP from hostname if you want to access it using hostname you defined in the configuration file.

Suppose you want to access paul’s share on the Samba server. You can do so using the following command.

If paul wants access to the common share (everyone), change the command to:

After running the above command, you will be asked for the Samba password and logged into a command line interface. This interface is most useful for testing usernames and passwords and read-write access.

You can create a new directory like this:

You can also list all the contents of the directory using the following command.

Linux ?KDE with Dolphin

Dolphin is the default file manager in Linux. You can access Samba shares using dolphin. Please follow the below-given instruction to access the Samba shares.

  1. Open the Dolphin file manager.
  2. Select Network from the Places list. (Available in the top left )
  3. Click on Add Network Folder.
  4. Select the Microsoft Windows network drive option.
  5. Click on Next button.
  6. Enter the details for Samba shares like Name, Server hostname, and Folder that was set in configuration file.
  7. Finally, click on Save & Connect button.
  8. You’ll be asked to enter username and password for user login. So, enter it and click on OK button.

You are now connected to Samba server and you can access samba shares as if it were a local directory.

Windows 10 ?The Command Line

Accessing Samba shares from windows 10 command line is so easy. All you need to do is execute one single command.

Replace the drive_letter with your hard drive and share with the username you want to access. For example, you want to access the paul’s share then execute the following command.

You’ll be asked to enter paul’s username and password so enter it.

After running above command you will get The command completed successfully at the end of the command.

You are now connected to Samba server and you can access samba shares from Windows File Explorer.

Windows 10 ?Desktop

You can connect to Samba server from windows GUI also. Please follow the below-given instructions.

  1. Right click on This PC.
  2. Click on Add a network location and then Next.
  3. Next, click on Choose a custom network location.
  4. Click Next.
  5. Enter the address of Samba share in following format \\your_samba_hostname_or_server_ip\share\.
  6. Click on Next button.
  7. You’ll be asked to enter the username and password for the user.

You are now connected to Samba server and you will now be able to use and manage files and folders in the Samba share.

Conclusion

In this tutorial, you have learned how to install and set up Samba share on an ECS Ubuntu instance. You have also configured the Samba server and accessed the share from various platforms like Linux and Windows machines.

We hope now you have enough knowledge to work with Samba server and you can share your files and directories.

Reference:https://www.alibabacloud.com/blog/how-to-set-up-a-samba-share-on-ubuntu-16-04_594300?spm=a2c41.12446907.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.