---
title: "Synchronize remote hosts"
description: "A simple script to sync remote hosts, steps to achieve it : 1.Avoid SCP prompting for password [ local host ] = ssh-keygen."
url: "https://h3manth.com/scribe/synchronize-remote-hosts/"
canonical_url: "https://h3manth.com/scribe/synchronize-remote-hosts/"
markdown_url: "https://h3manth.com/scribe/blog/synchronize-remote-hosts.md"
publishDate: "2010-01-18T00:00:00.000Z"
author: "Hemanth HM"
category: "linux"
tags: ["linux"]
word_count: 145
reading_time_minutes: 1
x_markdown_tokens: 365
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Synchronize remote hosts

> A simple script to sync remote hosts, steps to achieve it : 1.Avoid SCP prompting for password [ local host ] = ssh-keygen.

A simple script to sync remote hosts, steps to achieve it :

1.Avoid SCP prompting for password [ local host ] =>
ssh-keygen -t dsa -b 2048 -f ~/key.pub

2.SCP the key for the first time to the [ remote host ] =>

scp ~/key.pub remoteuser@remotehost:/home/remoteuser/

3. In remote host : 
[ Authorize the Host from which key was received ]

if [ ! -d .ssh ]; then 
     mkdir .ssh 
     chmod 700 .ssh 
fi
mv key.pub .ssh/
cd .ssh/
if [ ! -f authorized_keys ]; then 
     touch authorized_keys 
     chmod 600 authorized_keys ; 
fi
cat key.pub >> authorized_keys

"rsync + Cron jobs" to keep both the hosts in sync :

##################################################################
#!/bin/sh

RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/`whoami`/key.pub
LOCAL_PATH=/sync/dir
REMOTE_USER=remote-user
REMOTE_HOST=remote-host
REMOTE_PATH=/remote/dir

$RSYNC -az -e "$SSH -i $KEY" \
$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH 

##################################################################

## Setting up cron 
mv syncScp.sh /etc/cron.daily/

or
## crontab -e 
0 5 * * 5 /home/you/syncScp.sh
