Linux:networking:nfs
From Linux How-To Repository
Contents |
Setting Up NFS Among Linux Boxes
This worked without a hitch for me on Ubuntu, whereas Samba was quickly becoming a hassle. In my case, I wanted to share files between an iBook with Ubuntu and my main Ubuntu machine.
I started by doing this on my iBook, making it the "server" in the setup:
sudo apt-get install nfs-kernel-server nfs-common portmap
When configuring portmap do not bind loopback. If you do you can either edit /etc/default/portmap using the following:
sudo gedit /etc/default/portmap
Or, use the following command
sudo dpkg-reconfigure portmap
But you probably won't have to do anything. Now restart Portmap using the following command
sudo /etc/init.d/portmap restart
NFS Server Configuration
NFS exports from a server are controlled by the file /etc/exports. Each line begins with the absolute path of a directory to be exported, followed by a space-separated list of allowed clients.
Because I nominated my iBook as the "server" in this share arrangement, I edited the exports file on the iBook system using the following command:
sudo gedit /etc/exports
In this file I entered my home directory on my ibook as the directory I wanted to share (the following is fictitious, but you get the idea) and then next to it I put my main machine's IP:
/home/ibookuser 192.168.0.100(rw,async)
Here are some other quick examples of what you could add to your /etc/exports
For Full Read Write Permissions allowing any computer from 192.168.1.1 through 192.168.1.255
/files 192.168.1.1/24(rw,no_root_squash,async)
Or, for Read Only from a single machine
/files 192.168.1.2 (ro,async)
Save this file and exit.
A client can be specified either by name or IP address. Wildcards (*) are allowed in names, as are netmasks (e.g. /24) following IP addresses, but should usually be avoided for security reasons.
A client specification may be followed by a set of options, in parenthesis. It is important not to leave any space between the last client specification character and the opening parenthesis, since spaces are intrepreted as client seperators.
Now restart the NFS server using the following command
sudo /etc/init.d/nfs-kernel-server restart
If you make changes to /etc/exports on a running NFS server, you can make these changes effective by issuing the command
sudo exportfs -a
I can't remember now why I made my main machine the client and my iBook the server in this share setup. But it doesn't seem to matter either way.
NFS Client Support
Now I set up my main machine as the "client" that will be accessing the iBook.
sudo apt-get install portmap nfs-common
This will install all the required packages for nfs client
To mount the share file on my main machine, I would mount ibook.mydomain.com:/home/me to /media/ibook. In this example ibook.mydomain.com is the name of my server containing the nfs share, and "/media/ibook" is the name of the share on my main machine, the client
The mount point /media/ibook must first exist on the client machine.
Create /media/ibook directory using the following command
sudo mkdir /media/ibook
You need to mount the share using the following command
sudo mount ibook.mydomain.com:/home/ibookuser /media/ibook
Or, of course, something like this:
sudo mount 192.168.0.99:/home/ibookuser /media/ibook
Now you may need to restart services using the following command
sudo /etc/init.d/portmap restart
sudo /etc/init.d/nfs-common restart
If you want to mount at boot time using fstab file
sudo edit /etc/fstab
In this example my /etc/fstab was like this
ibook.mydomain.com:/home/me /media/ibook nfs rsize=8192,wsize=8192,timeo=14,intr
Change “ibook.mydomain.com:/home/ibookuser” and “/media/ibook” to match your server name, share name, and the name of the mount point you created.
Testing Your Configuration
Use the following command in terminal to test;
mount /media/ibook
The mount point /media/ibook will be mounted from the server.
Tips & Troubleshooting
In your client's host file, add any entry to simplify the server's address to make it more explicit, if you need to. I have something like this:
192.168.0.99 ibook
And so I can mount like this:
sudo mount ibook:/home/ibookuser /media/ibook
Or in the fstab, I could have it like this:
ibook:/home/ibookuser /media/ibook nfs rsize=8192,wsize=8192,timeo=14,intr
Now, for many people Ubuntu will not mount NFS automatically at boot. A number of ways of fixing it exist, such as modifying the start sequence for NFS service so it come after all of the other processes it needs. However, the easies way for me was to simply put the following line in the /etc/rc.local file:
mount /media/ibook
You still need the line in fstab, though.
