Wednesday, June 26, 2013

What is rdist? How rdist works in Linux?

Hello all,
Today, I would like to talk about “rdist” command. “rdist” is a remote file distribution program. It is a program to maintain identical copies of files over multiple hosts. It preserves the owner,  group, mode and mtime(modification time) of files and can update programs that are executing.

To install rdist, run the following command on server and all clients that you want to be in sync with server:

yum install rdist

Also, in order to communicate and distribute the files from server to clients, you need to install/start sshd service on clients and setup ssh in such a way that does not require a password but authenticate the client with cryptographic key pair. Therefore, run the following commands on server (Figure 1):

su -
ssh- keygen –t rsa –b 1024 
(Just hit Enter for the key and both passphrases)
ssh-copy-id root@client 
(copy key to clients. Note: replace the “client” with actual hostname of your clients. For example, MyTestMachine.localdomain).  And of course, you have to have a DNS to convert the hostname to IP address or simply define it in /etc/hosts.
Repeat this step for each client.

On clients, run the following commands:
service sshd start
chkconfig sshd on

                                                                    Figure 1

Now, let’s look at how it actually works. rdist looks for a control file called “Distfile" or “distfile” in current directory. If it’s not in current directory, you can explicitly specifies the control file’s name by –f flag, something like this:  rdist –f distfile

Inside “distfile” file, first you need to define the list of files that you want to distribute them on clients. For example,
SYSTEM_FILES = (/etc/group /root/test.txt)           --> separate files with one space
Then, list the clients (their host name):
HOSTS = (machine1.localdomain machine2.localdomain machine3.localdomain)

Now, define statements. The form of statement looks like this:
Label: pathnames -> destinations commands

So, the Label can be any name. The point of using Label is that you can run “rdist label” command to  distribute only the files described in a particular statement.

The pathnames and destinations are lists of files to be copied and hosts to copy them to, respectively. For instance,
All_clients: ${SYSTEM_FILES} -> ${HOSTS}
means copy /etc/group and /root/test.txt to machine1, machine2, and machine3
and of course you can use regular expression in pathnames, something like /usr/lib/*

By default, rdist copies the files and directories listed in pathnames to the equivalent paths on each destination machine. You can modify this behavior by supplying a sequence of commands and terminating each with a semicolon.

The commands are:
notify namelist;
except pathlist;
except_pat patternlist;
special [pathlist] string;
cmdspecial [pathlist] string;

The “notify” command takes a list of email addresses as its argument. So, when a file is updated, it sends an email to the defined list of emails. If you don’t add a complete email address, like, it will add the name of destination host as suffix. So, if you just enter Khosro, for example, it will add Khosro@machine1.localdomain to it.

The “except” and “except_pat” commands are used to except pathnames from the list of files to be copied. You can define pattern or regular expression in “except_pat” command as well. Therefore,
except /root/test.txt ;” command don’t copy test.txt file to clients.

The “special” command executes a shell command on each remote host. If there is a pathlist, the rdist executes the command once after copying each of specified files. For example,
special /root/test.txt “echo 1 > test2.txt” ;
does nothing when it copies the /etc/group, but it runs “echo 1 > test2.txt command when it copies /root/test.txt
If you don’t specify a pathlist, rdist executes the command after copying every file. For instance,
special  “echo 1 > test2.txt” ;
copies /etc/group, then runs “echo 1 > test2.txt”. and again, copies /root/test.txt and then runs “echo 1 > test2.txt” 

cmdspecial” is the same as “special”, however, it executes the shell command once after all copying is complete. So, in our example, it copies the /root/test.txt and /etc/group first, and then runs “echo 1 > test2.txt” command.

The following shows how to run rdist command through ssh tunnel:
rdist -P /usr/bin/ssh -f distfile

Now, let's see some examples:  

Scenario 1:
If you want to run a shell command after copying the second file, you need a distfile like Figure 2:
                                                                   Figure 2

Figure 3 shows the notification email. It has been sent to user Khosro:

                                                                    Figure 3

And Figure 4 shows the copied files in destination:

                                                                     Figure 4 

Scenario 2:
If you want to execute a shell command after copying every file, you just need to remove the pathlist in "special" command (Figure 5)

                                                                      Figure 5

Scenario 3:
If you want to run a shell command once after all copying is complete, you need to use "cmdspecial" command (Figure 6)

                                                                   Figure 6

Scenario 4:
If you want to except a file, you need to use "except" command in distfile (Figure 7)

                                                                  Figure 7

And That's all. Hope you enjoyed.
Khosro Taraghi

No comments:

Post a Comment