Ubuntu: setting up SSHFS for elegant SFTP

I needed to edit data on my webserver, and initially, still thinking in Windows terms I considered installing gFTP. But then I realized that networking is much more integrated in UNIX, and a separate application is not necessary: mounting my server directory was as simple as choosing to do so with Nautilus’ File > Connect to server item and providing the credentials.

But there’s an even more elegant way of doing things. SSHFS integrates remote locations into your system seamlessly, allowing you to use them as if they were genuine directories under your local file system.

For Gutsy Gibbon, all I needed to do to get the basic SSHFS functionality going was to install the sshfs package. But then I went ahead and integrated my home directory on the web server so that it’s now mounted automatically at boot. For this I adapted a set of instructions provided over at Debian/Ubuntu Tips & Tricks, as well as ones given in an Ubuntuforums thread.

First, I created the local mount point for the remote location, and gave myself permission to modify it:

sudo mkdir /media/my-remote-login
sudo chown root:fuse /media/my-remote-login
sudo chmod g+w /media/my-remote-login
sudo adduser my-login fuse

Then I added the mount point to /etc/fstab, adapting Darwin Award Winner‘s instructions to my own liking, with the following line (note that it all goes on a single line, the line breaks inserted below are only due to limited column width):

sshfs#my-remote-login@my-web-server:path-to-my-remote-home-directory /media/my-remote-login fuse users,uid=my-local-uid,gid=my-local-gid,reconnect,transform_symlinks,BatchMode=yes 0 0

(You’ll find out your local uid and gid with id.)

After this, I had to do a sudo - my-login to activate the changes made to group settings (the adding of my-login into fuse), before the remote home was mountable (mount /media/my-remote-login) with my normal user account. (Otherwise the changes won’t be in effect until the next log-out and log-in.)

Then I adapted prankst3r‘s instructions for establishing trust between my local host and the remote server:

cd ~
ssh-keygen -t rsa

I left all the fields, including the passphrase, blank (just pressed enter).

cd .ssh
sftp my-remote-login@my-web-server:path-to-my-remote-home-directory/.ssh
put id_rsa.pub
quit
ssh my-remote-login@my-web-server
cd path-to-my-remote-home-directory/.ssh
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
logout

After this I was able to log in to the server (ssh my-remote-login@my-web-server) without having to type in the password, and after the next boot, my remote home was fully accessible (with read/write/execute permissions) from /media/my-remote-login with the command-line as well as Nautilus.