Social Icons

twitterfacebookgoogle pluslinkedinrss feedemail

Friday, April 17, 2009

NFS mount Ubuntu linux drive on Mac OS X Leopard

After scouring the web for how to do this, I wound up having to piece through at least a half-dozen blogs to figure out how to do this and to get it working. So, to keep you from having to do the same, I give you:

How to NFS mount an Ubuntu Linux drive in Mac OS X Leopard
-- UPDATED for 9.04 support --

One assumption I have made are that both computers are running on the same network, but they don't have to be. Your Mac needs to be able to "see" your Linux box. If they're on separate networks, you will most likely have to deal with portmaping through routers and firewalls which is still fairly straight forward, but beyond the scope of this document.

I reference my Linux box by name because I have its IP address in my Mac's host file. You don't need this however, and can simply use it's IP address instead.

I also assume you have some rudimentary knowledge of your Mac and Linux, specifically, being able to open terminal windows to get a shell and command line as well as having sudo privileges on your Linux box.
  1. Before we begin we'll need some information for later. From your shell or terminal window in Ubuntu, execute the id command. You should see something similar to:

    uid=1000(doej) gid=1000(doej) groups=4(adm),20(dialout),1000(doej)

    All we care about are the uid and gid values for your username. In the output above, we see the uid and gid values are 1000 for the user doej (John Doe).

  2. Now, execute the same command on your Mac from the Terminal program. You'll notice the output is a little different:

    uid=501(doej) gid=20(staff) groups=20(staff),98(_lpadmin)

    Note that the uid is 501 and the gid is 20. In my examples, my username is the same between my Mac and Linux, but they don't have to be.

  3. Save these numbers or shell output for later.

  4. In Ubunutu make sure you have the following packages installed: nfs-user-server and nfs-common. Do this by executing "sudo apt-get install nfs-user-server nfs-common" from
    your shell or terminal window.

    NB: this will fail in Ubuntu 9.04 because the brilliant folks at Canonical decided to remove it and I don't know why. However, fear not you can still get the package here. Install this file with the command "sudo dpkg -i [filename]" - this will error out if you somehow already have the nfs-kernel-server package installed.

  5. Now we need to make a map of our user and group IDs we obtained in #1 and #2 above. This will ensure that when we NFS mount our drive on our Mac, the permissions and file ownership will all be set correctly. A common directory for this file is in /etc/nfs. If this directory doesn't exist for you, go ahead and create it.

    Create a file in this directory called foo.map - it can be called anything, but the naming convention is to use the name of the accessing server, in this case the hostname of your Mac. Mine, creatively enough, has a hostname of mac so my file is called mac.map.

    Here's what my file looks like:

    # mapping for client: mac
    # remote local
    uid 501 1000
    gid 20 1000

    The first two lines are just comments. The second two map the uid and gid from steps 1 and 2 above. Here, remote means your Mac, and local means your Linux box.

  6. NFS gets its export information (the directories that are allowed to be NFS mounted) from the file /etc/exports. If this file does not exist, just create it. Here's what my file looks like:

    /home/doej mac(rw,insecure,map_static=/etc/nfs/mac.map)

    The first argument is the directory on Linux that you want to be able to NFS mount. In this case, I've chosen to mount my home directory. The second parameter are the mount options. To see a full list of options, execute man exports. I've listed my Mac hostname as the server that is allowed to NFS mount this directory. The options I've chosen are:

    rw - read/write
    insecure - allow non-root user to NFS mount directory
    map_static - the file we created above that maps our user and group IDs from Mac to Linux box. If you want to be able to write to your NFS mount, you MUST have this option set and set correctly.

  7. Before you can mount anything on your Mac, you'll need to restart NFS on your Linux box (so it picks up the exports) with the following command:

    sudo /etc/init.d/nfs-user-server restart

  8. Finally, on your Mac, you can mount this drive with the following command:

    mount -t nfs bajan:/home/doej /Users/doej/foo

  9. This will mount your home directory on Linux (bajan is the hostname of my linux box) to the foo sub-directory in my home directory on my Mac. To test everything is working correctly, cd into the foo directory in Terminal and try creating a new file. If you can, you're all set. If not, shoot me a message and I'll try and help you out as best I can.
That's it! NFS is really easy once you get the hang of it, and even if you aren't Linux savvy, it should still be pretty easy to do.

11 comments:

  1. Great guide.

    Few caveats though:
    1) "nfs-user-server" is no longer a package in the latest Ubuntu 9.04 pkg repo, use "nfs-kernel-server"

    2) "map_static=" in your exports file is also depreciated, you should be able to substitute using anonuid/anongid
    ex:(rw,insecure,anonuid=1000,anongid=1000)

    3) Thus "sudo /etc/init.d/nfs-user-server restart" would become "sudo /etc/init.d/nfs-kernel-server restart"

    I haven't finished the mac side of things (I'm at work and didn't want to take the time to open all the bloody ports and do a nfs mount across the net - not yet anyway).

    I'll let you know if I find anything else.

    Cheers,mate.

    - Brennan

    ReplyDelete
  2. Figures they'd go and change everything after I got it working with 8.10.

    Thanks for taking the time to do this. I'll update the blog soon.

    ReplyDelete
  3. The rest checks out. Got my mounts working np.

    ReplyDelete
  4. Additionally, I had to use the flag "-o noowners" in the mount command (at the mac side) for everything to work ok...

    ReplyDelete
  5. For posterity, was this on Ubuntu? If so, what version?

    ReplyDelete
  6. I'm using 9.04 (Jaunty) and OS X 10.5.6 and 10.5.7

    ReplyDelete
  7. I've updated the instructions for 9.04 support. I had problems with Brennan's solution, specifically not being able to write to the mounted directory.

    However, installing Debian's version of the nfs-user-server package as noted above in the instructions fixed this because I could go back to using the uid/guid map file.

    ReplyDelete
  8. I believe you need to add all_squash to the exports setting suggested by Brennan. A sample from the exports man page:

    /home/joe pc001(rw,all_squash,anonuid=150,anongid=100)

    ReplyDelete
  9. Okay, the ultimate solution is...

    Install nfs-kernel-server on the ubuntu box

    on the ubuntu machine in terminal type

    id

    to get your uid and gid (typically each is 1000)

    on the ubuntu's /etc/exports add the following line

    /path/to/nfsdfolder machost.local(rw,all_squash,async,no_subtree_check,insecure,anonuid=1000,anongid=1000)

    of course replace the uid and gid values to what was produced from the id command on the ubuntu box, and input the proper paths

    in mac terminal create a directory you and set to mount with, and type the following in

    sudo mount -o noowners ubuntuiphost:/path/to/nfsfolder /path/to/mountfolder

    and there you go. hours of searching, and this page helped me piece the rest together. I can FINALLY now start to work. :D

    ReplyDelete
  10. Chance,

    I'm confused, when you mounted the drive on your Mac, did it properly show up as the right user owning the files/directories? This was the major problem I had with the nfs-kernel-server package - when I mounted, it didn't show my user as the owner and thus I couldn't write to the directory.

    Of course, the manner in which I am using this is probably not what most people are, but shouldn't be out of the norm of what people actually require.

    I'm NFS mounting an SVN repository for an Eclipse project under my Linux VM and I have Eclipse running natively on my Mac so I'm not running a memory-hogging app inside a memory hogging VM.

    But, in order to save, I have to own the files under my mount point, hence the need to own the files. (This seemed easier than attempting to get the group rwx mods correct).

    ReplyDelete
  11. Thanks so much for this. After a lot of searching it really helped and solved my issues!

    ReplyDelete