NFS
De SergiTurWiki
| Curs: | SambasobreLDAP, LinuxAdministracioAvancada |
| Fitxers: | AutenticacioiAutoritzacionsLinux.pdf |
| Repositori SVN: | https://anonymous@svn.projectes.lafarga.cat/svn/iceupc/SambaSobreLDAP |
| Usuari: | anonymous |
| Paraula de pas: | sense paraula de pas |
| Autors: | Sergi Tur Badenas, Lluís Pérez Vidal |
Contingut |
NFS
NFS (Network File System) és un protocol originàriament desenvolupat per SUN al 1984 que permet accedir al fitxer a través de la xarxa com si fossin fitxers d'un disc local. NFS com molts altres protocols està basat en Open Network Computing Remote Procedure Call system (ONC RPC).
NFS pot utilitzar els protocols UDP i TCP i el seu port per defecte és el 2049:
$ cat /etc/services | grep nfs nfs 2049/tcp # Network File System nfs 2049/udp # Network File System
Versions de NFS
La versió 1 de NFS va ser una versió experimental que va utilitzar Sun Microsystems però que mai es va fer pública. La versió 2 del protocol (RFC 1094, març 1989) operava completament amb UDP. La versió 3 (RFC 1813, Juny de 1995) afegeix múltiples funcionalitats i permet treballar amb TCP. La versió 4 (RFC 3010, desembre de 2004; revisada a RFC 3530, abril de 2003) inclou moltes millores de rendiment i seguretat.
La Versió 4 ha estat la primera versió desenvolupada fora de SUN per la Internet Engineering Task Force (IETF).
Els kernels de Linux tenen el següent suport per a versions de NFS:
- Kernel 1.2 o superior: NFS versió 2.
- Kernel 2.4 o superior: NFS versió 3.
- Kernel 2.6 o superior: NFS versió 2.
Consulteu la secció Com saber la versió de NFS, per tal de saber la versió de NFS que utilitza el vostre servidor.
Servidor NFS
Instal·lació
Per instal·lar nis hem d'executar:
$ sudo apt-get install nfs-kernel-server
Hi ha un altre paquet anomenat nfs-user-server que instal·la un servidor NFS en espai d'usuari. No utilitzarem aquest paquet en aquest article.
nfs-kernel-server depen de nfs-common i aquest alhora depèn de portmap i per tant tots dos s'instal·laran.
Podem consultar els fitxers que instal·la NIS amb la comanda:
$ dpkg -L nfs-kernel-server
Tenim les següents comandes/aplicacions/servidors:
$ dpkg -L nfs-kernel-server | grep bin /usr/sbin /usr/sbin/exportfs /usr/sbin/rpc.mountd /usr/sbin/rpc.nfsd
$ dpkg -L nfs-common | grep bin /sbin /sbin/rpc.lockd /sbin/rpc.statd /sbin/showmount /usr/sbin /usr/sbin/nfsstat /usr/sbin/rpc.gssd /usr/sbin/rpc.idmapd /usr/sbin/gss_clnt_send_err /usr/sbin/gss_destroy_creds /usr/sbin/rpcdebug
I els següents fitxers de configuració:
$ dpkg -L nfs-kernel-server | grep etc /etc /etc/init.d /etc/init.d/nfs-kernel-server
$ dpkg -L nfs-common | grep etc /etc /etc/init.d /etc/init.d/nfs-common
La resta de fitxers són manuals i documentació (carpeta /usr/share/doc).
Control del servei NFS. Execució, parada, status i reconfiguració de NFS
Seguint els estàndards de Debian GNU/Linux (basat en el sistema d'scripts d'inicialització SystemV) l'script de control del dimoni NFS és:
/etc/init.d/nfs-kernel-server
Les accions que podem fer amb aquest servei són start|stop|status|reload|force-reload|restart.
Els clients però també necessiten de certs serveis/dimonis per funcionar i són proporcionats per l'script SystemV:
/etc/init.d/nfs-common
Les accions que podem fer amb aquest servei són start|stop|status|reload|force-reload|restart. .
Cada cop que fem un canvi a la configuració de bind haurem de fer un restart o, millor encara, un reload del servei:
$ sudo /etc/init.d/nfs-kernel-server reload
La comanda exportfs ens permet que el dimoni NFS tingui el compte fet en la configuració de NFS (fitxer /etc/exports):
$ sudo exporfs -ra
Tots els dimonis utilitzen RPC (Remote Procedure Call) i podem utilitzar la comanda rpcinfo (consulteu la secció Com saber la versió de NFS.
Tal com podem veure executant:
$ sudo updatedb $ $ locate nfs | grep rc /etc/rc0.d/K79nfs-common /etc/rc0.d/K80nfs-kernel-server /etc/rc0.d/S31umountnfs.sh /etc/rc1.d/K79nfs-common /etc/rc1.d/K80nfs-kernel-server /etc/rc2.d/S20nfs-kernel-server /etc/rc2.d/S21nfs-common /etc/rc3.d/S20nfs-kernel-server /etc/rc3.d/S21nfs-common /etc/rc4.d/S20nfs-kernel-server /etc/rc4.d/S21nfs-common /etc/rc5.d/S20nfs-kernel-server /etc/rc5.d/S21nfs-common /etc/rc6.d/K79nfs-common /etc/rc6.d/K80nfs-kernel-server /etc/rc6.d/S31umountnfs.sh /etc/rcS.d/S01mountkernfs.sh /etc/rcS.d/S45waitnfs.sh
El servei NFS està disponible a partir del nivell 1.
El fitxer /etc/init.d/umountnfs.sh s'executa al reiniciar (nivell 6) o apagar el sistema per assegurar que els recursos NFS es desmunten de forma segura a l'apagar la màquina. També cal destacar que en single user mode (rcS.d) es té accés a NFS gràcies a les comandes mountkernfs.sh i waitnfs.sh.
NOTA: Cal tenir en compte que els scripts anteriors no són dels paquets nfs-kernel-server ni nfs-common sinó que estan proporcionats pel paquet initscripts (instal·lat en totes les màquines Debian GNU/Linux).
Podeu trobar més informació a l'article Configuració de serveis en Linux.
Configuració de NFS
La configuració de NFS és molt similar (però segurament més simple) que altres protocols de compartició de recursos de xarxa com Samba o SMB/CFIS. Al costat del servidor es defineixen sistemes de fitxers o directoris que es volen compartir a través de la xarxa. Al costat del client, les carpetes es poden muntar com si fossin particions de disc dur o dispositius extraibles situats a la màquina client.
El fitxer de configuració de NFS és /etc/exports i és llegit per el servei/dimoni NFS cada cop que s'executa. Per cada recurs que volem compartir afegim al fitxer /etc/exports una línia amb la següent estructura:
directori (o sistema de fitxers) client1 (opcio1, opcio2,.........,opcion) client2 (opcio1, opcio2,.........,opcion)
Podem utilitzar les següents opcions:
- secure: Aquesta opció està activada per defecte. Permet que NFS utilitzi ports TCP/IP per sota de 1024. L'opció insecure la desactiva.
- rw: Aquesta opció permet accés d'escriptura i lectura als clients. L'opció per defecte és només lectura.
- async: Aquesta opció pot millorar el rendiment de NFS, però pot provocar pèrdues si el servidor NFS no s'atura de forma neta. L'opció per defecte és sync.
- no_wdelay: S'activa el retard d'escriptura. Aquesta opció és ignorada en mode async.
- nohide: Si un directori és muntat sobre un altre, l'antic directori s'oculta o es veu aparentment buit. Per desactivar aquest comportament per defecte es pot activar l'opció hide.
- no_subtree_check: Aquesta opció desactiva el subtree checking. No és recomanable ja que s'executen unes comprovacions de seguretat que és important no saltar-se.
- no_auth_nlm: També anomenada insecure_locks, l'indica al dimoni NFS que no autentiqui les peticions de bloqueig. Si la seguretat és un tema important no s'ha d'activar aquesta opció.
- mp (mountpoint=path): Amb aquesta opció s'especifica de forma explícita que el directori a compartir estigui muntat.
- fsid=num: Per utilitzar NFS_Failover.
NFS permet assignar uns permisos concrets (un usuari i grups del servidor) per a un usuari treballant en un volum NFS compartit. L'usuari té els permisos de l'usuari i del grup assignats. Es pot crear un usuari genèric per a NFS i facilitar en gran manera l'administració de NFS.
El comportament per defecte és el que s'anomena squashed. L'usuari que accedeix al sistema (si no s'especifica explícitament un altre comportament) ho fa com un usuari anònim el qual per defecte només té permisos de lectura.
Aquest comportament per defecte es pot sobreescriure especificant un usuari (UID) i un grup (GID) al muntar el volum.
Les opcions relacionades amb assignació d'usuaris són:
- root_squash: Aquesta opció no permet l'accés de root als volums NFS.
- no_root_squash: El contrari que l'anterior.
- all_squash: Aquesta opció serveix per a directoris NFS públics. Aquest opció fa que tots els usuaris siguin squashed (tots els UIDS i GIDS usin la compte anònima). L'opció activada per defecte és no_all_squash.
- anonuid and anongid: Aquesta opció ens permet canviar l'UID i el GID dels usuaris de NFS.
Fitxer /etc/exports
Aquest és el fitxer de NFS on es configuren els recursos compartits.
Exemples
/opt/files 192.168.0.*
Exporta la carpeta /opt/files a tota la xarxa 192.168.0.0/24 amb les opcions per defecte.
/opt/files 192.168.0.120
Exporta la carpeta /opt/files per al client 192.168.0.120 amb les opcions per defecte.
/opt/files 192.168.0.125(rw, all_squash, anonuid=210, anongid=100)
Exporta la carpeta /opt/files per al client 192.168.0.125 amb les opcions per escriptura i lectura i amb l'usuari (UID) i grup (GID) especificat.
/opt/files *(ro, insecure, all_squash)
Aquesta última opció especifica un servidor NFS anònim.
SkoleLinux:
$ cat /etc/exports # /etc/exports: the access control list for filesystems which may be exported # to NFS clients. See exports(5). /opt/ltsp/i386 192.168.0.0/255.255.255.0(ro,no_root_squash) /var/opt/ltsp/swapfiles 192.168.0.0/255.255.255.0(rw,no_root_squash) /skole/tjener/home0 @ltsp-server-hosts(rw) @workstation-hosts(rw) @server-hosts(rw)
Recursos:
Fitxers /etc/hosts.allow i /etc/hosts.deny
TODO
These two files specify which computers on the network can use services on your machine. Each line of the file contains a single entry listing a service and a set of machines. When the server gets a request from a machine, it does the following:
1. It first checks hosts.allow to see if the machine matches a rule listed here. If it does, then the machine is allowed access. 2. If the machine does not match an entry in hosts.allow the server then checks hosts.deny to see if the client matches a rule listed there. If it does then the machine is denied access. 3. If the client matches no listings in either file, then it is allowed access.
In addition to controlling access to services handled by inetd (such as telnet and FTP), this file can also control access to NFS by restricting connections to the daemons that provide NFS services. Restrictions are done on a per-service basis.
The first daemon to restrict access to is the portmapper. This daemon essentially just tells requesting clients how to find all the NFS services on the system. Restricting access to the portmapper is the best defense against someone breaking into your system through NFS because completely unauthorized clients won't know where to find the NFS daemons. However, there are two things to watch out for. First, restricting portmapper isn't enough if the intruder already knows for some reason how to find those daemons. And second, if you are running NIS, restricting portmapper will also restrict requests to NIS. That should usually be harmless since you usually want to restrict NFS and NIS in a similar way, but just be cautioned. (Running NIS is generally a good idea if you are running NFS, because the client machines need a way of knowing who owns what files on the exported volumes. Of course there are other ways of doing this such as syncing password files. See the NIS HOWTO for information on setting up NIS.)
In general it is a good idea with NFS (as with most internet services) to explicitly deny access to IP addresses that you don't need to allow access to.
The first step in doing this is to add the followng entry to /etc/hosts.deny:
portmap:ALL
Starting with nfs-utils 0.2.0, you can be a bit more careful by controlling access to individual daemons. It's a good precaution since an intruder will often be able to weasel around the portmapper. If you have a newer version of nfs-utils, add entries for each of the NFS daemons (see the next section to find out what these daemons are; for now just put entries for them in hosts.deny):
lockd:ALL mountd:ALL rquotad:ALL statd:ALL
Even if you have an older version of nfs-utils, adding these entries is at worst harmless (since they will just be ignored) and at best will save you some trouble when you upgrade. Some sys admins choose to put the entry ALL:ALL in the file /etc/hosts.deny, which causes any service that looks at these files to deny access to all hosts unless it is explicitly allowed. While this is more secure behavior, it may also get you in trouble when you are installing new services, you forget you put it there, and you can't figure out for the life of you why they won't work.
Next, we need to add an entry to hosts.allow to give any hosts access that we want to have access. (If we just leave the above lines in hosts.deny then nobody will have access to NFS.) Entries in hosts.allow follow the format:
service: host [or network/netmask] , host [or network/netmask]
Here, host is IP address of a potential client; it may be possible in some versions to use the DNS name of the host, but it is strongly discouraged.
Suppose we have the setup above and we just want to allow access to slave1.foo.com and slave2.foo.com, and suppose that the IP addresses of these machines are 192.168.0.1 and 192.168.0.2, respectively. We could add the following entry to /etc/hosts.allow:
portmap: 192.168.0.1 , 192.168.0.2
For recent nfs-utils versions, we would also add the following (again, these entries are harmless even if they are not supported):
lockd: 192.168.0.1 , 192.168.0.2 rquotad: 192.168.0.1 , 192.168.0.2 mountd: 192.168.0.1 , 192.168.0.2 statd: 192.168.0.1 , 192.168.0.2
If you intend to run NFS on a large number of machines in a local network, /etc/hosts.allow also allows for network/netmask style entries in the same manner as /etc/exports above.
Recursos:
Comanda exportfs
Quan fem modificacions al fitxer /etc/exports hem d'executar la comanda:
$ sudo exportfs -ra
per tal que els canvis tinguin efecte.
Recursos:
Fitxers de log
Per veure el log de NFS podem executar:
$ sudo tail -f --lines=500 /var/log/messages Feb 24 13:59:48 tjener rpc.mountd: refused mount request from dhcp001.intern for /skole/tjener/home0 (/): no export entry Feb 24 14:00:12 tjener last message repeated 2 times Feb 24 14:02:13 tjener rpc.mountd: export request from 10.0.2.111 Feb 24 14:04:42 tjener rpc.mountd: refused mount request from dhcp001.intern for /skole/tjener/home0 (/): no export entry Feb 24 14:16:01 tjener rpc.mountd: refused mount request from dhcp001.intern for /skole/tjener/home0 (/): no export entry Feb 24 14:30:48 tjener rpc.mountd: Fake hostname etic2.intern for 10.0.2.102 - forward lookup doesn't exist Feb 24 14:30:48 tjener rpc.mountd: authenticated unmount request from 10.0.2.102:600 for /skole/tjener/home0 (/skole/tjener/home0)
NFS+NIS
NFS amb combinació amb NIS o altres protocols com LDAP per a gestionar usuaris de domini, s'utilitza per compartir la carpeta home dels usuaris. En aquest apartat veurem un exemple de com compartir la carpeta /home amb NFS.
Configuració del servidor
Modifiquem l'arxiu /etc/exports i afegim:
/home 192.168.1.0/255.255.255.0(rw)
Ara cal recarregar el servidor NFS
/etc/init.d/nfs-kernel-server reload
Configuració del client
Per tal de muntar el sistema de fitxers NFS utilitzem mount:
$ mount -t nfs ip_servidor_nfs:/home /home
Si volem que la carpeta es munti a l'inici de sessió podem afegir una entrada permanent al fitxer /etc/fstab:
192.168.1.1:/home /home nfs rsize=8192,wsize=8192,timeo=14,soft,user,suid,exec
Opcions:
- rsize=8192 i wsize=8192: S'utilitzen per millorar el rendiment de lectura i escriptura.
- exec: Amb exec permetem l'execució de programes a la carpeta muntada.
- L'opció users permet als usuaris poder utilitzar mount per muntar/desmuntar aquest recurs.
Exemple de compartició de homes
Al servidor on tindrem físicament les homes haurem d'instal·lar el servidors NFS:
$ sudo apt-get install nfs-kernel-server
I configurarem el fitxer /etc/exports de la següent manera:
$ sudo joe /etc/exports ... #HOMES /home 192.168.1.0/255.255.255.0(rw)
Com veieu indiquem quines màquines podem muntar les HOMES (xarxa de classe C 192.168.1.0/24).
Torneu a iniciar el servidor de NFS:
$ sudo /etc/init.d/nfs-kernel-server restart
Des de el client podeu comprovar que la home es comparteix correctament per part del servidor amb la comanda:
$ sudo showmount -e 192.168.1.3 Export list for 192.168.1.3: /home 192.168.1.*
Aquesta comanda la proporciona el client de NFS. Si no el teniu l'instal·leu amb:
$ sudo apt-get install nfs-client
Ara tenim dues formes de muntar les HOMES als clients.
Muntar les homes amb /etc/fstab
Al/s client/s instal·leu:
$ sudo apt-get install nfs-client
Editeu i afegiu les línies:
#HOMES NFS 192.168.1.3:/home /home nfs rw 0 0
al fitxer /etc/fstab.
192.168.1.3 és la IP del vostre servidor NFS.
Ara proveu de muntar la HOME:
$ sudo mount -a
Muntar les homes amb automount
SkoleLinux
Com podem veure s'utilitzen grups de xarxa (netgroups) però no són de NIS sinó que són de LDAP. S'administren via el mòdul netgroups del webmin
El cas d'SkoleLinux s'utilitza LDAP com podem veure al fitxer /etc/nsswitch.conf:
cat /etc/nsswitch.conf # /etc/nsswitch.conf # # Example configuration of GNU Name Service Switch functionality. # If you have the `glibc-doc' and `info' packages installed, try: # `info libc "Name Service Switch"' for information about this file. passwd: files ldap group: files ldap shadow: files ldap netgroup: files ldap automount: files ldap # passwd: compat # group: compat # shadow: compat hosts: files dns networks: files protocols: db files services: db files ethers: db files rpc: db files # netgroup: nis
Client NFS
Recursos:
- man mount (http://www.die.net/doc/linux/man/man8/mount.8.html)
- man fstab (http://www.die.net/doc/linux/man/man5/fstab.5.html)
- man nfs (http://www.die.net/doc/linux/man/man5/nfs.5.html)
Instal·lació del client
El client necessita els paquets:
- mount
- portmap
- nfs-common: instal·la el serveis rpc.statd i rpc.lockd
$ sudo apt-get install nfs-common
Podeu consultar la secció Instal·lació del servidor NFS per conèixer els detalls del paquet nfs-common (que també s'instal·la a l'instal·lar el servidor). Podem comprovar que tot està instal·lat i funcionant amb:
$ rpcinfo -p localhost program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 791 status 100024 1 tcp 794 status
Accedir a fitxers remots amb NFS
Normalment l'únic que necessitem per accedir a recursos NFS remots és la comanda mount. Tot i això, cal assegurar-se que tenim executant-se els serveis portmap (paquet portmap) i rpc.statd (paquet nfs-common). Podem comprovar amb les comandes:
$ rpcinfo -p localhost program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 927 status 100024 1 tcp 930 status
$ ps
El dimoni rpc.statd apareix com estatus. Hi ha un tercer servei (rpc.lockd) que s'activa ?
La sintaxi de mount és:
mount server:directory local mount point
Per muntar un recurs remot NFS cal ser root (a no ser que estigui el punt de muntatge al fitxer /etc/fstab). Per muntar la carepta /opt/files del servidor NFS 192.168.0.100 a la carpeta mnt executem:
$sudo mount 192.168.0.100:/opt/files /mnt
És possible que ens demani que especifiquem el tipus de sistema de fitxers a utilitzar:
mount -t nfs 192.168.0.100:/opt/files /mnt
Per establir el punt de muntatge de forma permanent hem d'editar el fitxer /etc/fstab i afegir la següent entrada:
192.168.0.100:/opt/files /mnt nfs rw 0 0
Comanda showmount
Els clients poden consultar la llista de carpetes exportades per un servidor NFS amb la comanda:
$ showmount -e ip_servidor_nfs
Es pot consultar les unitats ja muntades amb la comanda:
$ showmount -a
Recursos:
Eines gràfiques
Gnome system-tools.shares-admin
Rendiment
TODO
A. Review the performance section of the NFS Howto doc and then look at several things:
- How fast is the disk IO speed on your server(s)? That will have a big impact on overall NFS performance for both Version 2 and Version 3.
- Does your application open its files with the O_SYNC option? That will force NFS Version 3 to behave exactly like (synchronous) NFS Version 2.
- UDP requires IP fragment reassembly. If you see fragmentation errors indicated in the output of netstat -s you may want to increase the size of your socket buffers.
- Have you started enough NFS daemons? Review the contents of /proc/net/rpc/nfsd, especially the line that begins with "th". The first number on that line is the total number of NFS server threads that are started and waiting for NFS requests. The second number indicates whether at any time all of the threads were running at once. The remaining numbers are a thread count time histogram. See the NFS How-to for details on tuning your server based on the data in this histogram.
- Do your NICs and Switches/Hubs/Routers autonegotiate down to 10baseT or half duplex? Half duplex will give you many more network collisions, which are the worst thing possible for NFS performance in UDP.
- Are you running ext3 or ReiserFS? You might look at placing the journal on a separate disk, or on NVRAM. As of January 2002, ext3 allows this, and Reiser has a patch available.
Recursos:
TroubleShooting
Recursos:
Com saber la versió de NFS
Amb la comanda rpc-info podem sasber quins protocols i quines versions suporta un servidor NFS concret:
$ rpcinfo -p localhost program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100004 2 udp 695 ypserv 100004 1 udp 695 ypserv 100004 2 tcp 698 ypserv 100004 1 tcp 698 ypserv 100024 1 udp 927 status 100024 1 tcp 930 status 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100003 2 tcp 2049 nfs 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100021 1 udp 1029 nlockmgr 100021 3 udp 1029 nlockmgr 100021 4 udp 1029 nlockmgr 100021 1 tcp 4283 nlockmgr 100021 3 tcp 4283 nlockmgr 100021 4 tcp 4283 nlockmgr 100005 1 udp 678 mountd 100005 1 tcp 681 mountd 100005 2 udp 678 mountd 100005 2 tcp 681 mountd 100005 3 udp 678 mountd 100005 3 tcp 681 mountd
Com depurar errors de NFS
- Primer cal mirar el fitxer /etc/exports
- La comanda rpcinfo pot especificar-nos informació valiosa
rpcinfo -p ip-client (localhost) rpcinfo -p ip_servidor
Recursos:
Stale NFS file handling
En català:
bash: cd: egalofre218: L'indicador del fitxer NFS és obsolet
Això passa si en mig d'una sessió de treball remota amb NFS s'apaga el servidor i queda un fitxer a mig utilitzar. Les solucions:
- Unmount and remount the file system, may need to use the -O (overlay option) of mount.
From the man pages: -O Overlay mount. Allow the file system to be mounted over an existing mount point, making the underlying file system inaccessible. If a mount is attempted on a pre-existing mount point without setting this flag, the mount will fail,
producing the error "device busy".
- Kill or restart the process trying to use the nonexistent files.
- Create another mount point and access the files from the new mount point.
- Run: /etc/init.d/nfs.client stop; /etc/init.d/nfs.client start
- Reboot the client having problems.
Recursos:
