Scp command over ssh without password in script
If below scp command is being run on local_host
,then you are trying to scp as local_user
to local_host
as local_user
- ofcourse this would prompt for password, as you had only password less login for remote <-> local users only - not local <-> local
scp /home/${remote_user}/info.txt.gz ${local_user}@${local_host}:/root/
Related videos on Youtube
fuser
Updated on September 18, 2022Comments
-
fuser almost 2 years
Here is my problem: I would like to perform scp command after ssh to remote server without typing any passwords. I describe my steps. My first steps were:
ssh-keygen -t rsa
andlocal@host$ ssh-copy-id -i /root/.ssh/id_rsa.pub remote@host
on local machineThen I did the same on remote machine:
ssh-keygen -t rsa
andremote@host$ ssh-copy-id -i /root/.ssh/id_rsa.pub local@host
After all described above I could do ssh on both machines without typing any password. I could perform scp command on remote machine like
scp /home/remote/info.txt.gz local@host:/root/
and everything worked perfectly.Then I tried to make a script with some actions described below and the last step in my script was
scp
command that did not work as I expected.#!/bin/bash remote_user=$1 remote_host=$2 local_user=$3 local_host=$4 echo "Testing connection to ${host}..." ssh -n -o NumberOfPasswordPrompts=0 ${remote_user}@${remote_host} if [ $? -ne 0 ]; then echo "FATAL: You don't have passwordless ssh working." echo "Try running ssh-keygen" exit 1 fi echo "Okey. Starting the process." ssh ${remote_user}@${remote_host} netstat -tulpn > /home/${remote_user}/info.txt;uptime |awk '{ print $3 }' >> /home/${remote_user}/info.txt; if [ $? -ne 0 ]; then echo "An error occurred." else echo "File is ready for gzipping!" fi gzip /home/${remote_user}/info.txt if [ $? -ne 0 ]; then echo "file was not archived" else echo "Archive is ready!" fi echo "Starting copy archive from ${remote_host} to ${local_host}" scp /home/${remote_user}/info.txt.gz ${local_user}@${local_host}:/root/ if [ $? -ne 0 ]; then echo "Error while transferring!" else echo "Copy has been transferred successfully!" fi
Scp command asked me for password o_O.
When I did all steps in script manually everything worked perfectly but in script
scp
demanded password. I read a lot through stackexchange and found this answer Using an already established SSH channel. This answer requires Open SSH but my problem can be resolved manually via SSH as I said but in script it did not work. What am I going to do to makescp
works without password?-
Criveti Mihai over 8 yearsTL;DR. Is your SSH key encrypted (ex: do you type in a password when using your key?). Are you using
ssh-agent
(or PuTTY Agent on Windows)? -
Eric Renouf over 8 yearsYou know you're executing that
scp
from the first host right, not fromremote_host
The only command you're execute on the remote host isnetstat -tulpn
everything else is on your first host -
David King over 8 yearsTry adding the
-vv
flag on thescp
command to see if it's even attempting to do key-based auth. -
fuser over 8 yearsCriveti Mihai, yes I am using Putty Agent on Windows.
-
gogoud over 8 yearsFirst line of your script should be
#!/bin/bash
. Did you do all actions as root - or some as another user? I think your scp line last parameter should be${remote_user}@${remote_host}:/root/
- assuming you really want to save the .gz file at remote's /root which seems a strange location. -
fuser over 8 yearsI did all actions as root - FOR SURE! Such locations I had given only for example - because we can use in spite of them real host names and ip addresses.
-
fuser over 8 years
#!/bin/bash
it is just my blot. Thanks! I have corrected it. -
roaima over 8 yearsYour code uses paths such as
/home/${remote_user}/
on the local machine all over the place. Is that really intentional?
-
-
fuser over 8 yearsI tried to use what you said, mtk. Something like this
sftp ${local_user}@${local_host} <<EOT put info.txt.tgz quit EOT
inspite ofscp
command but it asked for password again -
ekoeppen over 8 years@fuser some step would be missing. check this post to setup passwordless sftp stackoverflow.com/a/15808596/1135954
-
fuser over 8 yearsWhy do I need to set up passwordless sftp If I have already had passwordless ssh?