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:
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.
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
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.
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.
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
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
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.
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.
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.
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.
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
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
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.
You are now connected to Samba server and you can access samba shares as if it were a local directory.
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.
You can connect to Samba server from windows GUI also. Please follow the below-given instructions.
You are now connected to Samba server and you will now be able to use and manage files and folders in the Samba share.
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.
Alibaba Clouder - March 7, 2019
Alibaba Clouder - June 11, 2018
Alibaba Clouder - February 13, 2018
Alibaba Clouder - September 5, 2018
francisndungu - May 29, 2019
Alibaba Clouder - December 21, 2018
Connect your business globally with our stable network anytime anywhere.Learn More
An end-to-end software-defined network solution for multinational enterprises.Learn More
Accelerate software development and delivery by integrating DevOps with the cloudLearn More
Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.Learn More
More Posts by Alibaba Clouder