How to mount remote directory over SSH

SSH is great, secure and stable protocol, that is used for remote access to *nix servers and file transfer. Now it is possible to mount remote folder. Remote server does not require any scripts or modules. You only need to install SSHFS module on your client.

Ubuntu, Centos, Debian, Mint have precompiled packages ready.

Installing SSHFS on a RHEL (Red Hat)/CentOS Linux

First, turn on EPEL repo

yum -y install epel-release

and then type the following yum command to install FUSE-Filesystem to access remote filesystems via SSH on a CentOS/RHEL:

sudo yum install fuse-sshfs

How do I mount the remote file system?

sshfs user@server /path/to/mountpoint
sshfs user@server /path/to/mountpoint options

Verify it:

sudo df -h

Sample outputs:

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/wks05-root 487G 114G 350G 25% /
none 4.1k 0 4.1k 0% /sys/fs/cgroup
udev 17G 4.1k 17G 1% /dev
tmpfs 3.4G 1.9M 3.4G 1% /run
none 5.3M 0 5.3M 0% /run/lock
none 17G 160k 17G 1% /run/shm
none 105M 50k 105M 1% /run/user
/dev/sda1 239M 89M 138M 40% /boot
[email protected]:/ 20G 12G 6.8G 64% /mnt/server1

How can I permanently mount the remote file system by updating /etc/fstab?

Edit the /etc/fstab file, the syntax is:

userNameHere@FQDN_OR_IP_HERE:/path/to/source/ /local/mountdir/ fuse.sshfs defaults,_netdev 0 0

Examples

sshfs#[email protected]:/ /mnt/server1

Another example with additional options:

sshfs#[email protected]:/ /mnt/server1 fuse defaults,idmap=user,allow_other,reconnect,_netdev,users,IdentityFile=/path/to/.ssh/keyfile 0 0

Recommend option for on-demand mounting if you are using systemd:

[email protected]:/project/www/ /mnt/server1 fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/vivek/.ssh/id_rsa,allow_other,reconnect 0 0

Where:

  • [email protected] : Remote server with sshd
  • fuse : File system type.
  • idmap=user : Only translate UID of connecting user.
  • allow_other : Allow access to other users.
  • reconnect : Reconnect to server.
  • _netdev : The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).
  • users : Allow every user to mount and unmount the filesystem.
  • IdentityFile=/path/to/.ssh/keyfile – SSH key file.

Source and full source: http://www.cyberciti.biz/faq/how-to-mount-remote-directory-filesystems-with-sshfs-on-linux/

Leave a Reply

Your email address will not be published. Required fields are marked *