nfs and smb

Linux Two Important Services: NFS and Samba

Introduction

If you are preparing for the RHCSA or working as a Linux sysadmin, understanding file sharing services is essential. This blog covers two of the most commonly used file sharing protocols NFS for Linux-to-Linux sharing and Samba (SMB) for Linux-to-Windows sharing along with their complete setup steps on RHEL/CentOS systems.

What is NFS and Samba?

NFS stands for Network File System. It is used for sharing files between Linux systems over a network.

Samba (SMB) stands for Server Message Block. It is used for sharing files between Windows and Linux machines.

Which Ports Do NFS and SMB Use?

  • NFS uses port 2049
  • SMB uses port 445

NFS Setup Steps

Step 1: Install the nfs-utils Package

sudo dnf install -y nfs-utils

Step 2: Set Up the Export Directory

File: /etc/exports

sudo vim /etc/exports

Entry format inside the file:

/shared_dir/    network(rw,sync,no_root_squash)

Example:

/mnt/nfsdata    192.168.1.0/24(rw,sync,no_root_squash)

Note: Never put a space between the network address and the options. 192.168.1.0/24 (rw,sync,no_root_squash) — this is NOT wrong syntax, but what it means is that the network and the options are treated as two separate entities. The system will then grant access to all networks with those options, which is a security loophole.

Note: no_root_squash means that if a root user from another system accesses this shared NFS directory, they will also gain root-level access to it. root_squash (the default) treats that remote root user as a regular user instead.

Step 3: Set the Correct SELinux Context

Context type: nfs_t

sudo semanage fcontext -a -t nfs_t “/dir(/.*)?””

sudo restorecon -Rv /dir

Note: (/.*)? means all files and directories inside the target directory.

Step 4: Allow the Service Through the Firewall

sudo firewall-cmd –add-service=nfs –permanent

sudo firewall-cmd –add-service=mountd –permanent

sudo firewall-cmd –add-service=rpc-bind –permanent

sudo firewall-cmd –reload

Note: mountd and rpc-bind are required by older NFS versions. In NFSv4, allowing only the nfs service through the firewall is sufficient. To check your version: rpm -q nfs-utils

  • rpc-bind tells the system which port mountd or nfsd needs to use.
  • mountd checks the mount table, looks up the IP address trying to access the share, and allows the client to mount if a matching entry exists.

Step 5: Enable and Start the Service

sudo systemctl enable nfs-server.service –now

Useful commands:

  • Find the service name: sudo systemctl list-unit-files | grep nfs
  • Browse NFS-related man pages: man -k nfs
  • If the service is already running and you add a new entry to /etc/exports, reload without restarting:

sudo exportfs -ra

Step 6: Verify the Mount Point

showmount -e

showmount -e 192.168.1.0

Samba Setup Steps

Step 1: Install Samba Packages

sudo dnf install samba -y

sudo dnf install samba-client -y

sudo dnf install samba-common -y

Step 2: Create a Samba User

Note: A Samba user is also a regular Linux user, but we do not give them a home directory or login shell for security reasons. In short, Samba users are credentials we provide to non-Linux (i.e., Windows) users so they can access shared files from the Linux machine. Samba passwords and Linux passwords are two separate things.

sudo useradd -M -s /bin/nologin smbuser

sudo smbpasswd -a smbuser

You will then be prompted to set a password for Samba access.

Step 3: Configure the smb.conf File

sudo vim /etc/samba/smb.conf

Add the following block at the bottom (the [global] section must always appear above any share definitions):

[ShareName]

    comment = Your description here

    path = /shared/sambadir

    writable = yes

    readable = yes

    browsable = yes

    valid users = smbuser

Note: The valid users directive controls which Samba users can see and access this share on a Windows machine. Incorrect values here will cause permission denials.

Step 4: Check the Syntax

testparm

Step 5: Set the Correct SELinux Context

sudo semanage fcontext -a -t samba_share_t “/shared/sambadir(/.*)?””

sudo restorecon -Rv /shared/sambadir

Note: Incorrect SELinux context will also cause permission denials.

Step 6: Allow Samba Through the Firewall

sudo firewall-cmd –add-service=samba –permanent

sudo firewall-cmd –reload

Step 7: Enable and Start the Service

sudo systemctl enable smb.service –now

If you make changes to the config file afterward:

sudo systemctl restart smb.service

Useful commands:

  • Find the service name: sudo systemctl list-unit-files | grep smb
  • Browse Samba-related man pages: man -k smb

Conclusion

Both NFS and Samba are essential file sharing services that a sysadmin uses regularly. NFS is the go-to for Linux-to-Linux sharing, while Samba bridges Linux and Windows environments seamlessly. Master both and you will be well-prepared for real-world sysadmin work and the RHCSA exam.