Synchronize network directories with Linux

Today I'll write about how to have a network synchronized folder using SSHFS.

SSHFS is a filesystem that is mounted as a client of a remote folder. It uses the SFTP protocol.
With SSHFS if you make a change in a file it will be reflected immediately in the other computer. If we edit a file in the client that has mounted the remote folder, the change will be reflected in the remote computer and the same if we do it the other way.

You only need to install SSHFS in the client, you don't need it in the server, the only service you need at the server is the SSH service installed and running.

Installation

For Fedora and similars:




For Ubuntu and similars:





Configuration

The first thing we have to do is choose the folder we want to synchronize, so the computer where that folder is would be the server.
In the example I'll use the directory /home/rukia/foo in which the owner and group is rukia.

In the client we create a folder. In the example I'll use the directory /home/goku/fooG in which the owner and group is goku and the UID and GID is 1000. I put a different name to the folder to avoid confussions but it can have the same name.

Let's create the folder /home/goku/fooG:




Now we mount the remote folder in fooG.
Before doing this I'll explain an option that we can apply to the SSHFS command.
With this option we established if we want the directory and files of the client to have the same owner of the folder at the server or if we want that each folder has it's own owner and group.



User and group options for SSHFS

The option for establishing the user and group is idmap=Type

Where type can be:

none: The UID and GID won't be changed, SSHFS will use the owner and group of the remote directory (default option).
user: It only changes the server UID for the client UID at the client.
file: Takes the UID and GID written at a file (I still don't know how is the format of this file).

For our examples will use the user type but we will also specify the GID.

Mount the remote directory establishing the UID an GID of the client:




And now we have a synchronized folder. If we make a ls in the client directory we'll see that the owner and group is our user, while in the server it still has it's own user and group.
And the best part is that if rukia or goku write, the permissions won't change, in the client goku will still be the owner and in the server will still be rukia.

But if we had chosen none in the idmap option then the owner and group for both parts will be the owner and group of the server. But even though the client can still write in the directory and the permissions won't change.


How to synchronized the folder at startup

Now we just have to make our folder available from the startup, fot this we add the next line at /etc/rc.local on top of exit 0:






Mount the directory without a password

If we want the before explanation to work and our directory can be mounted at system boot, we have to avoid the remote computer to ask for a SSH password. We can solve this creating public keys.

We create the key for the client user:




For default the key is savedd in the .ssh home directory of the user that generated it, in our example is goku, so the key is:




Now we have to copy this key to the server, we can do it with scp, the key has to be inside the file authorized_keys that is in the .ssh home directory of the ssh user that we'll use to log in, in this case is rukia. So we have to apply the next commands:

Copy the key to the server:




Add it to the authorized_keys file (this is done in the server side):




And finally for security reasons we just give permissions to the owner of the file:



And we have our synchronized folder. This can be done in more computers not just with one, you can have more clients using the same folder applying all this steps.
I hope this tutorial is useful, any doubt or complaint just comment.

Comentarios

Entradas populares de este blog

Como calcular permisos de umask

Configuración de un DNS IPv6 local en Linux con Bind

Permisos Unix: Los bits SUID, SGID y sticky