Skip to main content

Working with Network File Systems

1. Installing NFS Server

Step Description Command
1 Update package list sudo apt update
2 Install NFS server package sudo apt install nfs-kernel-server
3 Start and enable NFS service sudo systemctl start nfs-server.service
4 Verify NFS service status sudo systemctl status nfs-server.service

Example: Configure NFS Export

# Edit exports file
sudo nano /etc/exports

# Add export entry
/home/nfs_client 192.168.1.0/24(rw,sync,no_root_squash)
Note: Ensure the directory to be exported exists and has appropriate permissions.
Warning: Using no_root_squash can pose security risks. Use with caution.

2. Configuring NFS Exports

Export Path Permissions Options Notes
/srv/nfs/shared rw,sync,no_root_squash all_squash,anonuid=1000,anongid=1000 Read-write access with root squashing disabled
/home/user/data ro,sync no_subtree_check Read-only export for backup data

Example: /etc/exports configuration

/srv/nfs/shared *(rw,sync,no_root_squash)
/home/user/data *(ro,sync,no_subtree_check)
Note: Ensure /etc/exports is properly formatted with one export per line and no trailing spaces.
Warning: Disabling no_root_squash can pose security risks if not properly managed.
Option Description Default Usage
rw Read and write permissions None Allows clients to read and write files
ro Read-only permissions None Prevents modification of files on the server
sync Writes are committed to disk before acknowledgment None Ensures data consistency but may impact performance
async Writes are acknowledged before being committed to disk None Improves performance but may cause data loss in case of crash
no_root_squash Maps root user to root on client Enabled Can be a security risk; use with caution
root_squash Maps root user to anonymous user (typically nobody) Enabled by default Enhances security by preventing root access

Example: Applying NFS exports

sudo exportfs -ra
sudo systemctl restart nfs-server

3. Exporting NFS Shares

Configuration File Path Description
/etc/exports /etc/exports Defines NFS shares and access permissions

Example: Basic NFS Export Configuration

/srv/nfs/client1 192.168.1.100(rw,sync,no_root_squash)
/home/user 192.168.1.0/24(rw,sync,no_root_squash)
Note: The /etc/exports file must be owned by root and not world-writable.
Warning: Using no_root_squash can pose security risks as it allows root access from the client.
Export Option Description Default
rw Read-write access false
ro Read-only access false
sync Writes are committed to disk before acknowledgment false
async Writes are acknowledged before being committed to disk false
no_root_squash Root user on client has root access to the share false
root_squash Root user on client is mapped to anonymous user (nobody) true
all_squash All users are mapped to anonymous user false

Steps to Export an NFS Share

  1. Edit /etc/exports to define the share and client access
  2. Run exportfs -ra to reload the exports
  3. Ensure nfs-kernel-server service is running
  4. Verify the export with showmount -e localhost

4. Mounting NFS Shares

Option Values Notes
mount.nfs nfs File system type for NFS mounts
server 192.168.1.100 NFS server IP address or hostname
export /shared/data Exported directory on the NFS server
mount_point /mnt/nfs_share Local mount point directory

Example: Mounting an NFS Share

sudo mount -t nfs 192.168.1.100:/shared/data /mnt/nfs_share

Example: Mounting with Options

sudo mount -t nfs -o vers=4,hard,intr 192.168.1.100:/shared/data /mnt/nfs_share
Note: Ensure the NFS server is exporting the directory and that the client has proper network access.
Warning: Mounting NFS shares requires root privileges or appropriate permissions.

5. Configuring NFS Client

Configuration File Path Description
/etc/fstab /etc/fstab File system table used to define mount points and options for NFS shares.

Example: Mounting an NFS Share

nfs-server:/exported/path /local/mount/point nfs defaults,timeo=600,hard,intr 0 0
Note: Ensure the NFS server allows client connections and that the mount point exists locally.
Mount Option Description Default Value
timeo Timeout for NFS operations in tenths of a second. 600
hard Retry indefinitely on server errors. hard
intr Allow NFS operations to be interrupted. intr
rw Read-write access to the mounted file system. rw
ro Read-only access to the mounted file system. ro

Example: Mounting with Specific Options

mount -t nfs -o timeo=600,hard,intr nfs-server:/exported/path /local/mount/point

6. Installing Samba Server

Step Description Command
1 Update package list sudo apt update
2 Install Samba packages sudo apt install samba samba-common-bin
3 Start and enable Samba service sudo systemctl start smbd && sudo systemctl enable smbd
4 Check service status sudo systemctl status smbd

Example: Basic Samba Configuration

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
Note: Ensure the firewall allows traffic on port 445 and 139 for Samba.

7. Configuring Samba Shares

Property Value Description
[global] Global configuration section for Samba
server string Samba Server %v Server description shown to clients
workgroup WORKGROUP Workgroup name for the Samba server
security user Security mode (user, share, or server)
map to guest Bad User How to handle guest access (if enabled)

Example: Basic Samba Configuration

[global]
   server string = Samba Server %v
   workgroup = WORKGROUP
   security = user
   map to guest = Bad User

[shared]
   path = /srv/samba/shared
   browseable = yes
   writable = yes
   guest ok = yes
   read only = no
Note: Ensure the directory /srv/samba/shared exists and has appropriate permissions.

Example: Setting Up Samba Share with User Authentication

# Create the directory
sudo mkdir -p /srv/samba/shared

# Set ownership and permissions
sudo chown nobody:nogroup /srv/samba/shared
sudo chmod 777 /srv/samba/shared

# Add user to Samba database (if using user mode)
sudo smbpasswd -a username
Option Values Description
path /srv/samba/shared Directory to share
browseable yes Allow clients to browse the share
writable yes Allow write access to the share
guest ok yes Allow guest access (if security = share)
read only no Set to yes to make the share read-only

Workflow: Setting Up Samba Share

  1. Create a directory to share
  2. Set proper ownership and permissions
  3. Add the share to /etc/samba/smb.conf
  4. Restart Samba service
  5. Test the share from a client
Warning: Ensure that the Samba service is running and firewall rules allow SMB traffic (port 445).

8. Creating Samba Users

Command Description Notes
sudo smbpasswd -a username Add a new Samba user Creates a new Samba user with the specified username. The user will be prompted to set a password.
sudo smbpasswd -d username Disable a Samba user Disables the specified Samba user account. The user will not be able to authenticate to the Samba server.
sudo smbpasswd -e username Enable a Samba user Enables the specified Samba user account. The user will be able to authenticate to the Samba server again.
sudo smbpasswd -x username Delete a Samba user Deletes the specified Samba user account from the Samba password database. The user's home directory and files are not deleted.

Example: Adding a new Samba user

sudo smbpasswd -a john_doe
New SMB password: <em>[Enter new password]</em>
Retype new SMB password: <em>[Re-enter password]</em>
Added user john_doe.

Example: Disabling a Samba user

sudo smbpasswd -d john_doe
Account for john_doe is now disabled.
Note: The user must exist in the system before creating a Samba user. Use sudo useradd username to create the system user if needed.
Warning: Ensure that the Samba service is running before attempting to create or manage users. You can start it with sudo systemctl start smbd.

9. Mounting CIFS Shares

Option Values Description
username user Username for authentication
password pass Password for authentication
domain WORKGROUP Domain or workgroup name
uid 1000 User ID to map to
gid 1000 Group ID to map to
iocharset utf8 Character set for I/O operations
file_mode 0755 File permissions for mounted files
dir_mode 0755 Directory permissions for mounted directories

Example: Mounting a CIFS Share

sudo mount -t cifs //server/share /mnt/mountpoint -o username=user,password=pass,domain=WORKGROUP
Note: Ensure the cifs-utils package is installed before attempting to mount CIFS shares.
Warning: Avoid embedding passwords in command lines as they can be visible in process lists. Use credentials files instead.

10. Using autofs for Auto-Mounting

autofs is a Linux service that automatically mounts and unmounts filesystems on demand. It's particularly useful for managing network file systems like NFS or CIFS, where you want to avoid keeping them mounted continuously.

Configuration File Path Description
/etc/auto.master /etc/auto.master Master map file that defines the mount points and their corresponding maps.
/etc/auto.nfs /etc/auto.nfs Map file containing the actual mount definitions for NFS shares.

Example: Basic autofs configuration

# /etc/auto.master
/nfs /etc/auto.nfs --timeout=60

# /etc/auto.nfs
myshare -rw,sync server.example.com:/export/myshare
Note: The --timeout=60 option in /etc/auto.master specifies that the mount will be automatically unmounted after 60 seconds of inactivity.

Steps to set up autofs

  1. Edit /etc/auto.master to define the mount point and map file.
  2. Create or edit the map file (e.g., /etc/auto.nfs) to specify the remote filesystems.
  3. Start and enable the autofs service: systemctl start autofs && systemctl enable autofs.
  4. Access the mount point to trigger the auto-mount.

Key Features of autofs

  • Automatically mounts filesystems only when accessed.
  • Un-mounts filesystems after a period of inactivity.
  • Supports various network filesystem types (NFS, CIFS, etc.).
  • Can be configured with custom timeout values.

Was this article helpful?