How to install Samba server on CentOS 8

How to install Samba server on CentOS 8

Samba is free and open source software that can be used to share files, folders and printers between Linux and Windows systems. It is also used for authentication and authorization, name resolution and service announcements. It can run on different operating systems, including Linux, Unix, OpenVMS, etc.

In this tutorial, we will learn how to install Samba on SentOS 8 and configure it as a standalone shared server.

prerequisites

  • Server 8 running CentOS.
  • A root password is configured on your server.

Install Samba server

By default, the Samba package is available in the CentOS default repository. You can use the following command to install:

dnf install samba samba-common samba-client -y

After installing Samba, start the SMB service and use the following command to start it after the system reboots:

systemctl start smb systemctl enable smb

Now you can use the following command to verify the Samba service:

systemctl status smb

You should get the following output:

? smb.service - Samba SMB Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-02 23:03:30 EST; 8s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 2072 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 4 (limit: 25028)
   Memory: 33.8M
   CGroup: /system.slice/smb.service
           ??2072 /usr/sbin/smbd --foreground --no-process-group
           ??2074 /usr/sbin/smbd --foreground --no-process-group
           ??2075 /usr/sbin/smbd --foreground --no-process-group
           ??2076 /usr/sbin/smbd --foreground --no-process-group

Create a public share with Samba

In this section, we will create a public share with Samba so that everyone can access the public shared directory with a password.

Create a public shared directory

First, create a shared folder named public and create two files in the public directory:

mkdir -p /samba/share/public touch /samba/share/public/file1.txt touch /samba/share/public/file2.txt

Next, assign the necessary permissions and ownership using the following command:

chmod -R 0755 /samba/share/ chmod -R 0755 /samba/share/public chown -R nobody:nobody /samba/share chown -R nobody:nobody /samba/share/public

Configure Samba

Next, you will need to configure Samba to share the public directory.

First, create a backup copy of the /etc/samba/smb.conf file using the following command:

mv /etc/samba/smb.conf /etc/samba/smb.bak

Next, create a new Samba configuration file:

nano /etc/samba/smb.conf

Add the following lines:

[global]
workgroup = WORKGROUP
server string = Samba Server %v
netbios name = samba-server
security = user
map to guest = bad user
dns proxy = no

[Public]
path = /samba/share/public
browsable =yes
writable = yes
guest ok = yes
read only = no

Save and close the file. Then, restart the Samba service to apply the changes:

systemctl restart smb

Next, test the Samba configuration using:

testparm

You should see the following output:

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

# Global parameters
[global]
	dns proxy = No
	map to guest = Bad User
	netbios name = SAMBA-SERVER
	security = USER
	server string = Samba Server %v
	idmap config * : backend = tdb


[Public]
	guest ok = Yes
	path = /samba/share/public
	read only = No

Configure SELinux and firewall

Next, set the correct SELinux boolean and security context values ​​on the shared directory using the following command:

setsebool -P samba_export_all_ro=1 samba_export_all_rw=1 semanage fcontext –at samba_share_t "/samba/share/public(/.*)?" restorecon /samba/share/public

Next, use the following command to provide all Samba services through firewalld:

firewall-cmd --add-service=samba --zone=public --permanent firewall-cmd --reload

Accessing Samba shares from Ubuntu Gnome

To access the Samba share, go to the remote computer, open the Gnome file manager, and click connection to server As follows:

Provide your Samba server IP address and click connection Button. After successfully connecting, you should see the Samba share in the following screen:

Samba Share in File Explorer

Now click Go public Directory, you should see the files in the following screen:

List of Documents

Accessing Samba shares from the Ubuntu command line

You can also access Samba shares from the command line.

First, use the following command to list all available Samba shares:

smbclient -L //45.58.38.51

You should see the following output:

Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4]

	Sharename       Type      Comment
	---------       ----      -------
	Public          Disk      
	IPC$            IPC       IPC Service (Samba Server 4.10.4)
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------

You can also mount a Samba share using the cifs protocol. To do this, install the cifs-utils package using the following command:

apt-get install cifs-utils -y

Next, mount the Samba share to the / mnt directory using the following command:

mount -t cifs //45.58.38.51/public /mnt/

You will be asked to provide a password as follows:

Password for [email protected]//45.58.38.51/public: 

Just press Enter Install Samba shares without entering any password:

Now you can access the Samba share in the / mnt directory:

ls /mnt/

You should see the following output:

file1.txt  file2.txt

Create a private share with Samba

In this section, we will use Samba to create a private share so that only authenticated users can access the private share directory.

Create users and groups

First, create a group named private using the following command:

groupadd private

Next, create a new user named privateuser and add it to the private group:

useradd -g private privateuser

Next, set a password for the user using the following command:

smbpasswd -a privateuser

Output:

New SMB password:
Retype new SMB password:
Added user privateuser.

Create a private shared directory

Next, create a shared folder named private and create two files in the private directory:

mkdir -p /samba/share/private touch /samba/share/private/private1.txt touch /samba/share/private/private2.txt

Next, assign the appropriate permissions and ownership using the following command:

chmod -R 0770 /samba/share/private chown -R root:private /samba/share/private

Next, configure the SELinux context for the private directory using the following command:

semanage fcontext –at samba_share_t "/samba/share/private(/.*)?" restorecon /samba/share/private

Configure Samba

Next, open the Samba configuration file and define the private share:

nano /etc/samba/smb.conf

Add the following line at the end of the file:

[Private]
path = /samba/share/private
valid users = @private     
guest ok = no
writable = yes
browsable = yes

Save and close the file, then restart the Samba service to apply the changes:

systemctl restart smb

Next, check the Samba configuration using the following command:

testparm

You should see the following output:

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

# Global parameters
[global]
	dns proxy = No
	map to guest = Bad User
	netbios name = SAMBA-SERVER
	security = USER
	server string = Samba Server %v
	idmap config * : backend = tdb


[Public]
	guest ok = Yes
	path = /samba/share/public
	read only = No


[Private]
	path = /samba/share/private
	read only = No
	valid users = @private

Accessing Samba shares from the Ubuntu command line

First, use the following command to access the available shares:

smbclient -L //45.58.38.51

You should see the following output:

Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4]

	Sharename       Type      Comment
	---------       ----      -------
	Public          Disk      
	Private         Disk      
	IPC$            IPC       IPC Service (Samba Server 4.10.4)
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------

Next, connect to the Samba server and list the available shares using the following command:

smbclient //45.58.38.51/private -U privateuser

You will be asked to provide a password as follows:

Enter privateuser's password: 

Enter the password and press Enter to access the Samba shell as shown below:

Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4]
smb: >

Now use the following command to list the available shares:

smb: > ls

You should see the following output:

  
.                                   D        0  Tue Mar  3 10:03:22 2020
  ..                                  D        0  Tue Mar  3 10:01:56 2020
  private1.txt                        N        0  Tue Mar  3 10:03:17 2020
  private2.txt                        N        0  Tue Mar  3 10:03:22 2020

		51194 blocks of size 2097152. 49358 blocks available

Now, exit from the Samba shell using the following command:

smb: >exit

You can also mount the Samba share in the / opt directory:

mount -t cifs -o user=privateuser //45.58.38.51/private /opt

You will be asked to provide a password as follows:

Password for [email protected]//45.58.38.51/private:  *********

Provide the password and press Enter to mount the Samba share.

You can now check your Samba shares in the / opt directory as follows:

ls /opt/

Output:

private1.txt  private2.txt

Accessing Samba shares from Ubuntu Gnome

To access the Samba share, go to the remote computer, open the Gnome file manager, and click connection to server As follows:

Access to private shares in Gnome

Provide your Samba server IP address and click connection Button. After the connection is successfully established, you should see the Samba share on the following screen:

Public and private sharing

Now click private Directory, provide your username and password, and click the Connect button. You should see the file in the following screen:

Login with username and password

file

Congratulations! You have successfully installed and configured the Samba server on CentOS 8.

Related Posts