SSHFS

This article is a stub. You can help by expanding it.
Resources

sshfs (SSH File System) is a secure shell client used to mount remote filesystems to local machines. SSHFS uses Filesystem in Userspace (FUSE) to mount filesystems in a location users can easily access.

Installation[edit | edit source]

Kernel[edit | edit source]

Since SSHFS uses FUSE it will need to be enabled in the kernel.

KERNEL Enabling FUSE in the kernel
File systems  --->
   [*] FUSE (Filesystem in Userspace) support
Note
When enabling a built-in (non-modular) feature or driver in the kernel remember a recompile will be needed and the new kernel loaded into memory (system reboot) before changes will take effect. This step should be completed before moving on to other sections in this article.

USE flags[edit | edit source]

There are currently no available USE flags for SSHFS.

Emerge[edit | edit source]

Use the emerge command to ask Portage to install net-fs/sshfs:

root #emerge --ask net-fs/sshfs

Usage[edit | edit source]

In order to use SSHFS a SSH daemon needs to be running on the remote machine.

To mount a remote file system locally the right privileges will be needed. When attempting to mount a remote directory without adding a user name to the command the current active user name will be used by default. For example, if the user Larry is currently the active user on the system and this command is ran:

larry@example $sshfs remotehost:/var/db/repos/gentoo/ /mnt/portage

The command will most likely fail because larry's user name will be sent to the remote system. The previous command is the equivalent of running this command:

larry@example $sshfs larry@remotehost:/path/to/remote/system /path/to/local/sshfs/mount

To change the user, put the name of the user before the IP address to domain name. For example, to login to the remote system using the remote system's root user name and password, use:

user $sshfs root@remotehost:/path/to/remote/system /path/to/local/sshfs/mount

Mounting[edit | edit source]

larry@example $sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint

Unmounting[edit | edit source]

To unmount a directory with SSHFS use the fusermount command with the -u option:

larry@example $fusermount -u /path/to/local/sshfs/mount

Permissions based options[edit | edit source]

In order to have read/write access to a mounted remote directory you may need to use the allow_other and/or allow_root options depending on if you are a regular user or root. Simply enable as shown below (replace allow_other with allow_root, if root).

larry@example $sshfs -o allow_other larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint

Alternatively, the uid, gid, and umask options can be used to further fine tune permissions. When setting multiple options at the same time use a space separated list after a single -o.

CODE umask, uid, gid options
-o umask=M
    set file permissions (octal)

-o uid=N
    set file owner

-o gid=N
    set file group

Automating the connection[edit | edit source]

For remote file systems that need to be mounted frequently, it is wise to automate the sign in process. Automation can be achieved by an public/private SSH key pair combined with special instructions to mount the remote filesystem on a specific event (user login, or system boot for example).

The first step is to setup the SSH key pair on the local and remote machines. Visit the Passwordless Authentication section of the SSH article for further instructions on how set up an SSH key pair. When finished return to this article.

After the key pair has been created and properly set up, determine what event will be used to start the connection automatically. It is common for a system to attempt to remotely mount a file system upon user login or system boot. Controlling the sshfs mount depends on what software the user will be implementing in their local environment. There are several ways to handle the task.

fstab[edit | edit source]

sshfs can be used inside a system's fstab file. This enables over-the-network filesystems to be assigned to act as local filesystem mounts. Filesystems using sshfs require slightly different mount options, so be sure to look at the man page to be sure the options are correct in each use case. A example of using sshfs in fstab:

FILE /etc/fstabAdding sshfs to fstab
# Automatically mount ~/Music on connection
sshfs#SERVER_USER@remotehost:/SOURCEDIR /home/USER/Music fuse user,_netdev,idmap=user,transform_symlinks,identityfile=/home/USER/.ssh/id_rsa,allow_other,default_permissions,uid=1000,gid=1000 0 0

Login shells[edit | edit source]

Most shells include support for commands to be executed during user login or logout. This section will provide examples on how to automate the connection using built in shell script.

Before proceeding, it is necessary to know which shell is being used. Execute the following command as the user of interest to determine which shell is being used:

user $echo $SHELL

Possible output:

  • /bin/bash for bash
  • /bin/sh for sh
  • /bin/tcsh for tcsh (csh)
  • /bin/zsh for zsh
Note
For more information on available shells see the Shell article.
Bash[edit | edit source]

When using a bash shell, create a ~/.bash_login and ~/.bash_logout files in the user's home directory and add the sshfs command to the file.

user $touch ~/.bash_login ~/.bash_logout

Mount on shell login:

FILE ~/.bash_loginAdding sshfs mount to bash login shell
# Added to mount a remote directory at user login
sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint

Unmounting on shell logout:

FILE ~/.bash_logoutAdding sshfs unmount to bash logout shell
# Added to unmount a remote directory at user logout
fusermount -u /path/to/local/sshfs/mountpoint
Sh[edit | edit source]
user $touch ~/.profile
FILE ~/.profileAdding sshfs mount to sh login shell
# Added to mount a remote directory at user login
sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint
Zsh[edit | edit source]
user $touch ~/.zlogin
FILE ~/.zloginAdding sshfs mount to zsh login shell
# Added to mount a remote directory at user login
sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint
Tcsh (Csh)[edit | edit source]
user $touch ~/.login
FILE ~/.loginAdding sshfs mount to tcsh or csh login shell
# Added to mount a remote directory at user login
sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint

Desktop environments[edit | edit source]

Most desktop environments include methods for automatically starting programs.

GNOME[edit | edit source]

KDE[edit | edit source]

OpenBox[edit | edit source]

OpenBox uses the autostart file located in each user's home directory.

FILE ~/.config/openbox/autostartAdding sshfs mount OpenBox autostart file
# Added to mount a remote directory at user login
sshfs larry@remotehost:/path/to/remote/directory /path/to/local/sshfs/mountpoint &
Note
It is important to include the & (ampersand) on the end of commands issued inside OpenBox's autostart file.

See also[edit | edit source]

  • CurlFtpFS — allows for mounting an FTP folder as a regular directory to the local directory tree.
  • SCP — an interactive file transfer program, similar to the copy command, that copies files over an encrypted SSH transport.
  • SSH — an encrypted terminal program that replaces the classic telnet tool on Unix-like operating systems.
  • SFTP — an interactive file transfer program, similar to ftp, which performs all operations over an encrypted SSH transport.

External resources[edit | edit source]

This article is issued from Gentoo. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.