CentOS 7 – Setting up Samba and NFS for file sharing

How to install docker on windows 10

Samba is an open source project with the goal of providing a secure and stable data and printer-sharing service across multiple operating systems (Windows, Linux, and Mac OS).

Samba works using the Session Message Block ( SMB) protocol, which is a protocol supported by most Windows OS and Mac OS to serve those clients who have access to shared resources such as Linux storage media, printers, serial ports (hardware equipment).

For the tutorial, we are going to use a CentOS 7 Samba server and Mac OS machine to use the service to have access to a shared folder.

First, we will start by installing Samba on our CentOS 7 server:


$ sudo yum install samba samba-client samba-common

Before starting the configuration, we need to make some backup, environment creation, and firewall setup. So first, we need to create a backup for the Samba configuration file to make sure not to mess up the original configuration (this is useful in case of having many updated configurations). We better keep a backup of each stage in the update process:


$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup

Then we need to create the folder that we need to share, or prepare it to be shared if it is already there. We need to set the user permissions to grant everything to that folder:


$ sudo mkdir /SharedFolder/
$ sudo chmod -R 755 /SharedFolder/

Finally, we need to open the Samba service port at the server firewall:


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

Now we can start making the necessary configuration to the Samba configuration file:


$ sudo nano /etc/samba/smb.conf

We need to make sure that we set the appropriate workgroup (usually it is set as WORKGROUP):


workgroup = WORKGROUP

And we define a shared folder.


[Shared Folder]
path = /SharedFolder
read only = no
guest ok = yes
browsable =yes
writable = yes
create mask = 0755
directory mask = 0755

We save the file and create some Samba users:


$ sudo smbpasswd –a user1

Then we type in a password and retry typing it to create a new one.

If we want some folders to be shared with a specific user, we need to add the following line to the folder definition and we need to disable guest login by changing guest ok = yes to guest ok = no:


valid users = user1

For the testing phase, we need to start the services:


$ sudo systemctl enable smb.service
$ sudo systemctl enable nmb.service
$ sudo systemctl start smb.service
$ sudo systemctl start nmb.service

Then we go to the client side and type this link at the server connection:


smb://server_ip_address

This test is used on a Mac machine. For a Windows machine, we type the following link at the Explorer address field:


\\server_ip_address

Then we will have a login screen, where we need to type the Samba user ID and password, like this:

After typing the correct user ID and password, there should be a screen showing the possible folder that this user can have access to, as follows:

Then we will have our folder accessible with the rights as defined in the configuration folder. Since we have write permissions, we can create a folder and then check whether it did actually happen on the server.

We can run our server to verify whether the change made by the client has been applied.

If we ever need to change any of the folder-sharing configuration, we must restart the services to apply the change:


$ sudo systemctl restart smb.service
$ sudo systemctl restart nmb.service

Samba is good for multiple-environment file sharing, but we should not forget about Linux’s most famous file-sharing solution— Network File System ( NFS). NFS is a client-server tool used to share data between multiple Linux systems, via mounting many remote shared folders and drives across the network to directly access those files at the client side.

We are going to establish an NFS client-server test on CentOS 7 to show how it works.

First, we need to install the service at the server:


$ sudo yum install nfs-utils nfs-utils-lib

Then we start the service responsible for ensuring the functionality of the service:


$ sudo systemctl enable rpcbind
$ sudo systemctl start rpcbind
$ sudo systemctl enable nfs-server
$ sudo systemctl start nfs-server
$ sudo systemctl start nfs-lock
$ sudo systemctl start nfs-idmap

We prepare a folder to be shared:


$ sudo mkdir /NFSsharedFolder
$ sudo chmod –R 0755 /NFSsharedFolder

Then, to export this folder and make it accessible to be mounted on the client, we go ahead and edit the /etc/export file:


$ sudo nano /etc/export

And we add the following line:


/NFSsharedFolder 172.25.0.0/16(rw,sync,no_root_squash,no_all_squash)

This line means that we are exporting this folder to all IP addresses in the network. The rw option gives write permissions to the folder, sync makes sure that the folder is always in synchronization, no_root_squash gives root privileges for the folder at the client side, and finally, no_all_squash enables the user’s authority.

After every change to the export file, we need to restart the NFS server:


$ sudo systemctl restart nfs-server

Finally, we need to open the NFS services’ ports at the server firewall. Reload the firewall to apply the changes:


$ sudo firewall-cmd --permanent --add-port=111/tcp
$ sudo firewall-cmd --permanent --add-port=875/tcp
$ sudo firewall-cmd --permanent --add-port=2049/tcp
$ sudo firewall-cmd --permanent --add-port=20048/tcp
$ sudo firewall-cmd --permanent --add-port=42955/tcp
$ sudo firewall-cmd --permanent --add-port=46666/tcp
$ sudo firewall-cmd --permanent --add-port=54302/tcp
$ sudo firewall-cmd --reload

With this step, we can say that we are done configuring the server. Now we move to the client. Just like the server, we need to install the NFS utilities:


$ sudo yum install nfs-utils nfs-utils-lib

Then start the appropriate services:


$ sudo systemctl enable rpcbind
$ sudo systemctl start rpcbind
$ sudo systemctl enable nfs-server
$ sudo systemctl start nfs-server
$ sudo systemctl start nfs-lock
$ sudo systemctl start nfs-idmap

Before mounting the network-shared folder, we need to make sure that there is a location to mount on:


$ sudo mkdir /NFSfolder
$ sudo chmod –R 755 /NFSfolder

To test mount the folder, we can use the mount command or make the system mount it automatically by editing the /etc/fstab file:


$ sudo mount -t nfs 172.25.22.10:/NFSsharedFolder/ /NFSfolder/
$ sudo nano /etc/fstab

In addition, we insert the following line:


172.25.22.10:/NFSsharedFolder/ /NFSfolder/ nfs defaults 0 0

For a faster connection, we can always use the fourth version of NFS by typing the following:


172.25.22.10:/NFSsharedFolder/ /NFSfolder/ nfs4 defaults 0 0

NFS4 has more to offer than NFS3. We would recommend it for an environment that has Linux servers that do support NFS4.

Finally, if we need to check what we have mounted on a specific NFS client, we use mount:


$ sudo mount

Comments are closed.