The current project I am working on needs to access to a folder on a remote server. It seems to be 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 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 remote a 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.0
- SSHFS 2.5.0
The default installation is enough. Probably, newer versions will work well. Also, you can use homebrew, but, in this moment, the OSXFUSE version is still the 2.6.4 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 run 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 other stuffs first.
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, we do:
$ 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
And 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 must continue with the process.
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.
To do that we write these lines at the end of the file /etc/rc.common
:
/Library/Filesystems/osxfusefs.fs/Support/load_osxfusefs
sysctl -w osxfuse.tunables.allow_other=1
The first line loads the kernel module and the second one sets the variable.
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
Update:
I just upgraded to Yosemite and the file /etc/rc.common
was no longer running every time I restarted my computer. So, I needed to find another solution for setting up my automounting SSHFS.
The new solution is really simple, we only need to remove the changes to the file /etc/rc.common
that I suggested in the previous paragraphs (now they are stroke) and create the following files into the /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 defines how to run the second file, which is a bash script.
Now, the next step is to 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 .
And now, you can continue using SSHFS in your new Yosemite.
You can see the new post here
I hope these lines will be useful for you!