Published on

NFS The Network File System

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. NFS Network File System Revised November 16, 2003 Professor Tom Mavroidis
  2. 2. What is NFS? <ul><li>The Network File System (NFS) is a distributed file system that allows users to access files and directories located on remote computers and treat those files and directories as if they were local. For example, users can use operating system commands to create, remove, read, write, and set file attributes for remote files and directories. </li></ul>
  3. 3. The NFS HOWTO’s <ul><li>You can find the NFS how to at </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  4. 4. RFC’s <ul><li>You can follow the evolution of NFS at: </li></ul><ul><li>NFS protocol version 2 [ RFC1094 ] </li></ul><ul><li>NFS protocol version 3 [ RFC1813 ] </li></ul><ul><li>NFS version 4 Protocol [ RFC3010 ] </li></ul>
  5. 5. What’s new in Version 4 <ul><li>NFS (Network File System) version 4 is a distributed file system protocol which owes heritage to NFS protocol versions 2 [ RFC1094 ] and 3 [ RFC1813 ]. Unlike earlier versions, the NFS version 4 protocol supports traditional file access while integrating support for file locking and the mount protocol. In addition, support for strong security (and its negotiation), compound operations, client caching, and internationalization have been added. Of course, attention has been applied to making NFS version 4 operate well in an Internet environment. </li></ul>
  6. 6. Introduction <ul><li>Linux has multiple types of file systems available to it </li></ul><ul><li>NFS is just one of these types </li></ul><ul><li>NFS was developed by Sun Microsystems in 1994 </li></ul><ul><li>NFS allows fill or partial file systems from other hosts to be mounted to the local file system </li></ul>
  7. 7. Three Layers <ul><li>NFS has three layers </li></ul><ul><li>The RPC layer which passes the data between hosts [RFC3010] </li></ul><ul><li>the XDR layer which provides for machine independence of the data [RFC1832] </li></ul><ul><li>and the top layer which consists of the mount protocol and the NFS protocol </li></ul>
  8. 8. Disadvantage to NFS <ul><li>Limited security </li></ul><ul><li>Clients and servers trust each other unconditionally </li></ul><ul><li>Host names can be spoofed (a machine claiming to be another) </li></ul>
  9. 9. Starting up NFS <ul><li>There are three key things you need to start on Linux to make NFS work. </li></ul><ul><ul><ul><ul><li>/usr/sbin/rpc.portmap </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/usr/sbin/rpc.mountd </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/usr/sbin/rpc.nfsd </li></ul></ul></ul></ul><ul><li>These things should start up automatically at boot time. The file that makes this happen is &quot;/etc/rc.d/rc.inet2&quot; </li></ul>
  10. 10. You can verify that these are running by issuing this command (as root) <ul><li>rpcinfo -p localhost </li></ul><ul><li>program vers proto port </li></ul><ul><li>100000 2 tcp 111 portmapper </li></ul><ul><li>100000 2 udp 111 portmapper </li></ul><ul><li>100005 1 udp 679 mountd </li></ul><ul><li>100005 1 tcp 681 mountd </li></ul><ul><li>100003 2 udp 2049 nfs </li></ul><ul><li>100003 2 tcp 2049 nfs </li></ul><ul><li>Note that there are two copies of each running, one for tcp and one for udp protocols. </li></ul>
  11. 11. Ways to reduce security risks <ul><li>Use the secure,nosuid and root_squash options when exporting a resource </li></ul><ul><li>Disallow SUID execution </li></ul><ul><li>Map root access to the anonymous UID </li></ul><ul><li>Mount few file systems as RW </li></ul><ul><li>Hosts in the exports file should be FQDN (Fully Qualified Domain Names) </li></ul>
  12. 12. Similarities <ul><li>NFS is similar to SMB (Server Message Blocks) </li></ul><ul><li>NFS uses a different protocol than SMB and only shares files </li></ul><ul><li>NFS was created for UNIX </li></ul><ul><li>NFS uses a client /server relationship to handle file sharing </li></ul><ul><li>Any machine can be a client and a server at the same time </li></ul>
  13. 13. Notes <ul><li>NFS makes files on a remote host appear as part of the local host’s file system </li></ul><ul><li>NFS allows you to centralize administration of disks instead of duplicating directories on every system </li></ul><ul><li>Only the servers need to be backed up if clients do not store data locally </li></ul>
  14. 14. Principles <ul><li>On Linux we split the disk into Partitions (see next slide) </li></ul><ul><li>Each partition has a structure imposed on it </li></ul><ul><li>The Linux tree appears as one set of files and directories by mounting onto it during bootup </li></ul>
  15. 15. Linux Tree
  16. 16. During bootup <ul><li>On initial boot the Linux file system consists of only a mount point at the top of the tree </li></ul><ul><li>Linux startup scripts then issue mount commands to build the file tree </li></ul><ul><li>When mounting an NFS file system you specify the name of the host that the files are on instead of the local disk </li></ul>
  17. 17. NFS Client/Server <ul><li>Must mark the files and directories to be shared (known as resources) </li></ul><ul><li>The system that imports or mounts resources from a NFS server is known as a NFS client </li></ul><ul><li>An NFS client may only import a resource if it has been exported by an NFS server </li></ul><ul><li>Re-exporting resources are not allowed </li></ul>
  18. 18. NFS Server exports <ul><li>Resources that need to be shared must be exported by the server </li></ul><ul><li>Any file system or subset may be exported </li></ul><ul><li>A directory or sub-directory may be exported (if a directory is exported then a parent directory can only be exported if it exists on a different device </li></ul><ul><li>Only local resources may be exported </li></ul>
  19. 19. NFS Client <ul><li>Client must identify the server host it wishes to import resources from (those resources must have been exported by the server) </li></ul><ul><li>Imported resources appear as local files and directories </li></ul><ul><li>No restrictions on the number of resources a client may import </li></ul>
  20. 20. Client options <ul><li>Client may import a resource as read only even though it has been exported as read/write. </li></ul><ul><li>There are two types of import, hard and soft </li></ul><ul><li>Hard is recommended for read/write, it will continue to retry operations until the server responds. </li></ul><ul><li>Soft causes the client to give up after a certain time </li></ul>
  21. 21. Protocols <ul><li>Three separate layers </li></ul><ul><ul><li>RPC (Remote Procedure Call) </li></ul></ul><ul><ul><li>XDR (External Data Representation) </li></ul></ul><ul><ul><li>Top Layer </li></ul></ul><ul><ul><ul><li>two protocols </li></ul></ul></ul><ul><ul><ul><ul><li>Mount protocol(negotiates the import between the client and the server) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>NFS protocol(platform independent, deals with passing blocks of data back and forth between client and server) </li></ul></ul></ul></ul>
  22. 22. UDP <ul><li>NFS runs over UDP (sounds strange) </li></ul><ul><ul><li>Stateless (client does not detect absence of server) </li></ul></ul><ul><ul><li>Idenpotenet (request can be sent more than once to the sever without negative effects) (client can keep retrying its request until the server replies) </li></ul></ul>
  23. 23. Application Layer Protocol <ul><li>NFS is an application layer protocol </li></ul><ul><li>Mount protocol is system dependent and supports the import of remote procedure calls </li></ul><ul><li>NFS protocol is operating system independent </li></ul>
  24. 24. RPCs Remote Procedure Calls <ul><li>RPCs allow procedure calls similar to local procedure calls to be performed on a remote machine </li></ul><ul><li>NFS uses Suns Open Network Computing (ONC) Remote Procedure Call as defined in RFC 1057 </li></ul><ul><li>RPC servers use a port mapper to facilitate communications between client and server </li></ul>
  25. 25. RPCs <ul><li>Remote procedures are identified by a program number and procedure number </li></ul><ul><li>Program number specifies a group or related procedures </li></ul><ul><li>Procedure number identifies a procedure within that group </li></ul><ul><li>Each set of procedures has a version number </li></ul>
  26. 26. Client calls <ul><li>A client calls a remote procedure by specifying its program number, version number, and procedure number </li></ul>
  27. 27. Port Numbers <ul><li>Are allocated dynamically by a server </li></ul><ul><li>Client discovers the servers port number by sending a request to the port mapper on the server. The port mapper replies with the port number </li></ul><ul><li>The server requests a port number on startup from its host machine. The server returns the port number to the port mapper </li></ul>
  28. 28. Portmapper <ul><li>In UNIX the port mapper is implemented with the rpcbind or portmap server </li></ul><ul><li>Some services, mainly SUN's NIS and NFS services, don't use a static port like most services, but instead register themselves via RPC. To connect to an NFS server you first ask RPC which port to find it on. It may even be the case that the service isn't available until you request it via RPC. If you want to list the registered RPC services run rpcinfo: </li></ul>
  29. 29. rpcinfo -p localhost <ul><li>[tmavroid@bigbox tmavroid]$ rpcinfo -p localhost </li></ul><ul><li>program vers proto port </li></ul><ul><li>100000 2 tcp 111 portmapper </li></ul><ul><li>100000 2 udp 111 portmapper </li></ul><ul><li>100004 2 udp 644 ypserv </li></ul><ul><li>100004 1 udp 644 ypserv </li></ul><ul><li>100004 2 tcp 647 ypserv </li></ul><ul><li>100004 1 tcp 647 ypserv </li></ul><ul><li>100007 2 udp 663 ypbind </li></ul><ul><li>100007 1 udp 663 ypbind </li></ul><ul><li>100007 2 tcp 666 ypbind </li></ul><ul><li>100007 1 tcp 666 ypbind </li></ul><ul><li>100005 1 udp 635 mountd </li></ul><ul><li>100005 2 udp 635 mountd </li></ul><ul><li>100005 1 tcp 635 mountd </li></ul><ul><li>100005 2 tcp 635 mountd </li></ul><ul><li>100003 2 udp 2049 nfs </li></ul><ul><li>100003 2 tcp 2049 nfs </li></ul><ul><li>100009 1 udp 873 yppasswdd </li></ul>
  30. 30. nfsstat <ul><li>Gives statistics on client and server RPC calls and client and server NFS processes. </li></ul><ul><li>-u option makes a UDP call </li></ul><ul><li>-t option makes a TCP call </li></ul>
  31. 31. XDR (External data Representation) <ul><li>Deals with sending complex data types between machines of different architectures </li></ul><ul><li>Standardizes the formats of data sent across a network </li></ul><ul><li>Programs translate data to XDR’s canonical (A canonical format is the &quot;preferred&quot; format) and sends it across the network </li></ul>
  32. 32. Top Level Formats <ul><li>Two separate formats </li></ul><ul><ul><li>Mount protocol </li></ul></ul><ul><ul><ul><li>responsible for negotiating the mapping of the remote drive </li></ul></ul></ul><ul><ul><ul><li>operating system specific </li></ul></ul></ul><ul><ul><li>NFS Protocol </li></ul></ul><ul><ul><ul><li>operating system independent </li></ul></ul></ul><ul><ul><ul><li>Oldest common version of NFS is v3 (RFC 1813) </li></ul></ul></ul>
  33. 33. Limitations of NFS <ul><li>Linux symbolic links cannot be supported if the remote system does not support them </li></ul><ul><li>Deleting files while they are open </li></ul><ul><ul><li>Linux doesn’t free disk blocks until the file has been closed </li></ul></ul><ul><li>Access to device files (Linux special files and DOS LPT devices is not supported </li></ul><ul><li>File locking becomes difficult </li></ul>
  34. 34. EXPORTS <ul><li>Means making a local directory of file available for mounting over the network by NFS clients </li></ul><ul><li>NFS server maintain a table of exported resources </li></ul><ul><ul><li>$ exportsfs #shows current exports table </li></ul></ul><ul><ul><li>$ exprotsfs -a #exports all resources in exports file </li></ul></ul>
  35. 35. EXPORTS file and Options <ul><li>Exports file is a text file </li></ul><ul><li>Specifies those resource that may be exported </li></ul><ul><li>The file is /etc/exports </li></ul><ul><li>One line per resource containing the path to the directory to be exported followed by a space separated list of hosts that may access the resource followed by a parenthesized list of options </li></ul>
  36. 36. Export file options <ul><ul><li>Ro - resource is marked read only and may not be changed by this host </li></ul></ul><ul><ul><li>root-squash - Map request form UID or GID 0 (root) to the anonymous UID/GID </li></ul></ul><ul><ul><li>rw - read/write </li></ul></ul><ul><ul><li>sync - writes will be immediately written to disk </li></ul></ul><ul><li>/exports/diskless-root diskless*,no_root_squash) </li></ul>
  37. 37. Mounting <ul><li>Syntax is as follows </li></ul><ul><ul><li>$mount -t nfs -o optionlist hostname:/export /localpath </li></ul></ul><ul><li>Instead of a device name a hostname:/export pair is given </li></ul><ul><li>A hostname must refer to a system that can be contacted by the local system </li></ul><ul><li>The /export must refer to an exported resource on the remote system </li></ul>
  38. 38. Mount options <ul><li>Hard mount is recommended for file systems that are mounted read/write </li></ul><ul><li>intr option assures that processes accessing hard mounts may be interrupted it the NFS server crashes </li></ul><ul><li>If the file system is regularly mounted place an entry in the /etc/fstab file </li></ul>
  39. 39. Mount -a -t nfs <ul><li>In rc.local will make sure that all NFS resources in /etc/fstab are mounted </li></ul><ul><li>fstab entry </li></ul><ul><ul><li>hostname:/exportdir nfs rsize=8192,wsize=8192,timeo=14,intr 0 0 </li></ul></ul><ul><li>too large a value for rsize and wsize could cause problems with lost packets and retransmissions </li></ul>
  40. 40. Creating an NFS Export <ul><li>Edit the /etc/exports file on the server </li></ul><ul><ul><li>/home/yourname </li></ul></ul><ul><li>stop and restart the server </li></ul><ul><ul><li># etc/rc.d/init.d/nfs stop </li></ul></ul><ul><ul><li># etc/rc.s/init.d/nfs start </li></ul></ul><ul><li>edit the /etc/fstab on the client </li></ul><ul><ul><li> /mnt/testnfs nfs defaults 0 0 </li></ul></ul><ul><li>Create the mount point on the client </li></ul><ul><ul><li># mkdir /mnt.testnfs </li></ul></ul><ul><li>Mount the /mnt/testnfs directory on the client </li></ul><ul><ul><li># mount /mnt/testnfs </li></ul></ul>
  41. 41. Security <ul><li>NFS is a major security headache </li></ul><ul><li>NFS uses simple authentication bases on IP address and user ID </li></ul><ul><li>Use a secure authentication scheme </li></ul><ul><li>Turn on Root-squashing </li></ul><ul><li>Configure NFS to listen only on low numbered ports </li></ul>
  42. 42. So in summary <ul><li>Even with its simple security there are times you are forced to use NFS such as for </li></ul><ul><ul><li>Diskless workstations </li></ul></ul><ul><li>NFS gives you a rich alternative to remote file/directory sharing </li></ul><ul><li>Useful for remote loading of machines </li></ul>
  43. 43. Setting Up Suse Specific NFS <ul><li>Verify that the NFS package has been loaded </li></ul><ul><li>rpm -q knfsd </li></ul>
  44. 44. Verifying the NFS daemons <ul><li>If the portmap daemon is not running, you need to start it up first before you start up the NFS daemons. You can do this with the command: </li></ul><ul><li>rcportmap start </li></ul><ul><li>Once the portmap daemon is running, you can start up the NFS daemons with the command: </li></ul><ul><li>rcnfsserver start </li></ul>
  45. 45. You can verify that the rpc.knfsd, rpc.kmountd, and portmap daemons are running <ul><li>ps ax | grep nfs </li></ul><ul><li>323 ? SW 0:00 [nfsd] </li></ul><ul><li>324 ? SW 0:00 [nfsd] </li></ul><ul><li>325 ? SW 0:00 [nfsd] </li></ul><ul><li>326 ? SW 0:00 [nfsd] </li></ul><ul><li>327 ? SW 0:00 [nfsd] </li></ul><ul><li>328 ? SW 0:00 [nfsd] </li></ul><ul><li>329 ? SW 0:00 [nfsd] </li></ul><ul><li>330 ? SW 0:00 [nfsd] </li></ul><ul><li>662 ttyp1 S 0:00 </li></ul><ul><li>ps ax | grep mount </li></ul><ul><li>313 ? SW 0:00 [rpc.kmountd] </li></ul><ul><li>673 ttyp1 S 0:00 </li></ul><ul><li>rcportmap status OK </li></ul>
  46. 46. Starting the portmapper <ul><li>If the portmap daemon is not running, you need to start it up first before you start up the NFS daemons. You can do this with the command: </li></ul><ul><li>rcportmap start </li></ul>
  47. 47. Start up the NFS daemons <ul><li>Once the portmap daemon is running, you can start up the NFS daemons with the command </li></ul><ul><li>rcnfsserver start </li></ul>
  48. 48. Note: <ul><li>If the /etc/exports file does not exist or is empty, the NFS daemons will not start. </li></ul><ul><li>You can give NFS access to a file system by setting it up in the /etc/exports file. The file is set up on the exporting server. You can create a sample file entry by opening the /etc/exports file. Then you can add an entry like: </li></ul><ul><li>/usr/local/share </li></ul>
  49. 49. Starting and Stopping the Server <ul><li>To stop the NFS server, use the command: </li></ul><ul><li>rcnfsserver stop </li></ul><ul><li>You can restart the NFS process with the command: </li></ul><ul><li>rcnfsserver restart </li></ul>
  50. 50. Access options <ul><li>ro read only Only permits reading </li></ul><ul><li>rw read write Permits reading and writing. If both ro and rw are specified, rw takes priority. </li></ul><ul><li>root_squash client Anonymous user (nobody) access from client. </li></ul><ul><li>no_root_squash client Access request privileges per the privileges of the client root. Useful for diskless clients. </li></ul><ul><li>squash_uids and squash_gids Specify a list of UIDs or GIDs that should be subject to anonymous mapping. A valid list of IDs looks like this: squash_uids=0-15,20,25-50 </li></ul><ul><li>all_squash all access Processes all requests for access as anonymous user. </li></ul><ul><li>anonuid=uid root_squash or all_squash when options are set will assign a group ID to an anonymous user request. </li></ul><ul><li>anonuid=gid root_squash or all_squash when options are set will assign a group ID to an anonymous user request </li></ul>
  51. 51. sample  /etc/exports  file   <ul><li>A  sample  /etc/exports  file  is  shown  in  the  man  pages  for    exports   </li></ul><ul><li># sample /etc/exports file </li></ul><ul><li>/ master(rw) trusty(rw,no_root_squash) </li></ul><ul><li>/projects proj*.local.domain(rw) </li></ul><ul><li>/usr *.local.domain(ro) @trusted(rw) </li></ul><ul><li>/home/joe pc001(rw,all_squash,anonuid=150,anongid=100) </li></ul><ul><li>/pub (ro,insecure,all_squash) </li></ul><ul><li>/pub/private (noaccess) </li></ul>
  52. 52. # sample /etc/exports file <ul><li>This is just a comment. Any line or character string can be converted to a comment and disabled by entering a # symbol. Everything from that point to the end of the line is considered to be a comment </li></ul>
  53. 53. / master(rw) trusty(rw,no_root_squash) <ul><li>This says that the root directory (/) is exported to the servers: master  - whose rights are read-write </li></ul><ul><li>trusty  - whose rights are read-write and the access rights of the client root can be the same as the server’s root </li></ul>
  54. 54. /projects proj*.local.domain(rw) <ul><li>The directory /projects is read-write accessible to all servers whose names match the pattern proj*.local.domain . </li></ul><ul><li>This includes proj.local.domain, proj1.local.domain,projprojproj.local.domain  and so forth </li></ul>
  55. 55.   /usr *.local.domain(ro) @trusted(rw ) <ul><li>Any systems whose hostname ends in   .local.domain  is allowed read-only access. The   @trusted  netgroup is allowed read-write access </li></ul>
  56. 56. /home/joe pc001(rw,all_squash,anonuid=150,anongid=100) <ul><li>The directory /home/joe is accessible to pc001 for read-write access; all requests for access are processed as anonymous users. The anonymous UID number is set to 150 and the anonymous group ID is set to 100.This is useful when using a client that is running PCNFS or an equivalent NFS process on the PC. Since the PC IDs do not necessarily map to the UNIX IDs, this allows the proper file attributes to be set. </li></ul>
  57. 57. /pub (ro,insecure,all_squash) <ul><li>The directory /pub is accessible as read-only. The option in this entry also allows clients with NFS implementations that don't use a reserved port for NFS and process all requests as an anonymous user </li></ul>
  58. 58. /pub/private (noaccess) <ul><li>The directory /pub/private does not allow any NFS access. </li></ul>
  59. 59. Accessing data remotely with NFS <ul><li>To mount a remote file system on your local system, the mount point must exist. The mount process does not create the mount point automatically. The process of making the mount point is to use the Linux   mkdir   command. To make the /usr/local/share mount point, enter: </li></ul><ul><li>mkdir /usr/local/share </li></ul>
  60. 60. File attributes and ownership <ul><li>Typically you do not need to worry about file attributes and ownership when making an NFS mount point. The NFS access rights will usually supersede any rights established for the directory. </li></ul>
  61. 61. Once you have created the mount point, you can use the mount command as follows: <ul><li>mount -t nfs nfs_host:share_dir local_mount_dir </li></ul><ul><li>where: </li></ul><ul><li>-t nfs Says to do the mount as an NFS mount. This is now optional because if you explicitly specify the directory to be mounted as host:directory the   mount command knows that it is an NFS mount. </li></ul><ul><li>nfs_host Is the host that is exporting the file system to be shared. </li></ul><ul><li>share_dir Is the actual directory that is to be shared. </li></ul><ul><li>local_mount_dir Is the directory on the local host where the remote directory is going to be mounted. </li></ul><ul><li>As mentioned earlier, this mount point must exist . </li></ul>
  62. 62. Assignment <ul><li>Setup your machine as a NFS server and share out a directory. Have one of your classmates remotely mount the nfs share from their machine. </li></ul><ul><li>Once this is set up show me the results. </li></ul>