Connecting and configuring SSHFS on Linux

The SSHFS file system (Secure Shell fileSystem) allows you to mount the file system of a remote server using the SSH protocol. This can be very convenient if you need to transfer a lot of data to a remote server or download this data from there. Of course, there is the scp utility, but sometimes it is much more convenient to view the file system in the file manager.

In this short article, we will look at how to connect SSHFS in Linux, as well as how to configure automatic mounting of this file system at boot.

  • Connecting SSHFS to Linux
  • Automatic mounting of SSHFS
  • Conclusions

Connecting SSHFS to Linux

SSH access to a remote server is sufficient for the file system to work. You need to install the sshfs package on the client machine. If it is not installed, the installation command in Ubuntu will look like this:

sudo apt install sshfs

Mounting SSHFS is performed using the command of the same name. Its syntax is as follows:

$ sshfs options имя_пользователя@адрес:/путь /point / mount

For example, to mount a remote file system at 192.168.56.103 as root, just run:

sudo sshfs [email protected]:/ /mnt

Now you can view the contents of the file system. But there are a few disadvantages here, first – it is not always convenient to mount the file system on behalf of the superuser, and secondly, the mounted file system will be read and write only to the superuser.

To be able to mount on behalf of a normal user you need to create a fuse group:

sudo groupadd fuse

Then add the current user to this group:

sudo usermod -aG fuse $USER

After that, log in to the system so that the changes are applied. As a normal user, you will not be able to mount sshfs in /mnt because you do not have write access to this folder, create a folder to mount in the home folder:

mkdir ~/mnt/

Next, you can try to mount:

sshfs [email protected]:/ ~/mnt

Now you can use this folder to share files with the server. If you want other users to be able to access this folder, use the allow_other option. It will only work if the user_allow_other option is present in the /etc/fuse.conf file. Add it:

sudo vi /etc/fuse.conf

Now you can mount:

sshfs -o allow_other [email protected]:/ ~/mnt

To unmount a file system use the familiar umount command:

sudo umount ~/mnt/

Automatic mounting of SSHFS

You can configure automatic mounting of SSHFS in the /etc/fstab file. To do this, you will first have to create an SSH key and send it to the remote server. Create a new key:

ssh-keygen

Then pass it to the server:

ssh-copy-id -i ~/.ssh/id_dsa.pub [email protected]

When the key is loaded, it will be enough to pass the path to the key to the utility in the mount options. To make sure that everything works, run:

sshfs -o identityfile=~/.ssh/id_dsa.pub [email protected]:/ ~/mnt

If everything works, you can create a configuration dump for /etc/fstab:

sudo vi /etc/fstab

[email protected]:/ /mnt fuse.sshfs noauto,x-systemd.automount,_netdev,follow_symlinks,identityfile=/home/sergiy/.ssh/id_dsa,allow_other,default_permissions,reconnect 0 0

The path to the key file must be complete, so replace the user name with your own. In order not to get access errors when writing, it is desirable that the user name on the local machine and on the remote machine match. Alternatively, you can specify the user ID and the owner group during the mount. First look at the UID and GID of the current user:

cat /etc/passwd | grep $USER

In this case, it’s 1000. The first digit is the UID, the second is the GID. Then pass them in the mount options uid and gid:

[email protected]:/ /mnt fuse.sshfs uid=1000,gid=1000,noauto,x-systemd.automount,_netdev,follow_symlinks,identityfile=/home/sergiy/.ssh/id_dsa,allow_other,default_permissions,reconnect 0 0

Everything should work now.

Conclusions

Now you know how to connect and configure SSHFS Linux. As you can see, this is not very difficult, but at the same time it is convenient for transferring a large number of files.

Source: losst.ru

(Visited 10 times, 1 visits today)

Leave a Reply

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