×
Community Blog How To Set Up a Samba Share on Ubuntu 16.04

How To Set Up a Samba Share on Ubuntu 16.04

In this tutorial, we will learn how to install and setup Samba share on an Alibaba Cloud ECS instance with 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't 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.

sudo apt-get update

Install Samba

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

sudo apt-get install samba

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.

sudo systemctl stop nmbd.service
sudo systemctl disable nmbd.service

You should see the following output.

ubuntu@Sajid:~$ sudo systemctl disable nmbd.service
nmbd.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install disable nmbd
insserv: warning: current start runlevel(s) (empty) of script `nmbd' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `nmbd' overrides LSB defaults (0 1 6).

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.

sudo systemctl stop smbd.service

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.

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orig

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

ip link

You should see something similar to this as your output.

ubuntu@Sajid:~$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 02:f8:7d:b9:7b:dc brd ff:ff:ff:ff:ff:ff

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.

sudo apt-get install nano

Now, edit the configuration file.

sudo nano /etc/samba/smb.conf

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

[global]
        server string = samba_server
        server role = standalone server
        interfaces = lo your_network_interface
        bind interfaces only = yes
        disable netbios = yes
        smb ports = 445
        log file = /var/log/samba/smb.log
        max log size = 10000

Save the file and exit from the text editor.

Execute the following command to check there are no errors.

testparm

The above command will give you result like this:

Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

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

#Global parameters
[global]
        server string = samba_server
        interfaces = lo your_network_interface
        bind interfaces only = Yes
        server role = standalone server
        log file = /var/log/samba/smb.log
        max log size = 10000
        smb ports = 445
        disable netbios = Yes
        idmap config * : backend = tdb

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.

sudo mkdir /samba/

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.

sudo chown :sambashare /samba/

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

sudo mkdir /samba/paul

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

sudo adduser --home /samba/paul --no-create-home --shell /usr/sbin/nologin --ingroup sambashare paul

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.

sudo chown paul:sambashare /samba/paul/
sudo chmod 2770 /samba/paul/

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.

sudo smbpasswd -a paul

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

sudo smbpasswd -e paul

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.

sudo mkdir /samba/everyone

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

sudo adduser --home /samba/everyone --no-create-home --shell /usr/sbin/nologin --ingroup sambashare admin

sudo chown admin:sambashare /samba/everyone/
sudo chmod 2770 /samba/everyone/
sudo smbpasswd -a admin
sudo smbpasswd -e admin

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.

sudo groupadd admins
sudo usermod -G admins admin

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.

sudo nano /etc/samba/smb.conf

Add the following content to the file for user Paul.

[paul]
        path = /samba/paul
        browseable = no
        read only = no
        force create mode = 0660
        force directory mode = 2770
        valid users = paul @admins

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.

[everyone]
        path = /samba/everyone
        browseable = yes
        read only = no
        force create mode = 0660
        force directory mode = 2770
        valid users = @sambashare @admins

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

[global]
        server string = samba_server
        server role = standalone server
        interfaces = lo your_network_interface
        bind interfaces only = yes
        disable netbios = yes
        smb ports = 445
        log file = /var/log/samba/smb.log
        max log size = 10000

[paul]
        path = /samba/paul
        browseable = no
        read only = no
        force create mode = 0660
        force directory mode = 2770
        valid users = paul @admins

[everyone]
        path = /samba/everyone
        browseable = yes
        read only = no
        force create mode = 0660
        force directory mode = 2770
        valid users = @sambashare @admins

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.

testparm

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.

sudo systemctl start smbd.service

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

sudo apt-get update
sudo apt-get install smbclient

On CentOS

sudo yum update
sudo yum install samba-client

There is a specific format defined to access Samba shares:

smbclient //YourServerIP/share -U username

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.

smbclient //YourServerIP/paul -U paul

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

smbclient //YourServerIP/everyone -U paul

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:

smb: \> mkdir direc

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

smb: \> ls

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.

C:\> net use drive_letter \\YourServerIP\share

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.

C:\> net use F: \\YourServerIP\paul

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.

1 1 1
Share on

Alibaba Clouder

1,138 posts | 190 followers

You may also like

Comments

Raja_KT February 14, 2019 at 6:28 am

Good one. Heard it interacts with Glusterfs but not sure the advantages.