copy files using SSH from a server to another..both remote
i have 2 remote servers. i want to connect from a third machine to the first server using ssh and copy some files from this server to the second server from the third machine itself.
can i automate this process using cron?
pls provide a step by step procedure to do this.
to copy file remotely and securely, use scp below is the generally usage
note: i am using IPs instead of hostnames and also assumes I am on first server.
$scp filename 18.104.22.168:/tmp/
the above command will copy the file from frist server to second servers /tmp direcotory
Yes you can automate the task using cron.
but then do i need to establish a ssh connection before issuing the scp command?
also can i get a method to extract a file using ssh from a remote server i.e. can i put a cron job to extract a file from a remote server on my server?
The scp command automatically uses ssh to copy. You can use scp to either copy to, or from a remote machine.
the syntax is:
scp <source file> <destination file>
Depending on whether you are copying to, or from, you put the remote file either in the source or destination file.
scp localfile 10.1.1.1:/remotefile
scp 10.1.1.1:/remotefile localfile
scp and passwordless keys
The other posts are accurate. As noted, scp actually does an ssh login to do the copy. So, to use ssh as a user named bigdog to transfer a file named /home/poodle/test from a host named dog to /home/persian on a host named cat using user littlecat to log into cat, do this:
scp bigdog@dog:/home/poodle/test littlecat@cat:/home/persian
You would have to enter the passwords for both users.
If you don't specify a user scp will assume you are logging in to remote machines as the same user as is currently logged in to the local machine. So, if you are root and don't specify the "username@" it would assume "root@" and ask for the root password of the remote machine.
So, to do this with a cron job, and assuming that you are logging in as root you have the problem of not being there to enter the root password. The way around this is to use ssh-keygen to generate a passwordless key. IIRC something like "ssh-keygen -t dsa -o filename" should generate a public key/private key pair of type dsa and put them in filename.pub and filename, respectively. If you don't use "-o" the default is to name them id_dsa and id_dsa.pub.
You generate these keys on the host that you want to run the cron job on. You then put the public key in the ~root/.ssh/authorized_keys file of the hosts that you want to copy to/from and they will trust the first machine to allow scp without someone there to input the root password.
Whew! This reply turned into a bit of a book. If it's not clear you can probably find a how-to that will explain it better and in more detail. A google for something like "passwordless ssh authorized_keys" might do it, but I need to get back to work for now.
to do SSH password-less entries you have to first create your keys , one private and one public with this command
ssh-keygen -t rsa
for most default machines you can simply answer yes to all the files
now you wanna scp like this
scp /root/.ssh/id_rsa.pub remotemacine:/root/.ssh/autorized_keys2
it will ask your a password , but that will be the last time it does. If you want some more ssh uses check out the faqs, man pages and a good book is 101 hacks for the linux server it was a great read
thanx everybody...its pretty clear now...
thanx for the help
|All times are GMT -5. The time now is 07:07 AM.|