Copy directory from one ssh server to another

Asked

Viewed 2,187 times

3

I need to copy a directory on a server with a script .sh running on another server. I read about the command scp and saw some examples of use, but did not figure out how I put the password of the server I am accessing to make the copy directly in my script, follows an example of the command scp:

scp /path/to/file username@a:/path/to/destination

It doesn’t have to be just one line, it could be more...

2 answers

3


1 - Create the keys as already reported in the comments. Use the commands below:

ssh-keygen
  • Answer the questions, you can type for all

2- Copy key to target host

ssh-copy-id host_ou_ip_destino

If the target machine uses another user or port in SSH make the copy like this:

ssh-copy-id "[email protected] -p 2200"

Confirm the options that appear from finger-print recording.

3 - Testing authentication with keys.

ssh host_ou_ip_destino

4- To copy the directory, although sending a tar is more efficient as commented in your post by Daniel Omine, utilize:

scp -rp dir_origem [email protected]:/dir_destino

EDITING TO GENERATE LOCAL TAR
5- To generate a local tar with remote files, use:

ssh [email protected] "tar -cvzf - /dir_remoto/origem/" > local.tar

Source:
Copy directory: https://serverfault.com/a/264598/209974
Beget and copy key: https://darkstrikerd.wordpress.com/2010/05/14/usando-ssh-ou-scp-sem-senha/

  • Hi Celso, it worked the way you answered, just one more question, how to create tar straight on the other machine and pull tar instead of directory ?

  • Thanks Celso!!!!!

  • In the example I posted there, it is easier than with SCP, because tar is created on the direct local machine. It will not even generate a tar on the remote machine.

1

You can use the sshpass:

sshpass is a Utility Designed for running ssh using the mode referred to as "Keyboard-Interactive" password Authentication, but in non-interactive mode.

To install on Ubuntu/Debian:

sudo apt-get install sshpass

No Centos:

yum -y install sshpass

To use it, do so:

#!/bin/bash
sshpass -p "<senha>" scp -r /path/to/file [email protected]:/path/to/destination

The option -r of scp is used to recursively copy the directory.

  • I cannot use sudo command on the server, it does not allow!

  • how do I use the key after ? I make the same call I put to the question ? scp /path/to/file username@a:/path/to/Destination

  • I have id_rsa.pub inside the /.Shh/authorized_keys/ folder, so I use the scp /path/to/Diretory username@host:/path/to/Destination command, then it asks for the password and the worst is that I am putting the password and the Putty screen is locked, if I hit enter it goes down the line but nothing happens, and I guess I wasn’t supposed to ask for the password

  • yes the file is already on the remote server! keeps asking for the password

  • I typed the remote server password and pressed Enter! generated the 3 files and the . pub sent to the remote server

  • @A.S. That must be the problem! Type the command ssh-keygen -t dsa when asking for password press Enter twice! then send the public key to the remote server and do the test again!

  • the command is "ssh-keygen -t dsa" or the same as the article q gave me "ssh-keygen -t rsa"? By enabling scp’s debug mode I can see the transactions between the keys, but he says he can’t find the rsa...

  • @A.S. It is "rsa"....

  • @A.S. Try to generate the keys again, but this time do not enter password, then send the public key to the other server, this is to work..

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.