This is an update of my previous post that you can read here.

The current project I am working on needs to access to a folder on a remote server. It seems like a simple task, but there is one issue: I am a Mac user.

Mounting a server folder is very useful if you have a lot of data to share with your colleagues. It is insane to copy it to your hard drive every time that it changes or manage large amounts of data with version control since it will slow down the repository.

The best solution we found in the lab is using SSH and mounting folders using sshfs. It works really well in Linux and we don't want to use a different system for other operating systems.

For this project I need a new folder inside my /home folder to use as a mount point for the remote file system. This point is not a negotiable due to the project requirements.

When I run mkdir on my mac to create the /home/projects folder, the command runs but doesn’t create a folder. I ran the mount command and found that the /home folder on Mavericks is an auto mounted folder (see the automount man page).

My first thought was: "well, I could deactivate the automount”. However, radical changes like this are ill-advised. In the next sections I will explain how to install SSHFS for Mac and two different ways to mount a remote ssh folder: deactivating the automount and using it.

Installing SSHFS for Mac

The easy way to install SSHFS is navigate to http://osxfuse.github.io and download two files:

  • OSXFUSE 2.7.3
  • SSHFS 2.5.0

Also, you can use homebrew, but, in this moment, the OSXFUSE version is still the 2.7.3 and you can have problems using the automount.

Deactivating the automount

To deactivate the automount, I only need to edit the file /etc/auto_master and comment the line starting with /home auto_home.... After that, I ran sudo automount -vc to tell the daemon that the configuration file was changed. I unmount the /home folder running "umount /home" (be careful, you must not be in this folder while running this command). At this point, I can create a new folder, change the permissions for the new folder created, and mount the remote filesystem. The steps were:

$ sudo mkdir /home/projects 
$ sudo chown myuser:staff /home/projects 
$ sshfs mysecretuser@mysecrethost:/my/secret/folder /home/projects

Note: type yoursecretpassword

Using automount

For use OSXFUSE on a Mac, we need to write a single line in the file /etc/auto_home, but, we need to prepare a few other items.

The automount system runs as a daemon without user interaction. This means that we need to create a shared key, which the daemon can use to connect to the server.

To create a shared key, run:

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/myuser/.ssh/id_rsa): /Users/myuser/.ssh/myserver_id_rsa

Note: the passphrase can be empty

Next we must copy the key to the server. To do that, we need to install the ssh-copy-id script:

$ brew install ssh-copy-id

Copy the key using the previously installed script:

$ ssh-copy-id -i ~/.ssh/myserver_id_rsa.pub myuser@myserver

Note: It will ask for the password

Then test if it is ok trying to connect to the server:

$ ssh -i ~/.ssh/myserver_id_rsa myuser@myserver

Note: It won't ask for the password!!!

At this point, we can manually mount sshfs like in the previous section, but using the shared key:

$ sshfs -o IdentityFile=~/.ssh/myserver_id_rsa myuser@myserver:/my/remote/folder /my/local/folder

However, if we want to automatically mount the remote folder, we still have work to do.

For use OSX fuse in the automount tools, we need to set the next kernel variable to 1:

osxfuse.tunables.allow_other=1

It will be nice to set this variable in the /etc/sysctl.conf file, but the problem is that we can only set up this variable once the kernel module is loaded. Apple encourages programmers to use launchd, so, I started using it.

The solution is really simple, we only need to create these files into /Library/Application Support/AmaralAutoMount folder:

org.amaral-lab.automount.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.example.app</string>
        <key>ProgramArguments</key>
        <array>
            <string>/bin/sh</string>
            <string>/Library/Application Support/AmaralAutoMount/org.amaral-lab.automount.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
    </dict>
</plist>

org.amaral-lab.automount.sh

#!/bin/sh
/Library/Filesystems/osxfusefs.fs/Support/load_osxfusefs
sysctl -w osxfuse.tunables.allow_other=1

The first file define how to run the second one that is a sh script.

Now, the next step is configure launchd to use this file. To do that, I suggest to create a soft link to the “plist” file:

$ cd /Library/LaunchDaemons
$ sudo ln -s "/Library/Application Support/AmaralAutoMount/org.amaral-lab.automount.plist" .

To run this script we can restart the computer (easy way) or run this command:

$ sudo launchctl load /Library/LaunchDaemons/org.amaral-lab.automount.plist

Finally, we have all the prerequisites to write this line at the end of /etc/auto_home:

projects -fstype=sshfs,allow_other,idmap=user,ro,IdentityFile=/Users/myuser/.ssh/myserver_id_rsa myuser@myserver:/home/projects

Now, run this command to tell the daemon that the configuration files are changed so we can use the folder:

$ sudo automount -vc
$ ls /home/projects

I hope these lines will be useful for you!