Next revision | Previous revision |
nfs [2023/07/10 18:41] – created - external edit 127.0.0.1 | nfs [2024/07/23 16:19] (current) – external edit 127.0.0.1 |
---|
NFS Server | **Add the NFS shares to the /etc/exports file** |
| |
You setup an NFS server by creating or editing the file /etc/exports. That file has a man page (man exports) and I encourage you to read that if you want more than my simple example. But basically, this file can look like this: | <code>nano /etc/exports</code> |
| |
# See exports(5) for a description. | <code>/nfs_share 192.168.1.1/24(rw,sync,no_subtree_check)</code> |
# This file contains a list of all directories exported to other computers. | |
# It is used by rpc.nfsd and rpc.mountd. | |
/home 192.168.0.0/24(rw,async,no_root_squash) | |
/var/www/htdocs 192.168.0.0/24(rw,all_squash,anonuid=99,anongid=99) | |
/home/ftp/pub 192.168.0.0/24(ro,sync,insecure,all_squash) | |
| |
This creates three exports, all accessible by any client with an IP address in the range 192.168.0.0/24. I'll discuss them in reverse order: | **Start the NFS and RPC daemons** |
| |
the ftp server's 'pub' directory aka the anonymous ftp area. This export will be available as read-only (the 'ro' parameter) with as safe as possible settings | <code>chmod 755 /etc/rc.d/rc.nfsd</code> |
| <code>chmod 755 /etc/rc.d/rc.rpc</code> |
| <code>/etc/rc.d/rc.nfsd start</code> |
| <code>/etc/rc.d/rc.rpc start</code> |
| |
your webserver's DocumentRoot (/var/www/htdocs) which will be available as writable, but on the server side, all writes will appear to originate from the user with the userid:groupid of 99:99 which is actually the “nobody” user. If you let the DocumentRoot tree be owned by this account (a configuration you often see), then the Web Server's CGI or PHP scripts can write files in these directories | **Export the shares** |
| |
the server's /home directory tree which can be mounted writable (the 'rw') using asynchronous transfers (faster but with a chance of data corruption in case of a server crash - 'sync' is safe but slower). User ID's (uid) and group ID's (gid) will be mapped 1-on-1 (even for user 'root' - the 'no_root_squash' option). This means, if the server knows a user 'alien' with a “uid:gid” pair of 1001:100, then alien's files in his homedirectory will appear with this uid:gid number pair on the NFS client side as well! So, if the NFS client PC also has an account 'alien' with the same “uid:gid” number pair 1001:100, this alien will be able to use the files on the server as they were his own. | <code>exportfs -a</code> |
| |
You see why it is important to create users on your LAN with the same UID (and GID) on all computers if you ever intend to install a NFS server. | Check to see if the shares are being shared |
| |
NFS client | <code>exportfs</code> |
| |
On a NFS client, you should enable the portmapper. To do so, activate the rc script and then run that script -once- manually (saves you a reboot to make it work) | |
| |
chmod +x /etc/rc.d/rc.portmap | |
/etc/rc.d/rc.portmap start | |
| |
Should you ever forget this, you will still be able to mount a NFS export, but it will take forever for the mount command to return to the prompt (if you run the mount command in a console). | =====mount nfs shares===== |
| |
Next, you should add a line to your /etc/fstab for the NFS export that you want to mount on your NFS client. Suppose your NFS server has IP Address 192.168.0.1 and it exports /home you could add this line to the fstab file: | |
| |
192.168.0.1:/home /mnt/nfs/home nfs auto,rsize=8192,wsize=8192,hard,intr 0 0 | |
| |
This NFS export will then automatically be mounted by Slackware when booting up. The manual mount command would be: | |
| |
mount -t nfs -o rsize=8192,wsize=8192,hard,intr 192.168.0.1:/home /mnt/nfs/home | __Client Setup__ |
| |
Note, that I expect you to create the mount point (/mnt/nfs/home in the example, but you may pick your own of course) in advance…! | Create the mount point |
| |
I hear you thinking… how do I find out the export list of my NFS server? This is easy: run | <code>mkdir /mnt/nfs_share</code> |
| |
showmount -e <NFS_servername> | Start the RPC daemon |
| |
to obtain a list. This is what the output will look like: | <code>chmod 755 /etc/rc.d/rc.rpc |
| /etc/rc.d/rc.rpc start</code> |
| |
# showmount -e bob | Mounting |
Export list for bob: | |
/home 192.168.0.0/24 | |
/var/www/htdocs 192.168.0.0/24 | |
/home/ftp/pub 192.168.0.0/24 | |
| |
Note that this specific NFS server also exports the webserver's DocumentRoot and the ftp server's 'pub' directory. What you don't see how those exports are configured (access restrictions and such, apart from the allowed IP address range). | On the CLIENT machine we'll cover 2 options here: manually mounting and auto-mount at boot |
| |
| __MANUALLY MOUNT__ |
| |
| <code>mount my.nfs.server:/nfs_share /mnt/nfs_share</code> |
| |
| AUTO-MOUNT AT BOOT |
| Add the mount command to /etc/fstab |
| |
| <code>nano /etc/fstab</code> |
| |
| <code>my.nfs.server:/nfs_share /mnt/nfs_share nfs rw,defaults 0 0</code> |
| |
| |
| NOTE ABOUT AUTO_MOUNTING |
| If you mount at boot and the server machine is unavailable, it will cause your client machine to take a long time to boot as the NFS client will make multiple attempts to connect and you will have to wait for it to time-out for each attampt. |