Samba Linux: The Open Source File and Print Server
Samba is an open-source software suite that runs on Unix/Linux based platforms but is able to communicate with Windows clients like a native application. So Samba is able to provide this service by using the Common Internet File System (CIFS).
- File & print services
- Authentication and Authorization
- Name resolution
- Service announcement (browsing)
Samba can be run on many different platforms including Linux, Unix, OpenVMS and operating systems other than Windows and allows the user to interact with a Windows client or server. It can basically be described as the Standard Windows interoperability suite of programs for Linux and Unix.
How to install Samba Server on Ubuntu?
Let’s take a look at setting up a Samba Server on Ubuntu to share files with Windows clients. Note that we will create two forms of Samba server. One setup won’t require a password to share files with any client on the network, which is the anonymous share. The other setup will require setting up users and permissions.
1. Install Samba
Binary packages of Samba are included in almost any Linux distribution. There are also some packages available at the Samba homepage. In fact there are now several GUI interfaces to Samba available to help with configuration and management. This article will help you install and set up Samba via the Linux terminal.
We install the samba package from the terminal in Ubuntu with the following code –
sudo apt-get update sudo apt-get install samba
2. Configure File Server – Anonymous Share
a. Create a shared folder called “shared folder”.
sudo mkdir -p /srv/samba/sharedfolder
b. Allow anyone to access and store files in folder.
sudo chown nobody:nogroup /srv/samba/sharedfolder/
c. Edit configuration file to enable sharing.
Make a backup of the configuration file before editing
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old
Open the conf file and make the following changes –
Add the following at the end of the file to enable sharing – d. Restart smbd service –
sudo systemctl restart smbd.service nmbd.service
3. Configure File Server – Secured Share
a. Create shared folder called “securedfolder”.
sudo mkdir -p /srv/samba/securedfolder
b. Create a new user group named “selected”.
c. Modify permission and ownership for the folder.
sudo chown root:selected /srv/samba/securedfolder/ sudo chmod 770 /srv/samba/securedfolder/
d. Edit configuration file to allow sharing
Make a backup of the configuration file before editing.
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old
Open the conf file and make the following changes –
Add the following at the end of the file to enable sharing –
[sharedfolder] comment = secured shared folder path = /srv/samba/securedfolder Valid users = @selected guest ok = no writable = yes browsable = yes
sudo systemctl restart smbd.service nmbd.service
f. Once Samba has restarted, use this command to check your smb.conf for any syntax errors. testparm g. Add new users
We are going to create and add a user “Jack” to the user group “selected” with restricted shell access. sudo useradd jack -s /usr/sbin/nologin -G selected To add a password for the user –
h. Add existing user Jane to the group to the group “selected”.
sudo usermod jane -G selected
Conclusion
You should be able to access and browse files from these servers from your Windows client. If you do not see your client automatically, you can try accessing it via its IP address.
You can access the Ubuntu sharing in Windows by entering “\\sharedfolder” or “\\securedfolder” in the windows search field of the menu or use the network browser of the Windows file explorer to connect to the share.
In the case of the secured share, the user will be required to enter the password before being able to access the shared folder. And there it is folks, a short guide to setting up samba server on Ubuntu. You can also check out the official how-to to find out more about installing and setting up Samba.
Getting started with Samba for interoperability
The Samba suite helps you to easily share files between Linux and Windows systems. This first article of two teaches you how and where to begin with interoperability.
File shares are important for collaboration in any network. In this series of articles, I will show you how Samba can help you set up and serve file shares on Linux servers that are accessible from both Linux and Windows clients.
Before we dive in and set up file shares, let’s cover what Samba is and what it can do for you.
Great Linux resources
Samba is a suite of applications that implements the Server Message Block (SMB) protocol. Many operating systems, including Microsoft Windows, use the SMB protocol for client-server networking. Samba enables Linux / Unix machines to communicate with Windows machines in a network.
Samba is open source software. Originally, Samba was developed in 1991 for fast and secure file and print share for all clients using the SMB protocol. Since then it has evolved and added more capabilities. Today Samba provides a suite of applications enabling seamless networking and interoperability between *nix and Windows.
Install the Samba packages
In this example, I’m using CentOS 8 with the hostname centos as my Samba server with an IP address of 192.168.1.122. The only package you will need to install is Samba, but I would like to include other client tools like samba-common and samba-client to test out our shares later.
# yum install samba samba-client samba-common -y
Create a directory to share and set its permissions
Once you’ve installed the required packages on the server, let’s create a directory and provide the required permissions on the directory. Create a directory called share on our root directory. You can name this directory anything you want to, and give it read, write and execute permissions. In this example, we will rely on the Samba configuration to control read, write access to the shared directory.
# mkdir /share # chmod -R 777 /share
Now that we have installed necessary packages, it’s time to configure the shared directory. The configuration file is located under /etc/samba . Using your favorite text editor, append the following to /etc/samba/smb.conf . Leave the rest of the configuration file as is and this should be good for our setup.
# vim /etc/samba/smb.conf [myshare] comment = My share path = /share read only = No
I have created a share called myshare and provided a path to the share, which in our case is located at /share . I then gave it read/write permissions with read only = No . There are many other configuration parameters that you may pass here, but for our setup these settings are good.
Check the Samba configuration
Samba provides a tool to check our configuration file. We can check if our configuration file has any errors by using the testparm command.
Next, we need to create a Samba user account, so that Samba can communicate in Samba sessions with shares. The smbpasswd command has several functions depending on if you run the command as root or as a standard user. If you issue smbpasswd without any arguments as a non-root user, by default smbpasswd will change the current logged in user’s Samba password. When you run smbpasswd command as root , you will be able to create new Samba accounts. You can create a Samba account only for existing Linux user accounts.
# smbpasswd -a user New SMB password: Retype new SMB password:
We need to configure SELinux on the /share directory so the Samba service has proper permissions.
# semanage fcontext -a -t samba_share_t "/share(/.*)?" # restorecon -R -v /share
Next, we need to enable and start the smb and nmb services.
# systemctl enable smb # systemctl start smb #systemctl enable nmb #systemctl start nmb
If you have firewalld running on your server, we need to configure it to allow Samba traffic. You can do so by using the firewall-cmd command. Don’t forget to make those changes permanent and reload firewalld .
Now we have our services enabled and started. We can check to see if the share is available from the server by using the smbclient tool. This is the reason I have installed the Samba client tools on the local machine. The command uses the following syntax:
# smbclient -U user -L 192.168.1.122 Enter SAMBA\user's password:
Let’s check if we can access the share by issuing the following command on the server:
# smbclient -U user //192.168.1.122/myshare Enter SAMBA\user's password: Try "help" to get a list of possible commands. smb: \>
We have installed and configured the Samba file share service on our Linux server. We were able to access those shares on the local machine. In the next article, we will set up one Linux client and one Windows 10 client to access share we just created.
[ Want to learn more about Linux system administration? Take a Red Hat system administration course. ]