Setting Up NFS Server on CentOS 7
This guide uses CentOS 7. Note: CentOS 6 is end-of-life; using a newer distribution is recommended.
1. Basic Server Security
Before installing services, perform basic security hardening.
1.1 Change SSH Port
Edit the SSH configuration file:
vi /etc/ssh/sshd_config
Find the line #Port 22, uncomment it, and change 22 to another port (e.g., 2222). Save, exit, and restart the SSH service:
systemctl restart sshd
Note: Ensure the firewall allows the new port and reconnect using it to avoid being locked out.
1.2 Create a Regular User
Use a regular user for daily operations:
useradd xiaohost
passwd xiaohost
1.3 Disable Root SSH Login
Edit the SSH configuration file again:
vi /etc/ssh/sshd_config
Find PermitRootLogin and set it to no. Save, exit, and restart SSH:
systemctl restart sshd
2. Update the System
Update package lists before installing software:
yum -y update
Note: yum update updates all packages to the latest versions from repositories, handling dependencies. It differs slightly from yum upgrade, which may remove obsolete dependency packages. Choose based on your needs.
3. Install NFS Service
Install the required NFS server packages:
yum install -y nfs-utils
4. Configure NFS Share
The main NFS configuration file is /etc/exports. Create it if it doesn't exist.
vim /etc/exports
Key Files and Commands:
- Configuration file:
/etc/exports– Defines shared directories and client access permissions. - Server management command:
/usr/sbin/exportfs– Manages exported file systems. - Log files: Located in
/var/lib/nfs/.etabrecords exported directories and detailed permissions;xtabrecords connected client information. - Client query command:
/usr/sbin/showmount– Used by clients to view shared resources on the NFS server.
A simple configuration example (add to /etc/exports):
/data/share 192.168.1.0/24(rw,sync,no_root_squash)
This shares the /data/share directory to the 192.168.1.0/24 subnet with read-write (rw) permissions, synchronous writes (sync), and allows root users to retain their privileges (no_root_squash). After configuration, start and enable the NFS service:
systemctl start nfs-server
systemctl enable nfs-server
Then use exportfs -arv to re-export the configuration and apply changes immediately.