Set up Samba Share on AlmaLinux 9: Best File Sharing

Posted on

Set up Samba Share on AlmaLinux 9: Best File Sharing

Set up Samba Share on AlmaLinux 9: Best File Sharing

In this tutorial, we want to teach you to Set up Samba Share on AlmaLinux 9. Samba is a potent tool that allows you to create seamless file and printer sharing to SMB/CIFS clients from a Linux server/desktop. With Samba you can even connect that Linux machine to a Windows Domain.

Now follow the steps below on the Orcacore website to install Samba on your Linux server and set up the Samba clients on both Linux and Windows.

To complete this guide, 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 AlmaLinux 9.

1. Install Samba on AlmaLinux 9

Samba packages are available in the default AlmaLinux repository. First, update your local package index with the command below:

sudo dnf update -y

Then, use the following command to install Samba:

sudo dnf install samba samba-common samba-client

This will install the dependencies and required packages.

Then, use the commands below to start and enable the Samba service on Almalinux 9:

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

Verify your Samba service is active and running:

sudo systemctl status smb.service
Verify Samba service is active and running AlmaLinux 9

2. Configure Samba on AlmaLinux 9

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

Set Samba Global Settings

At this point, you need to open the Samba configuration file with your favorite text editor, here we use 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 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 AlmaLinux 9 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 AlmaLinux 9 that is working correctly or not with the following command:

sudo testparm
Verify Samba Configuration AlmaLinux 9

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 AlmaLinux 9:

sudo systemctl restart nmb

Before you set up Samba clients, you can try accessing your shared files with the command below on AlmaLinux 9:

smbclient '\localhostprivate' -U sambauser
Access Share Files From Local Machine

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 AlmaLinux 9 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 AlmaLinux 9
Samba Credentials

Then, the Samba shared folders on AlmaLinux 9 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 AlmaLinux:

sudo dnf install samba samba-common samba-client

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

At this point, you have learned to Set up Samba Share on AlmaLinux 9. And also, you have learned to set up Samba clients on both Windows and Linux machines. This guide demonstrates the power and flexibility of Set up Samba Share on AlmaLinux 9.

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

Reset Root Password on AlmaLinux 9

How To Install Gradle on AlmaLinux 9

Count Files in a Linux Directory with Examples

Work With FFmpeg on Linux with Examples

Install Heimdall Application Dashboard on Linux

Network Bridge Config on Debian 12

Learn Iptables on Debian Linux

Powershell script under Linux

Alternative Solutions for File Sharing on AlmaLinux 9

While Samba is a robust solution, other methods exist to share files on AlmaLinux 9. Here are two alternative approaches:

1. Using NFS (Network File System)

NFS is another file-sharing protocol primarily used in Unix-like operating systems. Unlike Samba, which is designed for interoperability with Windows, NFS is generally preferred for sharing files between Linux/Unix systems due to its performance and simpler configuration (in some cases).

Explanation:

NFS allows you to mount a directory on a server to a client machine, making it appear as if the shared directory is a local drive. This is achieved by configuring the server to export the desired directory and then configuring the client to mount it. NFS relies on RPC (Remote Procedure Call) for communication.

Steps:

  1. Install NFS Server and Client Packages:

    On the server (AlmaLinux 9), install the NFS server:

    sudo dnf install nfs-utils -y
    sudo systemctl enable --now nfs-server
    sudo systemctl enable --now rpcbind

    On the client (AlmaLinux 9 or another Linux distribution), install the NFS client:

    sudo dnf install nfs-utils -y
  2. Configure NFS Exports:

    Edit the /etc/exports file on the server to specify the directories to be shared and the clients that can access them. For example, to share the /public directory to any client on the 192.168.1.0/24 network, add the following line:

    /public 192.168.1.0/24(rw,sync,no_subtree_check)
    • rw: Allows read and write access.
    • sync: Forces changes to be written to disk before the server replies.
    • no_subtree_check: Disables subtree checking, which can improve performance.

    Export the shared directories:

    sudo exportfs -a
  3. Configure Firewall:

    Allow NFS traffic through the firewall on the server:

    sudo firewall-cmd --permanent --add-service=nfs
    sudo firewall-cmd --permanent --add-service=rpc-bind
    sudo firewall-cmd --permanent --add-service=mountd
    sudo firewall-cmd --reload
  4. Mount the NFS Share on the Client:

    Create a mount point on the client:

    sudo mkdir /mnt/public

    Mount the NFS share:

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

    Replace <server_ip> with the IP address of the NFS server.

  5. Make the Mount Persistent (Optional):

    To automatically mount the NFS share on boot, add an entry to /etc/fstab:

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

    Code Example (fstab entry):

    192.168.1.100:/public /mnt/public 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 method is simple to set up and secure, as it leverages the existing SSH infrastructure. It is particularly useful for sharing files between systems where SSH access is already configured.

Explanation:

SSHFS works by establishing an SSH connection to the remote server and then using the SFTP (SSH File Transfer Protocol) to transfer files. The client machine mounts the remote directory, making it accessible as a local drive.

Steps:

  1. Install SSHFS:

    On both the client and server (if not already installed), install SSHFS:

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

    On the client, create a directory where you want to mount the remote share:

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

    Use the sshfs command to mount the remote directory:

    sudo sshfs <user>@<server_ip>:<remote_directory> /mnt/sshshare
    • <user>: The username on the remote server.
    • <server_ip>: The IP address of the remote server.
    • <remote_directory>: The path to the directory on the remote server that you want to share (e.g., /home/user/shared).

    Example:

    sudo sshfs user1@192.168.1.100:/home/user1/shared /mnt/sshshare

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

  4. Unmounting the Share:
    To unmount the share, you can use the command:

        sudo umount /mnt/sshshare
  5. Make the Mount Persistent (Optional):

    To make the mount persistent, you need to add an entry in /etc/fstab. However, since SSHFS requires user interaction (password), it’s a bit more complex. You can use keys for authentication instead of passwords.

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

    ssh-keygen -t rsa

    Copy the public key to the server:

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

    Now, you can add the following entry to /etc/fstab:

    sshfs#<user>@<server_ip>:<remote_directory> /mnt/sshshare fuse.sshfs defaults,_netdev,user,idmap=user,follow_symlinks,allow_other 0 0
    • _netdev: Ensures that the filesystem is only mounted after the network is available.
    • user: Allows non-root users to mount the filesystem.
    • idmap=user: Maps the remote user ID to the local user ID.
    • follow_symlinks: Follow symbolic links on the server.
    • allow_other: Allows other users to access the mount.

    Code Example (fstab entry):

    sshfs#user1@192.168.1.100:/home/user1/shared /mnt/sshshare fuse.sshfs defaults,_netdev,user,idmap=user,follow_symlinks,allow_other 0 0

These alternative methods, NFS and SSHFS, offer viable solutions for file sharing on AlmaLinux 9, each with its own advantages and disadvantages. NFS is generally preferred for performance in Linux-to-Linux environments, while SSHFS provides a secure and straightforward solution leveraging existing SSH infrastructure. The best choice depends on your specific needs and environment. Properly Set up Samba Share on AlmaLinux 9 can provide great benefits, it is important to know the alternatives.

Leave a Reply

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