1. Introduction

File servers are dedicated machines responsible for storing, managing, and providing access to files for authorized clients on a computer network. In particular, the server disk (usually an HDD or SSD) stores the files instead of each individual client. However, all clients on the computer network can access the same files.

In this tutorial, we’ll learn how to set up a file server on a Linux machine with Samba and Network File System (NFS).

This guide assumes two Ubuntu 22.04 servers: one acting as a file server and the other as a client. Both servers must be connected to the same computer network.

2. Setting up a File Server With Samba

Samba is an open-source software product that’s the free implementation of the SMB (Server Message Block) protocol.

Samba is cross-platform interoperable, meaning it enables file sharing across different operating systems such as Windows, Linux, and macOS. It also has extra features such as client authentication and encryption to safeguard data.

In this section, we’ll configure Samba on the server machine to share files over the network with the client machine. Then, we’ll learn how to access the shared files on the client machine.

2.1. Install Samba

We can install Samba on the server machine using apt.

Let’s update the package repositories to ensure we install the latest versions:

$ sudo apt-get update

Then, we perform the installation:

$ sudo apt-get install samba

This command installs and starts the Samba smbd service.

To ensure security, it’s best to stop the Samba service before making configuration changes and then restart it afterward:

$ sudo systemctl stop smbd

The above command stops the Samba smbd service.

2.2. Create Samba Share Directory

On the file server machine, let’s create a directory named clientshare:

$ mkdir clientshare

Then, we create some files within:

$ cd clientshare
$ touch file1 file2

The purpose of creating these files is for testing on the client machine.

2.3. Backup Samba Configuration File

The /etc/samba/smb.conf file is the main configuration file for Samba. It contains all the directives that define how Samba operates.

The file has two main sections:

  • [global]
  • sharename]

Rather than making changes directly to the smb.conf file, we’ll rename the original to smb.conf.original:

$ sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.original

Then, we create a new empty smb.conf file:

$ touch /etc/samba/smb.conf

This is a common practice before making changes to critical configuration files. In other words, renaming the original file to smb.conf.original creates a backup copy. Doing so enables us to revert to the original configuration if something goes wrong after making edits.

2.4. Set Samba Global Options

The [global] section of this file defines the server role (standalone server), user mapping for guest access, and allowed access restrictions based on the IP address subnet.

Let’s edit the smb.conf file in a text editor such as vim:

$ sudo vim /etc/samba/smb.conf

In particular, we add the the [global] configuration:

[global]
       server role = standalone server
           map to guest = Bad User
       usershare allow guests = yes
       hosts allow = 192.168.0.0/16
       hosts deny = 0.0.0.0/0

This configuration denies guest access through mapping but allows access to user shares with guest permissions enabled. Then, the network access is limited to a specific subnet (192.168.0.0/16).

Now, let’s save and exit the editor.

2.5. Test Current Configuration

However, whenever we edit the smb.conf, it’s recommended to run testparm, a Samba command-line utility to verify the syntax of the file:

$ testparm
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]
    server role = standalone server
        map to guest = Bad User
    usershare allow guests = Yes
    idmap config * : backend = tdb
    hosts allow = 192.168.0.0/16
    hosts deny = 0.0.0.0/0

Hence, we’ve configured the [global] option for the Samba file server.

2.6. Set Samba Share Name Options

The [sharename] section creates a blueprint for a specific directory or resource we want to share with clients over the network using Samba. Additionally, multiple [sharename] sections can be present in the smb.conf file, enabling us to share various directories independently.

Let’s create a [sharename] section named testshare in the smb.conf file and add the relevant configurations:

[testshare]
       comment = test file share
       guest ok = yes
           path = /home/path_to_samba_share_directory
       read only = no

The configuration specifies [testshare] as the resource to be shared. In this case, this is the Samba share directory we created in the previous section. Then, the configuration forces ownership of all files within the shared directory to the server user and group of choice. Finally, it allows access for anyone, including guest users, and grants them read and write permissions.

Again, let’s execute testparm after saving:

$ testparm
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]
    server role = standalone server
        map to guest = Bad User
    usershare allow guests = Yes
    idmap config * : backend = tdb
    hosts allow = 192.168.0.0/16
    hosts deny = 0.0.0.0/0


[testshare]
    comment = test file share
    force group = server_group
    force user = server_user
    guest ok = Yes
    path = /home/path_to_samba_share_directory
    read only = No

The output confirms we have no error in the syntax of the smb.conf file.

2.7. Create Samba User Account

To log in from the client machine, we set up a Samba user account on the server machine:

$ smb passwd -a username

Then, we enable the created user:

$ smbpassword -e username

This completes the configurations for the Samba file server.

2.8. Access Share Directory on Client

Now, we can access the shared directory over the network with Samba.

To begin with, we install smbclient on the client:

$ sudo apt-get install smbclient

smbclient is a command-line tool that connects clients to remote directories shared by Samba servers. This tool enables us to connect to the shared directory on the Samba server.

Once it’s installed, we connect to the Samba server:

$ smbclient //servers_ip/testshare -U samba_username

The above command attempts to connect to a Samba share named [testshare] on the Samba server via its IP address with the username we set up earlier. If successful, it provides a command-line interface (CLI) to manage files and directories within the share.

Let’s list all files or directories in the Samba CLI:

smb: \> ls
  .                                   D        0  Thu Mar 28 01:21:28 2024
  ..                                  D        0  Thu Mar 28 01:43:40 2024
  file1                               N        0  Thu Mar 28 01:21:28 2024
  file2                               N        0  Thu Mar 28 01:21:28 2024

        40581564 blocks of size 1024. 38768520 blocks available
smb: \>

As seen above, the files we created in the previous steps are visible in the Samba share.

3. Setting up a File Server With NFS

NFS, short for Network File System, is a distributed file system protocol used to access files and directories on a remote server. The NFS server runs on the machine with the storage we want to share. On the other hand, we use the mount command with the NFS server’s IP address to access the share on the client machine.

3.1. Configure NFS Share

First, we install nfs-kernel-server, nfs-common, and rpcbind on the server machine as the prerequisites for NFS:

$ sudo apt install nfs-kernel-server nfs-common rpcbind

Next, we create an NFS share directory with the path /var/nfs/public:

$ sudo mkdir -p /var/nfs/public

Then, we grant all permissions to everyone:

$ sudo chmod 777 /var/nfs/public/

However, sharing this directory with the client machine requires defining an export rule in the /etc/exports file:

$ sudo echo /var/nfs/public client_machine_ip(rw,sync,no_subtree_check) >> /etc/exports

This export rule allows the client machine with the specified IP address to read and modify files within the share and prioritize data consistency over performance (due to sync).

To verify the changes, let’s reboot the server and then confirm their implementation:

$ sudo reboot
$ showmount -e
Export list for server_username:
/var/nfs/public client_machine_ip

The output confirms that the export rule has been successfully created.

3.2. Configure NFS Client Mount

The client machine, identified by its IP address, mounts the file system provided by the NFS server.

We begin by creating a mount point named nfs-public and granting it permissions:

$ mkdir /mnt/nfs-public
$ sudo chmod 777 /mnt/nfs-public

Next, we install the prerequisites needed for the client machine to access the NFS server:

$ sudo apt install rpcbind nfs-common

These packages enable the client machine to connect to the NFS server.

3.3. Mount NFS Share on Client

Now we can mount the NFS share:

$ sudo mount server_machine_ip:/var/nfs/public /mnt/nfs-public/

However, the client machine unmounts the mount point during a reboot. To make it permanent, let’s add the mount to /etc/fstab:

$ sudo echo '#NFS server
server_machine_ip:/var/nfs/public /mnt/nfs-public nfs rw 0 0' >> /etc/fstab

Thus, we enable automatic mounting of the NFS share at boot time.

3.4. Access Share

Once it’s mounted, we can access files and directories within the NFS share using the mount point directory. We can execute commands like cd, ls, and cp to interact with shared files.

For example, we can create a directory named testdir in the mount point:

$ cd /mnt/nfs-public
$ mkdir testdir

At this point, testdir should be reflected in the NFS share directory on the server machine. Let’s confirm that on the server machine:

$ cd /var/nfs/public
$ ls
testdir

As seen above, the created directory is also available in the NFS share.

4. Conclusion

In this article, we learned how to set up a simple file server using Samba and NFS. We explored how to configure Samba and NFS for sharing files across networks.

By understanding these tools and instructions, we can establish secure file sharing between machines and streamline collaboration, backups, and more.