logo

jupfaf

some stuffs, and some others ...

Shared folder between two machines

Written by julien no comments

I sometimes need to have a shared folder between some of my test machines. The need is sporadic, so I don't want to setup a NFS server. I found sshfs for this usage which is really handy.

It is using the ssh layer, so one of the machine will be the server, and the other the client. As usual, my test machines are running on CentOS 7.

On the server side:

  • my user is named admin
  • the shared folder is /home/admin/shared
  • ssh server is already configured

On the client side:

  • my user is named user
  • the shared folder is /home/user/shared
  • ssh client is installed
  • I need to install sshfs

The package name is fuse-sshfs, and it can be found in the EPEL repository (so this repository needs to be configured):


user@client:~$ sudo yum install -y sshfs

The shared folder can now be mounted by using the following command:


user@client:~$ sshfs admin@server:/home/admin/shared ~/shared
(it is not recommended to mount the folder as a superuser)

Now the folder is shared and synchronized between the two machines, so if a file is created on the server:


admin@server:~/shared$ touch file.txt
it will be visible from the client:

user@client:~/shared$ ls
file.txt
and vice versa.

Unmount the folder has to be done on the client side, with the command:


user@client:~$ fusermount -u ~/shared

Because it is using the ssh layer, it is possible to configure the connection to use a private key and avoid sshfs asking for your admin@server password. First, the key has to be generated:


user@client:~$ ssh-keygen -t rsa -b 2048
(check for the options to generate a far more secure key)

then sent to the server:


user@client:~$ ssh-copy-id -i /home/user/.ssh/id_rsa admin@server
A config file can be created to not be bothered with connection options:

user@client:~$ cat >> .ssh/config
Host server
        User admin
        IdentityFile /home/user/.ssh/id_rsa
^C
user@client:~$ chmod 600 .ssh/config
The shared folder can now be mounted with the following command:

user@client:~$ sshfs server:/home/admin/shared ~/shared
and no password will be asked.