exportfs Command: Tutorial & Examples
The exportfs
command is a powerful utility in Linux used to maintain the current table of exported file systems
for NFS (Network File System). This command allows you to export directories to remote users, granting them access over the network.
With exportfs
, you can manage which directories are available to which clients and with what permissions.
How It Works
exportfs
works by manipulating the /var/lib/nfs/etab
file, which keeps track of the exported file systems. When you use exportfs
to export directories, it
updates this file to reflect the current state of exports. The NFS Kernel server then reads this information to determine which
directories are accessible to remote clients.
The command interacts closely with the /etc/exports
file, which contains a list of directories to be shared and the permissions associated with them.
When exportfs
is run, it reads this file to determine the list of directories to export.
What It Is Used For
exportfs
is primarily used to:
- Export directories to specific clients or networks.
- Unexport directories that were previously exported.
- Display the current list of exported directories.
- Refresh the list of exported directories to apply changes made in the
/etc/exports
file.
Why It Is Important
The exportfs
command is vital for administrators who need to manage shared resources in a networked environment. By allowing precise control over which
directories are shared and the permissions granted to each client, exportfs
plays a critical role in ensuring both accessibility and security.
How to Use It and Common Command Line Parameters
Here are some common uses and parameters of the exportfs
command:
Display Current Exports
To display the current list of exported directories, use:
exportfs -v
Export All Directories
To export all directories listed in the /etc/exports
file, use:
exportfs -a
Unexport All Directories
To unexport all currently exported directories, use:
exportfs -u -a
Export a Specific Directory
To export a specific directory (e.g., /srv/nfs
), use:
exportfs -o rw,sync,no_subtree_check client_ip:/srv/nfs
Here, the -o
flag specifies the options for the export. Common options include:
rw
: Read-write access.ro
: Read-only access.sync
: Replies to requests only after changes have been committed to stable storage.no_subtree_check
: Disables subtree checking, improving performance.
Refresh Exports
To re-read the /etc/exports
file and apply any changes, use:
exportfs -r
Potential Problems and Pitfalls
While using exportfs
, you might encounter some common issues:
Permissions Errors
If you don't have the necessary permissions to execute the command, you might see errors like:
exportfs: /mnt/nfs: Function not implemented
Make sure you run the command as the root user or with sudo
.
Configuration Errors in /etc/exports
Errors in the /etc/exports
file can prevent directories from being exported. A common mistake is incorrect syntax or missing options. Always double-check
your /etc/exports
file for errors:
/srv/nfs client_ip(rw,sync,no_subtree_check)
Network Issues
If clients can't access the exported directories, you might be facing a network issue. Ensure that both the server and clients are properly connected and that firewalls are configured to allow NFS traffic.
Typical Output
When you run exportfs -v
, you might see output like:
/srv/nfs client_ip(rw,sync,wdelay,hide,no_subtree_check,insecure,root_squash,no_all_squash)
This output lists the exported directory, the client IP, and the options associated with the export.
Examples
Here are some practical examples of using exportfs
in a shell:
Example 1: Exporting a Directory
sudo exportfs -o rw,sync,no_subtree_check 192.168.1.100:/srv/nfs
This command exports the /srv/nfs
directory to the client with IP 192.168.1.100
with read-write access.
Example 2: Unexporting a Directory
sudo exportfs -u 192.168.1.100:/srv/nfs
This command unexports the /srv/nfs
directory for the specified client.
Example 3: Refreshing Exports
sudo exportfs -r
This command re-reads the /etc/exports
file and applies any changes.
Example 4: Displaying Current Exports
sudo exportfs -v
Displays the list of currently exported directories along with their options.
Conclusion
The exportfs
command is an essential tool for managing NFS exports in a Linux environment. By understanding how to use it effectively, you can ensure that
your network shares are configured correctly and securely. Whether you're exporting directories, unexporting them, or simply checking the current state of
exports, exportfs
provides the functionality you need to manage your NFS server efficiently.