Install and Configure Samba Share on Rocky Linux 8: Free File Share

Posted on

Install and Configure Samba Share on Rocky Linux 8: Free File Share

Install and Configure Samba Share on Rocky Linux 8: Free File Share

In this guide, we aim to demonstrate how to Install and Configure Samba Share on Rocky Linux 8. Samba is a powerful open-source software suite that operates seamlessly on Unix/Linux-based systems, offering the unique capability to communicate with Windows clients as if it were a native application. This interoperability is achieved through Samba’s utilization of the Common Internet File System (CIFS) protocol.

You can now proceed to the following guide steps on the Orcacore website to start the Samba File share setup on Rocky Linux 8.

To set up Samba File Share, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 8.

Samba packages are readily available in the default Rocky Linux repository.

First, update your local package index with the command below:

sudo dnf update -y

Then, use the following command to install Samba packages:

sudo dnf install samba samba-common samba-client  -y

This will install the dependencies and required packages.

Manage Samba Service

Then, use the commands below to start and enable the Samba service on Rocky Linux 8:

# sudo systemctl start smb.service
# sudo systemctl enable smb.service

Verify your Samba file share service is active and running:

sudo systemctl status smb.service
**Output**
● smb.service - Samba SMB Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset=>
   Active: **active** (**running**) since Mon 2023-03-27 03:54:16 EDT; 11s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 90690 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 3 (limit: 23699)
   Memory: 8.5M
   CGroup: /system.slice/smb.service
...

You need to make some changes to the Samba config file and create share samba directories. To do these, follow the steps below.

Samba Global Settings

At this point, you need to open the Samba configuration file with your favorite text editor, here we use the vi editor:

sudo vi /etc/samba/smb.conf

Under the Global section, find the line below and make sure it is like this:

workgroup = WORKGROUP

When you are done, save and close the file.

At this point, you can share both public and private directories. So you can create the two directories by using the following commands:

# sudo mkdir /public
# sudo mkdir /private

Now you need to open the Samba config file again and add the shares and authentication methods to the end of the file.

sudo vi /etc/samba/smb.conf
[public]
   comment = Public Folder
   path = /public
   writable = yes
   guest ok = yes
   guest only = yes
   force create mode = 775
   force directory mode = 775
[private]
   comment = Private Folder
   path = /private
   writable = yes
   guest ok = no
   valid users = @smbshare
   force create mode = 770
   force directory mode = 770
   inherit permissions = yes

When you are done, save and close the file.

At this point, you need the Samba share user group to access the Private share as specified in the config file above.

Create the group by using the command below:

sudo groupadd smbshare

Set the correct permissions for the private share by using the commands below:

# sudo chgrp -R smbshare /private/
# sudo chgrp -R smbshare /public

Next, set the correct permissions for the directories:

# sudo chmod 2770 /private/
# sudo chmod 2775 /public

Note: The value 2 at the beginning of the above commands, stands for the SGID bit. This allows newly created files to inherit the parent group.

Create a Samba no-login Local User

Now you should create a no-login local user to access the private share by using the command below:

sudo useradd -M -s /sbin/nologin sambauser

Then, add the user to the Samba share group on Rocky Linux 8 with the following command:

sudo usermod -aG smbshare sambauser

Finally, set a password for your Samba user:

sudo smbpasswd -a sambauser
**Output**
New SMB password:
Retype new SMB password:
Added user sambauser.

Enable the created account by using the following command:

sudo smbpasswd -e sambauser
**Output**
Enabled user sambauser.

Verify Samba Configuration

When you are done with the above steps, you can test your Samba configuration on Rocky Linux 8 that is working correctly or not with the following command:

sudo testparm
**Output**
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Weak crypto is allowed

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
        printcap name = cups
        security = USER
        idmap config * : backend = tdb
        cups options = raw

[homes]
        browseable = No
        comment = Home Directories
        inherit acls = Yes
        read only = No
        valid users = %S %D%w%S

[printers]
        browseable = No
        comment = All Printers
        create mask = 0600
        path = /var/tmp
        printable = Yes

[print$]
        comment = Printer Drivers
        create mask = 0664
        directory mask = 0775
        force group = @printadmin
        path = /var/lib/samba/drivers
        write list = @printadmin root

[public]
        comment = Public Folder
        force create mode = 0775
        force directory mode = 0775
        guest ok = Yes
        guest only = Yes
        path = /public
        read only = No

[private]
        comment = Private Folder
        force create mode = 0770
        force directory mode = 0770
        inherit permissions = Yes
        path = /private
        read only = No
        valid users = @smbshare

This means that everything is configured appropriately.

At this point, you can create demo files in the Samba shares. To do this. you can run the following commands:

# sudo mkdir /private/demo-private /public/demo-public
# sudo touch /private/demo1.txt /public/demo2.txt

To apply the changes, restart the Samba service on Rocky Linux 8:

sudo systemctl restart nmb

Before you set up Samba clients, you can try accessing your share files with the command below on Rocky Linux 8:

smbclient '\localhostprivate' -U sambauser
**Output**
Password for [WORKGROUPsambauser]:
Try "help" to get a list of possible commands.
smb: > ls
  .                                   D        0  Mon Mar 27 04:00:26 2023
  ..                                  D        0  Mon Mar 27 03:56:04 2023
  demo1.txt                           N        0  Mon Mar 27 04:00:26 2023
  demo-private                        D        0  Mon Mar 27 04:00:20 2023

                51024384 blocks of size 1024. 45362236 blocks available
smb: >

At this point, we want to show you access to the share from Windows. First, open a run box using Win+R and enter your Rocky Linux 8 IP address in the box and click Ok:

Run Box, open Samba share
Run Box

You will see the following screen, you should enter your Samba user credentials and click Ok.

Connect to samba share Rocky Linux 8
Samba Credentials

Then, the Samba shared folders on Rocky Linux 8 should appear as below:

Samba share files
Samba share files

You can open one of the files, and create a new file there. You should see the file on your server machine too.

Mount Network Drive

At this point, you can mount the Samba share permanently on your Windows system. Right-Click on This PC->Map Network Drive. This will open a window for you, provide the Path details and click Finish.

Map Samba Network Folder
Map Samba Network Folder

Then, enter the Samba user credentials and click ok.

You will have the share available on your This PC.

At this point, you can access the share folders from a Linux client. To do this, you need to have Samba packages installed on your server. Here our Linux client is Rocky Linux 8:

sudo dnf install samba samba-common samba-client -y

Then, navigate to File manager->Other locations and add your share using the syntax below.

smb://server-name/Share_name

Enter the credentials for the samba user. That is it! You have your Samba share on your Linux client machine.

Conclusion

A Samba share file is used for sharing files and directories between computers on a network, allowing access and collaboration between systems running different operating systems, such as Linux, Windows, and macOS. At this point, you have learned to Install and Configure Samba Share on Rocky Linux 8.

Hope you enjoy using it. You may also like these articles:

Install Apache Kafka on Rocky Linux 8

Install OpenJDK 19 on Rocky Linux 8

How To Install OpenSSL 3 on Rocky Linux 8

TensorFlow Python Rocky Linux 8

Laravel PHP Setup Rocky Linux 8

Upgrading Linux Kernel For Rocky Linux 8

Netdata Monitoring Install Rocky Linux 8

Erlang Programming Language Rocky Linux 8

Alternative Solutions for File Sharing on Rocky Linux 8

While Samba is a robust and widely used solution for file sharing between Linux and Windows systems, alternative methods exist that may be more suitable depending on specific requirements and network environments. Here are two different ways to solve the problem of file sharing on Rocky Linux 8:

1. Using NFS (Network File System)

NFS is a distributed file system protocol that allows users to access files over a network as if they were stored locally. It’s primarily designed for Unix-like systems, making it a great choice when sharing files between Linux machines.

Explanation:

NFS offers advantages in terms of performance and simplicity when all clients are Linux-based. It avoids the overhead of translating file permissions and protocols required by Samba. However, it’s generally less secure than Samba when used with Windows clients, and requires more configuration on Windows (third-party NFS client software). The process to Install and Configure Samba Share on Rocky Linux 8 is different from NFS.

Implementation:

  1. Install NFS Server:

    sudo dnf install nfs-utils -y
  2. Create a Shared Directory:

    sudo mkdir /nfs_share
    sudo chown nfsnobody:nfsnobody /nfs_share
    sudo chmod 777 /nfs_share
  3. Configure /etc/exports:

    Edit the /etc/exports file to define the shared directory and client access.

    sudo vi /etc/exports

    Add a line like this (replace 192.168.1.0/24 with your network):

    /nfs_share 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
    • rw: Read-write access.
    • sync: Forces writes to be synchronous.
    • no_root_squash: Allows root user on the client to have root privileges on the shared directory. Use with caution!
    • no_all_squash: Don’t map all uids/gids to the anonymous user.
  4. Export the Share and Enable NFS Service:

    sudo exportfs -a
    sudo systemctl enable --now nfs-server rpcbind
    sudo systemctl status nfs-server
  5. Configure Firewall:

    sudo firewall-cmd --permanent --add-service=nfs
    sudo firewall-cmd --permanent --add-service=mountd
    sudo firewall-cmd --permanent --add-service=rpc-bind
    sudo firewall-cmd --reload
  6. On the Client (Another Linux Machine):

    Install NFS client tools:

    sudo dnf install nfs-utils -y

    Create a mount point:

    sudo mkdir /mnt/nfs_share

    Mount the NFS share:

    sudo mount <server_ip>:/nfs_share /mnt/nfs_share

    (Replace <server_ip> with the IP address of your Rocky Linux 8 server.)

  7. Make the Mount Permanent (Optional):

    Add a line to /etc/fstab to mount the share automatically on boot:

    sudo vi /etc/fstab

    Add a line like this:

    <server_ip>:/nfs_share /mnt/nfs_share nfs defaults 0 0

2. Using SSHFS (SSH File System)

SSHFS allows you to mount a directory on a remote server over an SSH connection. This is a very secure method as it leverages the encryption and authentication mechanisms of SSH. It’s particularly useful when connecting to servers over the internet. This is different from the steps to Install and Configure Samba Share on Rocky Linux 8.

Explanation:

SSHFS is simple to set up and requires minimal configuration. Since it relies on SSH, it’s inherently secure. However, performance might be slightly lower compared to NFS or Samba, especially with large files or high latency connections.

Implementation:

  1. Install SSHFS on the Client:

    sudo dnf install fuse-sshfs -y
  2. Create a Mount Point:

    sudo mkdir /mnt/sshfs_share
  3. Mount the Remote Directory:

    sshfs <user>@<server_ip>:<remote_directory> /mnt/sshfs_share
    • <user>: Username on the remote server.
    • <server_ip>: IP address of the remote server.
    • <remote_directory>: The directory you want to share on the remote server (e.g., /home/<user>/shared_files).

    You’ll be prompted for the user’s password on the remote server.

  4. Unmount the Share:

    fusermount -u /mnt/sshfs_share
  5. Make the Mount Permanent (Optional):

    To make the mount permanent, you can add an entry to /etc/fstab. However, this requires storing the password, which is not recommended. A more secure approach is to use SSH keys for passwordless authentication.

    First, generate an SSH key pair if you don’t already have one:

    ssh-keygen -t rsa

    Copy the public key to the remote server:

    ssh-copy-id <user>@<server_ip>

    Now, you can add an entry to /etc/fstab:

    sudo vi /etc/fstab

    Add a line like this:

    <user>@<server_ip>:<remote_directory> /mnt/sshfs_share fuse.sshfs defaults,_netdev,user,idmap=user,allow_other 0 0
    • _netdev: Indicates that this is a network filesystem and should be mounted after the network is up.
    • user: Allows a normal user to mount the filesystem.
    • idmap=user: Maps UIDs and GIDs to the local user.
    • allow_other: Allows other users on the client machine to access the mounted filesystem.

After editing /etc/fstab, run:

sudo mount -a

This guide has shown you how to Install and Configure Samba Share on Rocky Linux 8, and provided two alternative solutions, NFS and SSHFS, each with its own strengths and weaknesses. Choosing the right solution depends on your specific needs and environment. The process to Install and Configure Samba Share on Rocky Linux 8 is slightly more complicated than these options, but offers the broadest compatability.

Leave a Reply

Your email address will not be published. Required fields are marked *